Completed
Pull Request — dev (#10)
by Konstantin
01:48
created

CatalogElementService::setPage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
3
namespace linkprofit\AmoCRM\services;
4
5
use linkprofit\AmoCRM\entities\CatalogElement;
6
use linkprofit\AmoCRM\entities\CustomField;
7
use linkprofit\AmoCRM\entities\EntityInterface;
8
use linkprofit\AmoCRM\entities\Value;
9
10
/**
11
 * Class CatalogElementService
12
 *
13
 * @package linkprofit\AmoCRM\services
14
 */
15
class CatalogElementService extends BaseService
16
{
17
    /**
18
     * @var CatalogElement[]
19
     */
20
    protected $entities = [];
21
22
    /**
23
     * @var int
24
     */
25
    protected $listPage = 1;
26
27
    /**
28
     * @var string
29
     */
30
    protected $listQuery;
31
32
    /**
33
     * @var array
34
     */
35
    protected $listParams = [];
36
37
    /**
38
     * @param EntityInterface|CatalogElement $catalogElement
39
     */
40 11
    public function add(EntityInterface $catalogElement)
41
    {
42 11
        if ($catalogElement instanceof CatalogElement) {
43 11
            $this->entities[] = $catalogElement;
44
        }
45 11
    }
46
47
    /**
48
     * @param int $page
49
     *
50
     * @return $this
51
     */
52 2
    public function setPage($page)
53
    {
54 2
        $this->listPage = $page;
55
56 2
        return $this;
57
    }
58
59
    /**
60
     * @param string $query
61
     *
62
     * @return $this
63
     */
64 2
    public function setQuery($query)
65
    {
66 2
        $this->listQuery = $query;
67
68 2
        return $this;
69
    }
70
71
    /**
72
     * @param array $params
73
     *
74
     * @return $this
75
     */
76 1
    public function setParams(array $params)
77
    {
78 1
        $this->listParams = $params;
79
80 1
        return $this;
81
    }
82
83
    /**
84
     * @param $link
85
     *
86
     * @return string
87
     */
88 7
    protected function composeListLink($link)
89
    {
90 7
        $query['PAGEN_1'] = $this->listPage;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$query was never initialized. Although not strictly required by PHP, it is generally a good practice to add $query = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
91
92 7
        if (!empty($this->listQuery)) {
93 4
            $query['term'] = $this->listQuery;
94
        }
95
96 7
        $query = array_merge($query, $this->listParams);
97
98 7
        $link .= '?' . http_build_query($query);
99
100 7
        return $link;
101
    }
102
103
    /**
104
     * @deprecated
105
     *
106
     * @param int $page
107
     * @param string|null $query
108
     * @param array $params
109
     *
110
     * @return array|bool
111
     */
112 4
    public function lists($page = 1, $query = null, array $params = [])
113
    {
114 4
        $this->listPage = $page;
115 4
        $this->listQuery = $query;
116 4
        $this->listParams = $params;
117
118 4
        return $this->getList();
119
    }
120
121
    /**
122
     * @param $array
123
     *
124
     * @return CatalogElement
125
     */
126 11
    public function parseArrayToEntity($array)
127
    {
128 11
        $element = new CatalogElement();
129 11
        $element->set($array);
130
131 11
        if (isset($array['custom_fields'])) {
132 7
            foreach ($array['custom_fields'] as $customFieldArray) {
133 7
                $customField = new CustomField($customFieldArray['id']);
134 7
                $customField->set($customFieldArray);
135
136 7
                if (isset($customFieldArray['values'])) {
137 7
                    foreach ($customFieldArray['values'] as $value) {
138 7
                        $value = new Value($value);
139 7
                        $customField->addValue($value);
140
                    }
141
                }
142
143 7
                $element->addCustomField($customField);
144
            }
145
        }
146
147 11
        return $element;
148
    }
149
150
    /**
151
     * @return string
152
     */
153 11
    protected function getLink()
154
    {
155 11
        return 'https://' . $this->request->getSubdomain() . '.amocrm.ru/api/v2/catalog_elements';
156
    }
157
158
}