Issues (3627)

app/bundles/CoreBundle/Factory/IpLookupFactory.php (1 issue)

1
<?php
2
3
/*
4
 * @copyright   2015 Mautic Contributors. All rights reserved
5
 * @author      Mautic
6
 *
7
 * @link        http://mautic.org
8
 *
9
 * @license     GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
10
 */
11
12
namespace Mautic\CoreBundle\Factory;
13
14
use Joomla\Http\Http;
15
use Mautic\CoreBundle\IpLookup\AbstractLookup;
16
use Psr\Log\LoggerInterface;
17
18
class IpLookupFactory
19
{
20
    /**
21
     * @var LoggerInterface
22
     */
23
    protected $logger;
24
25
    /**
26
     * @var string
27
     */
28
    protected $cacheDir;
29
30
    /**
31
     * @var array
32
     */
33
    protected $lookupServices;
34
35
    /**
36
     * @var Http|null
37
     */
38
    protected $httpConnector;
39
40
    /**
41
     * IpLookupFactory constructor.
42
     *
43
     * @param null $cacheDir
44
     */
45
    public function __construct(array $lookupServices, LoggerInterface $logger = null, Http $httpConnector = null, $cacheDir = null)
46
    {
47
        $this->lookupServices = $lookupServices;
48
        $this->logger         = $logger;
49
        $this->cacheDir       = $cacheDir;
50
        $this->httpConnector  = $httpConnector;
51
    }
52
53
    /**
54
     * @param      $service
55
     * @param null $auth
56
     *
57
     * @return AbstractLookup|null
58
     */
59
    public function getService($service, $auth = null, array $ipLookupConfig = [])
60
    {
61
        static $services = [];
62
63
        if (empty($service)) {
64
            return null;
65
        }
66
67
        if (!isset($services[$service]) || (null !== $auth || null !== $ipLookupConfig)) {
0 ignored issues
show
The condition null !== $ipLookupConfig is always true.
Loading history...
68
            if (!isset($this->lookupServices[$service])) {
69
                throw new \InvalidArgumentException($service.' not registered.');
70
            }
71
72
            $className = $this->lookupServices[$service]['class'];
73
            if ('\\' !== substr($className, 0, 1)) {
74
                $className = '\\'.$className;
75
            }
76
77
            $services[$service] = new $className(
78
                $auth,
79
                $ipLookupConfig,
80
                $this->cacheDir,
81
                $this->logger,
82
                $this->httpConnector
83
            );
84
        }
85
86
        return $services[$service];
87
    }
88
}
89