Completed
Pull Request — master (#278)
by De Cramer
05:16
created

Countries   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 20.83%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 0
dl 0
loc 80
ccs 5
cts 24
cp 0.2083
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getCodeFromCountry() 0 9 2
A getCountryFromCode() 0 15 4
A parseCountryFromPath() 0 9 2
A __construct() 0 6 1
1
<?php
2
3
namespace eXpansion\Framework\Core\Helpers;
4
5
class Countries
6
{
7
    /** @var array  */
8
    protected $countriesMapping = [];
9
10
    /** @var string */
11
    protected $otherCountryCode;
12
13
    /** @var string */
14
    protected $otherCountryLabel;
15
16
    /**
17
     * Countries constructor.
18
     *
19
     * @param array $countriesMapping
20
     * @param string $otherCountryCode
21
     * @param string $otherCountryLabel
22
     */
23 141
    public function __construct(array $countriesMapping, string $otherCountryCode, string $otherCountryLabel)
24
    {
25 141
        $this->countriesMapping = $countriesMapping;
26 141
        $this->otherCountryCode = $otherCountryCode;
27 141
        $this->otherCountryLabel = $otherCountryLabel;
28 141
    }
29
30
31
    /**
32
     * Get 3-letter country code from full country name
33
     *
34
     * @param $country
35
     * @return mixed|string
36
     */
37
    public function getCodeFromCountry($country)
38
    {
39
        $output = 'OTH';
40
        if (array_key_exists($country, $this->countriesMapping)) {
41
            $output = $this->countriesMapping[$country];
42
        }
43
44
        return $output;
45
    }
46
47
    /**
48
     * Get full country name from 3-letter country code
49
     *
50
     * @param string $code
51
     * @return string
52
     */
53
    public function getCountryFromCode($code)
54
    {
55
        $code = strtoupper($code);
56
        $output = "Other";
57
        if (in_array($code, $this->countriesMapping)) {
58
            foreach ($this->countriesMapping as $country => $short) {
59
                if ($code == $short) {
60
                    $output = $country;
61
                    break;
62
                }
63
            }
64
        }
65
66
        return $output;
67
    }
68
69
    /**
70
     * Parses country from maniaplanet player objects' path
71
     *
72
     * @param string $path Maniaplanet path from player object
73
     * @return string long country name
74
     */
75
    public function parseCountryFromPath($path)
76
    {
77
        $parts = explode("|", $path);
78
        if (count($parts) >= 2) {
79
            return $parts[2];
80
        }
81
82
        return "Other";
83
    }
84
}
85