Base::setApiRequest()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php namespace GameScan\WoW\Entity;
2
3
use GameScan\WoW\EntityInformation;
4
use GameScan\WoW\Exceptions\EntityInformationDecodeException;
5
use GameScan\WoW\WowApiRequest;
6
7
abstract class Base
8
{
9
10
    /**
11
     * @type WowApiRequest
12
     */
13
    protected $apiRequest = null;
14
    protected $entityInformation = null;
15
    protected $parameters = null;
16
17
18 2 View Code Duplication
    public function __construct(WowApiRequest $api, $locale = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
19
    {
20 2
        $this->apiRequest = $api;
21 2
        if ($locale !== null) {
22
            $this->apiRequest->setLocale($locale);
23
        }
24 2
        $this->loadInformation();
25 2
    }
26
27
    public function setApiRequest(WowApiRequest $apiRequest)
28
    {
29
        $this->apiRequest = $apiRequest;
30
    }
31
32
    abstract public function getRessource();
33
34 2
    public function getParametters()
35
    {
36 2
        return $this->parameters;
37
    }
38
39 2
    public function loadInformation()
40
    {
41 2
        $this->entityInformation = $this->apiRequest->get($this->getRessource(), $this->getParametters());
42 2
    }
43
44 2
    protected function getEntityInformations()
45
    {
46 2
        if ($this->entityInformation === null) {
47
            $this->loadInformation();
48
        }
49 2
        $entityInformation = json_decode($this->entityInformation);
50 2
        if (JSON_ERROR_NONE !== json_last_error()) {
51
            throw new EntityInformationDecodeException(json_last_error_msg());
52
        }
53 2
        return (new EntityInformation($entityInformation));
54
    }
55
56
    public function __toString()
57
    {
58
        return $this->entityInformation;
59
    }
60
61
    public function retreiveField($fieldName)
62
    {
63
        if (!$this->checkIfFieldExist($fieldName)) {
64
            $this->addField($fieldName);
65
            $this->loadInformation();
66
        }
67
    }
68
69
    protected function checkIfFieldExist($fieldName)
70
    {
71
        if ($this->parameters === null || empty($this->parameters['fields'])) {
72
            return false;
73
        }
74
        if (strpos($this->parameters['fields'], $fieldName) !== false) {
75
            return true;
76
        }
77
        return false;
78
    }
79
80
    protected function addField($fieldName)
81
    {
82
        $fields = explode(',', $this->parameters['fields']);
83
        if ($fields === false) {
84
            $fields = array();
85
        }
86
        $fields[] = $fieldName;
87
        $fields = array_unique($fields);
88
        $this->parameters['fields'] = implode(",", $fields);
89
    }
90
91
    protected function getHost()
92
    {
93
        return $this->apiRequest->getHost();
94
    }
95
96
    protected function getRegion()
97
    {
98
        return $this->apiRequest->getRegion();
99
    }
100
101
    protected function getApiRequest()
102
    {
103
        return $this->apiRequest;
104
    }
105
}
106