Completed
Push — master ( 2546e0...ad929d )
by Alexey
12:49 queued 02:29
created

GeoIpRemoteServices::getSortedServices()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 5
c 1
b 0
f 0
nc 3
nop 0
dl 0
loc 11
rs 10
1
<?php
2
3
namespace CodeblogPro\GeoLocation\Application\GeoIpRemoteServices;
4
5
class GeoIpRemoteServices
6
{
7
    private static $instance = null;
8
    private array $sortToServicesMap = [];
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_ARRAY, expecting T_FUNCTION or T_CONST
Loading history...
9
10
    private function __construct()
11
    {
12
        $this->initServicesMap();
13
        $this->sortServicesMap();
14
    }
15
16
    public static function getInstance()
17
    {
18
        if (is_null(self::$instance)) {
19
            self::$instance = new self();
20
        }
21
22
        return self::$instance;
23
    }
24
25
    public function getSortedToServicesMap(): array
26
    {
27
        return $this->sortToServicesMap;
28
    }
29
30
    public function getSortedServices(): array
31
    {
32
        $services = [];
33
34
        foreach ($this->sortToServicesMap as $remoteServiceSort => $remoteServices) {
35
            foreach ($remoteServices as $remoteService) {
36
                $services[] = $remoteService;
37
            }
38
        }
39
40
        return $services;
41
    }
42
43
    protected function __clone()
44
    {
45
    }
46
47
    private function initServicesMap()
48
    {
49
        $daDataOptions = new DaDataOptions();
50
51
        if ($daDataOptions->isEnabled()) {
52
            $daData = new DaData($daDataOptions);
53
            $this->sortToServicesMap[$daDataOptions->getSort()][] = $daData;
54
        }
55
56
        $sypexGeoOptions = new SypexGeoOptions();
57
58
        if ($sypexGeoOptions->isEnabled()) {
59
            $sypexGeo = new SypexGeo($sypexGeoOptions);
60
            $this->sortToServicesMap[$sypexGeoOptions->getSort()][] = $sypexGeo;
61
        }
62
63
        $maxMindOptions = new MaxMindOptions();
64
65
        if ($maxMindOptions->isEnabled()) {
66
            $maxMind = new MaxMind($maxMindOptions);
67
            $this->sortToServicesMap[$maxMindOptions->getSort()][] = $maxMind;
68
        }
69
    }
70
71
    private function sortServicesMap()
72
    {
73
        ksort($this->sortToServicesMap);
74
    }
75
}
76