WowApiRequest::setLocale()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2.0932

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 5
cts 7
cp 0.7143
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 1
crap 2.0932
1
<?php namespace GameScan\WoW;
2
3
use GameScan\Core\Request\Api\GameApiRequest;
4
use GameScan\Core\Tools\LoggerFactory;
5
use GameScan\WoW\Exceptions\HostNotFoundException;
6
7
class WowApiRequest extends GameApiRequest
8
{
9
    /**
10
     * @type HostInformations
11
     */
12
    private $host = null;
13
    private $locale = null;
14
15 2
    public function setHost(HostInformations $hostInformations)
16
    {
17 2
        $this->host = $hostInformations;
18 2
    }
19
20 2
    public function getHost()
21
    {
22 2
        return $this->host->getHost();
23
    }
24
25
    public function getRegion()
26
    {
27
        return $this->host->getRegion();
28
    }
29
30 2
    public function setLocale($locale)
31
    {
32 2
        $this->checkHost();
33 2
        if ($this->isAvailableLocale($locale)) {
34
            $this->locale = $locale;
35
        } else {
36 2
            LoggerFactory::getLogger()->info("The locale $locale isn't available for the host " . $this->getHost());
37
        }
38 2
    }
39 2
    private function checkHost()
40
    {
41 2
        if ($this->host === null) {
42
            throw new HostNotFoundException;
43
        }
44 2
    }
45
46 2
    private function isAvailableLocale($locale)
47
    {
48 2
        return in_array($locale, $this->host->getAvailableLocales(), true);
49
    }
50
51
    public function get($ressourceToGrab, array $parameters = null)
52
    {
53
        $this->checkHost();
54
        $ressourceToGrab = $this->buildUrl($ressourceToGrab);
55
        $parameters = $this->addLocaleToParameters($parameters);
56
        return parent::get($ressourceToGrab, $parameters);
57
    }
58
59
    private function buildUrl($ressourceToGrab)
60
    {
61
        $ressourceToGrab = $this->getHost() . $ressourceToGrab;
62
        return $ressourceToGrab;
63
    }
64
65
    private function addLocaleToParameters($parameters)
66
    {
67
        if ($parameters === null || $this->locale === null) {
68
            return $parameters;
69
        }
70
        $parameters['locale'] = $this->locale;
71
        return $parameters;
72
    }
73
}
74