Completed
Pull Request — dev (#10)
by Konstantin
04:59
created

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