Completed
Pull Request — master (#4)
by Konstantin
01:40
created

AccountService::getUsers()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 1
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 1
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 0
crap 2
1
<?php
2
3
namespace linkprofit\AmoCRM\services;
4
5
use linkprofit\AmoCRM\entities\Account;
6
use linkprofit\AmoCRM\entities\TaskType;
7
use linkprofit\AmoCRM\RequestHandler;
8
9
/**
10
 * Class AccountService
11
 * @package linkprofit\AmoCRM\services
12
 */
13
class AccountService implements AccountServiceInterface
14
{
15
    /**
16
     * @var \linkprofit\AmoCRM\RequestHandler
17
     */
18
    protected $request;
19
20
    /**
21
     * ServiceInterface constructor.
22
     *
23
     * @param RequestHandler $requestHandler
24
     */
25 3
    public function __construct(RequestHandler $requestHandler)
26 1
    {
27 3
        $this->request = $requestHandler;
28 3
    }
29
30
    /**
31
     * @return bool|array
32
     */
33 1
    public function getAllArray()
34
    {
35 1
        $this->send();
36
37 1
        return $this->request->getResponse();
38
    }
39
40
    /**
41
     * @return array|bool|\linkprofit\AmoCRM\entities\Account
42
     */
43 1
    public function getAccount()
44
    {
45 1
        $this->send([]);
46
47 1
        return $this->parseArrayToAccountEntity($this->request->getResponse());
48
    }
49
50
    /**
51
     * TODO
52
     *
53
     * @return \linkprofit\AmoCRM\entities\CustomField[]
54
     */
55
    public function getCustomFields() {}
56
57
    /**
58
     * TODO
59
     *
60
     * @return \linkprofit\AmoCRM\entities\User[]
61
     */
62
    public function getUsers() {}
63
64
    /**
65
     * TODO
66
     *
67
     * @return \linkprofit\AmoCRM\entities\Pipeline[]
68
     */
69
    public function getPipelines() {}
70
71
    /**
72
     * TODO
73
     *
74
     * @return \linkprofit\AmoCRM\entities\Group[]
75
     */
76
    public function getGroups() {}
77
78
    /**
79
     * TODO
80
     *
81
     * @return \linkprofit\AmoCRM\entities\NoteTypes[]
82
     */
83
    public function getNoteTypes() {}
84
85
    /**
86
     * @return \linkprofit\AmoCRM\entities\TaskType[]
87
     */
88 1
    public function getTaskTypes()
89
    {
90 1
        $this->send(['with' => 'task_types']);
91
92 1
        return $this->parseArrayToTaskTypeEntities($this->request->getResponse()['_embedded']['task_types']);
93
    }
94
95
    /**
96
     * @return string
97
     */
98 3
    protected function getLink()
99
    {
100 3
        return 'https://' . $this->request->getSubdomain() . '.amocrm.ru/api/v2/account';
101
    }
102
103
    /**
104
     * @param array $with
105
     */
106 3
    private function send($with = ['with' => 'custom_fields,users,pipelines,groups,note_types,task_types'])
107
    {
108 3
        $link = $this->getLink() . '?' . http_build_query($with);
109 3
        $this->request->performRequest($link, [], 'application/json', 'GET');
110 3
    }
111
112
    /**
113
     * @param $array
114
     *
115
     * @return Account
116
     */
117 1
    private function parseArrayToAccountEntity($array)
118
    {
119 1
        $account = new Account();
120 1
        $account->set($array);
121
122 1
        return $account;
123
    }
124
125
    /**
126
     * @param array $array
127
     *
128
     * @return array
129
     */
130 1
    public function parseArrayToTaskTypeEntities(array $array)
131
    {
132 1
        $entities = [];
133
134 1
        foreach ($array as $item) {
135 1
            $entity = new TaskType();
136 1
            $entity->set($item);
137 1
            $entities[] = $entity;
138 1
        }
139
140 1
        return $entities;
141
    }
142
}