Completed
Push — dev ( 619916...3e2483 )
by Konstantin
9s
created

AccountService::parseArrayToCatalogFieldEntities()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 8
ccs 6
cts 6
cp 1
rs 9.4285
cc 2
eloc 5
nc 2
nop 1
crap 2
1
<?php
2
3
namespace linkprofit\AmoCRM\services;
4
5
use linkprofit\AmoCRM\entities\Account;
6
use linkprofit\AmoCRM\entities\Field;
7
use linkprofit\AmoCRM\entities\TaskType;
8
use linkprofit\AmoCRM\RequestHandler;
9
10
/**
11
 * Class AccountService
12
 * @package linkprofit\AmoCRM\services
13
 */
14
class AccountService implements AccountServiceInterface
15
{
16
    /**
17
     * @var \linkprofit\AmoCRM\RequestHandler
18
     */
19
    protected $request;
20
21
    /**
22
     * ServiceInterface constructor.
23
     *
24
     * @param RequestHandler $requestHandler
25
     */
26 4
    public function __construct(RequestHandler $requestHandler)
27
    {
28 4
        $this->request = $requestHandler;
29 4
    }
30
31
    /**
32
     * @return bool|array
33
     */
34 1
    public function getAllArray()
35
    {
36 1
        $this->send();
37
38 1
        return $this->request->getResponse();
39
    }
40
41
    /**
42
     * @return array|bool|\linkprofit\AmoCRM\entities\Account
43
     */
44 1
    public function getAccount()
45
    {
46 1
        $this->send([]);
47
48 1
        return $this->parseArrayToAccountEntity($this->request->getResponse());
49
    }
50
51
    /**
52
     * TODO
53
     *
54
     * @return \linkprofit\AmoCRM\entities\Field[]
55
     */
56 1
    public function getCustomFields()
57
    {
58 1
        $this->send(['with' => 'custom_fields']);
59
60 1
        return $this->parseCustomFieldsArrayToFieldEntities($this->request->getResponse()['_embedded']['custom_fields']);
61
    }
62
63
    /**
64
     * TODO
65
     *
66
     * @return \linkprofit\AmoCRM\entities\User[]
67
     */
68
    public function getUsers() {}
69
70
    /**
71
     * TODO
72
     *
73
     * @return \linkprofit\AmoCRM\entities\Pipeline[]
74
     */
75
    public function getPipelines() {}
76
77
    /**
78
     * TODO
79
     *
80
     * @return \linkprofit\AmoCRM\entities\Group[]
81
     */
82
    public function getGroups() {}
83
84
    /**
85
     * TODO
86
     *
87
     * @return \linkprofit\AmoCRM\entities\NoteTypes[]
88
     */
89
    public function getNoteTypes() {}
90
91
    /**
92
     * @return \linkprofit\AmoCRM\entities\TaskType[]
93
     */
94 1
    public function getTaskTypes()
95
    {
96 1
        $this->send(['with' => 'task_types']);
97
98 1
        return $this->parseArrayToTaskTypeEntities($this->request->getResponse()['_embedded']['task_types']);
99
    }
100
101
    /**
102
     * @return string
103
     */
104 4
    protected function getLink()
105
    {
106 4
        return 'https://' . $this->request->getSubdomain() . '.amocrm.ru/api/v2/account';
107
    }
108
109
    /**
110
     * @param array $with
111
     */
112 4
    private function send($with = ['with' => 'custom_fields,users,pipelines,groups,note_types,task_types'])
113
    {
114 4
        $link = $this->getLink() . '?' . http_build_query($with);
115 4
        $this->request->performRequest($link, [], 'application/json', 'GET');
116 4
    }
117
118
    /**
119
     * @param $array
120
     *
121
     * @return Account
122
     */
123 1
    private function parseArrayToAccountEntity($array)
124
    {
125 1
        $account = new Account();
126 1
        $account->set($array);
127
128 1
        return $account;
129
    }
130
131
    /**
132
     * @param array $array
133
     *
134
     * @return array
135
     */
136 1
    public function parseArrayToTaskTypeEntities(array $array)
137
    {
138 1
        $entities = [];
139
140 1
        foreach ($array as $item) {
141 1
            $entity = new TaskType();
142 1
            $entity->set($item);
143 1
            $entities[] = $entity;
144 1
        }
145
146 1
        return $entities;
147
    }
148
149
    /**
150
     * @param array $array
151
     *
152
     * @return array
153
     */
154 1
    private function parseCustomFieldsArrayToFieldEntities(array $array)
155
    {
156 1
        $entities = [];
157 1
        foreach ($array as $elementTypeKey => $items) {
158
            switch ($elementTypeKey) {
159 1 View Code Duplication
                case 'contacts':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
160 1
                    $elementType = Field::CONTACT_ELEMENT_TYPE;
161 1
                    $entities = array_merge($entities, $this->parseArrayToFieldEntities($items, $elementType));
162 1
                    break;
163 1 View Code Duplication
                case 'leads':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
164 1
                    $elementType = Field::LEAD_ELEMENT_TYPE;
165 1
                    $entities = array_merge($entities, $this->parseArrayToFieldEntities($items, $elementType));
166 1
                    break;
167 1 View Code Duplication
                case 'companies':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
168 1
                    $elementType = Field::COMPANY_ELEMENT_TYPE;
169 1
                    $entities = array_merge($entities, $this->parseArrayToFieldEntities($items, $elementType));
170 1
                    break;
171 1 View Code Duplication
                case 'customers':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
172 1
                    $elementType = Field::CUSTOMER_ELEMENT_TYPE;
173 1
                    $entities = array_merge($entities, $this->parseArrayToFieldEntities($items, $elementType));
174 1
                    break;
175 1
                case 'catalogs':
176 1
                    $entities = array_merge($entities, $this->parseArrayToCatalogFieldEntities($items));
177 1
                    break;
178
            }
179 1
        }
180
181 1
        return $entities;
182
    }
183
184
    /**
185
     * @param array $items
186
     * @param int   $elementType
187
     *
188
     * @return array
189
     */
190 1
    private function parseArrayToFieldEntities(array $items, $elementType)
191
    {
192 1
        $entities = [];
193 1
        foreach ($items as $item) {
194 1
            $entities[] = $this->parseArrayToFieldEntity($item, $elementType);
195 1
        }
196
197 1
        return $entities;
198
    }
199
200
    /**
201
     * @param array $item
202
     * @param       $elementType
203
     *
204
     * @return \linkprofit\AmoCRM\entities\Field
205
     */
206 1
    private function parseArrayToFieldEntity(array $item, $elementType)
207
    {
208 1
        $entity = new Field();
209 1
        $entity->set($item);
210 1
        $entity->element_type = $elementType;
211
212 1
        return $entity;
213
    }
214
215
    /**
216
     * @param array $items
217
     *
218
     * @return array
219
     */
220 1
    private function parseArrayToCatalogFieldEntities(array $items)
221
    {
222 1
        $entities = [];
223 1
        foreach ($items as $elementType => $catalogItems) {
224 1
            $entities = array_merge($entities, $this->parseArrayToFieldEntities($catalogItems, $elementType));
225 1
        }
226 1
        return $entities;
227
    }
228
}