Completed
Pull Request — dev (#3)
by
unknown
01:49
created

AccountService   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 134
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 74.36%

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 3
dl 0
loc 134
ccs 29
cts 39
cp 0.7436
rs 10
c 0
b 0
f 0

13 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getAllArray() 0 6 1
A getAccount() 0 6 1
A getCustomFields() 0 4 1
A getUsers() 0 4 1
A getPipelines() 0 4 1
A getGroups() 0 4 1
A getNoteTypes() 0 4 1
A getTaskTypes() 0 6 1
A getLink() 0 4 1
A send() 0 5 1
A parseArrayToAccountEntity() 0 7 1
A parseArrayToTaskTypeEntities() 0 12 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
    {
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
     * @return \linkprofit\AmoCRM\entities\CustomField[]
52
     */
53
    public function getCustomFields()
54
    {
55
        // TODO
56
    }
57
58
    /**
59
     * @return \linkprofit\AmoCRM\entities\User[]
60
     */
61
    public function getUsers()
62
    {
63
        // TODO
64
    }
65
66
    /**
67
     * @return \linkprofit\AmoCRM\entities\Pipeline[]
68
     */
69
    public function getPipelines()
70
    {
71
        // TODO
72
    }
73
74
    /**
75
     * @return \linkprofit\AmoCRM\entities\Group[]
76
     */
77
    public function getGroups()
78
    {
79
        // TODO
80
    }
81
82
    /**
83
     * @return \linkprofit\AmoCRM\entities\NoteTypes[]
84
     */
85
    public function getNoteTypes()
86
    {
87
        // TODO
88
    }
89
90
    /**
91
     * @return \linkprofit\AmoCRM\entities\TaskType[]
92
     */
93 1
    public function getTaskTypes()
94
    {
95 1
        $this->send(['with' => 'task_types']);
96
97 1
        return $this->parseArrayToTaskTypeEntities($this->request->getResponse()['_embedded']['task_types']);
98
    }
99
100
    /**
101
     * @return string
102
     */
103 3
    protected function getLink()
104
    {
105 3
        return 'https://' . $this->request->getSubdomain() . '.amocrm.ru/api/v2/account';
106
    }
107
108
    /**
109
     * @param array $with
110
     */
111 3
    private function send($with = ['with' => 'custom_fields,users,pipelines,groups,note_types,task_types'])
112
    {
113 3
        $link = $this->getLink().'?'.http_build_query($with);
114 3
        $this->request->performRequest($link, [], 'application/json', 'GET');
115 3
    }
116
117
    /**
118
     * @param $array
119
     * @return Account
120
     */
121 1
    private function parseArrayToAccountEntity($array)
122
    {
123 1
        $account = new Account();
124 1
        $account->set($array);
125
126 1
        return $account;
127
    }
128
129
    /**
130
     * @param array $array
131
     *
132
     * @return array
133
     */
134 1
    public function parseArrayToTaskTypeEntities(array $array)
135
    {
136 1
        $entities = [];
137
138 1
        foreach ($array as $item) {
139 1
            $entity = new TaskType();
140 1
            $entity->set($item);
141 1
            $entities[] = $entity;
142
        }
143
144 1
        return $entities;
145
    }
146
}