Completed
Push — master ( bbe9d4...e82ffc )
by De Cramer
10s
created

Countries::getCodeFromCountry()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
crap 2
1
<?php
2
3
namespace eXpansion\Framework\Core\Helpers;
4
5
use League\ISO3166\Exception\OutOfBoundsException;
6
use League\ISO3166\ISO3166;
7
use Psr\Log\LoggerInterface;
8
9
class Countries
10
{
11
    /** @var LoggerInterface */
12
    protected $logger;
13
14
    protected $iso;
15
16
    /** @var array  */
17
    protected $countriesMapping = [];
18
19
    /** @var string */
20
    protected $otherCountryCode;
21
22
    /** @var string */
23
    protected $otherCountryLabel;
24
25
    /**
26
     * Countries constructor.
27
     *
28
     * @param LoggerInterface $logger
29
     * @param array           $countriesMapping
30
     * @param string          $otherCountryCode
31
     * @param string          $otherCountryLabel
32
     */
33 151
    public function __construct(
34
        LoggerInterface $logger,
35
        array $countriesMapping,
36
        string $otherCountryCode,
37
        string $otherCountryLabel
38
    ) {
39 151
        $this->logger = $logger;
40 151
        $this->countriesMapping = $countriesMapping;
41 151
        $this->otherCountryCode = $otherCountryCode;
42 151
        $this->otherCountryLabel = $otherCountryLabel;
43
44 151
        $this->iso = new ISO3166();
45 151
        $contries = $this->iso->all();
46 151
        $contries[] = [
47
            'name' => 'Chinese Taipei',
48
            'alpha2' => 'CT',
49
            'alpha3' => 'TPE',
50
        ];
51
52 151
        $this->iso = new ISO3166($contries);
53 151
    }
54
55
56
    /**
57
     * Get 3-letter country code from full country name
58
     *
59
     * @param $country
60
     * @return mixed|string
61
     */
62 1
    public function getCodeFromCountry($country)
63
    {
64 1
        $output = 'OTH';
65 1
        if (array_key_exists($country, $this->countriesMapping)) {
66 1
            $output = $this->countriesMapping[$country];
67
        }
68
69 1
        return $output;
70
    }
71
72
    /**
73
     * Get full country name from 3-letter country code
74
     *
75
     * @param string $code
76
     * @return string
77
     */
78
    public function getCountryFromCode($code)
79
    {
80
        $code = strtoupper($code);
81
        $output = "Other";
82
        if (in_array($code, $this->countriesMapping)) {
83
            foreach ($this->countriesMapping as $country => $short) {
84
                if ($code == $short) {
85
                    $output = $country;
86
                    break;
87
                }
88
            }
89
        }
90
91
        return $output;
92
    }
93
94
    /**
95
     * Parses country from maniaplanet player objects' path
96
     *
97
     * @param string $path Maniaplanet path from player object
98
     * @return string long country name
99
     */
100
    public function parseCountryFromPath($path)
101
    {
102
        $parts = explode("|", $path);
103
        if (count($parts) >= 2) {
104
            return $parts[2];
105
        }
106
107
        return "Other";
108
    }
109
110
    /**
111
     * Get iso2 code from Maniaplanet Country name
112
     *
113
     * @param $name
114
     *
115
     * @return string
116
     */
117 1
    public function getIsoAlpha2FromName($name)
118
    {
119
        // First try and fetch from alpha3 code.
120
        try {
121 1
            return $this->iso->alpha3($this->getCodeFromCountry($name))['alpha2'];
122 1
        } catch (OutOfBoundsException $e) {
123
            // Nothing code continues.
124
        }
125
126
        // Couldn't getch alpha3 from code try from country name.
127
        try {
128 1
            return $this->iso->name($name)['alpha2'];
129
        } catch (OutOfBoundsException $e) {
130
            $this->logger->warning("Can't get valid alpha2 code for country '$name'");
131
        }
132
133
        return "OT";
134
    }
135
}
136