File size: 2,142 Bytes
d2897cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<?php

namespace Mautic\CoreBundle\IpLookup;

use GuzzleHttp\Client;
use Mautic\CoreBundle\Helper\CoreParametersHelper;
use Psr\Log\LoggerInterface;

abstract class AbstractLookup
{
    public $city         = '';

    public $region       = '';

    public $zipcode      = '';

    public $country      = '';

    public $latitude     = '';

    public $longitude    = '';

    public $isp          = '';

    public $organization = '';

    public $timezone     = '';

    public $extra        = '';

    /**
     * @var string|null IP Address
     */
    protected $ip;

    /**
     * Return attribution HTML displayed in the configuration UI.
     *
     * @return string
     */
    abstract public function getAttribution();

    /**
     * Executes the lookup of the IP address.
     */
    abstract protected function lookup();

    public function __construct(
        protected ?string $auth = null,
        protected $config = null,
        protected ?string $cacheDir = null,
        protected ?LoggerInterface $logger = null,
        protected ?Client $client = null,
        protected ?CoreParametersHelper $coreParametersHelper = null
    ) {
    }

    /**
     * @return $this
     */
    public function setIpAddress($ip)
    {
        $this->ip = $ip;

        if ($this->shouldPerformLookup()) {
            // Fetch details from the service
            $this->lookup();
        }

        return $this;
    }

    /**
     * Return details of the IP address lookup.
     *
     * @return array
     */
    public function getDetails()
    {
        return [
            'city'         => $this->city,
            'region'       => $this->region,
            'zipcode'      => $this->zipcode,
            'country'      => $this->country,
            'latitude'     => $this->latitude,
            'longitude'    => $this->longitude,
            'isp'          => $this->isp,
            'organization' => $this->organization,
            'timezone'     => $this->timezone,
            'extra'        => $this->extra,
        ];
    }

    protected function shouldPerformLookup(): bool
    {
        return true;
    }
}