Completed
Push — dev ( 4a164e...157791 )
by Konstantin
02:03
created

CatalogElementService::setQuery()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
c 0
b 0
f 0
ccs 3
cts 3
cp 1
rs 10
cc 1
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
use linkprofit\AmoCRM\traits\TermList;
10
11
/**
12
 * Class CatalogElementService
13
 *
14
 * @package linkprofit\AmoCRM\services
15
 */
16
class CatalogElementService extends BaseService
17
{
18
    use TermList;
19
20
    /**
21
     * @var CatalogElement[]
22
     */
23
    protected $entities = [];
24
25
    /**
26
     * @var int
27
     */
28
    protected $listPage = 1;
29
30
    /**
31
     * @var array
32
     */
33
    protected $listParams = [];
34
35
    /**
36
     * @param EntityInterface|CatalogElement $catalogElement
37
     */
38 11
    public function add(EntityInterface $catalogElement)
39
    {
40 11
        if ($catalogElement instanceof CatalogElement) {
41 11
            $this->entities[] = $catalogElement;
42
        }
43 11
    }
44
45
    /**
46
     * @param int $page
47
     *
48
     * @return $this
49
     */
50 2
    public function setPage($page)
51
    {
52 2
        $this->listPage = $page;
53
54 2
        return $this;
55
    }
56
57
    /**
58
     * @param array $params
59
     *
60
     * @return $this
61
     */
62 1
    public function setParams(array $params)
63
    {
64 1
        $this->listParams = $params;
65
66 1
        return $this;
67
    }
68
69
    /**
70
     * @param $link
71
     *
72
     * @return string
73
     */
74 7
    protected function composeListLink($link)
75
    {
76 7
        $query = [];
77
78 7
        $query['PAGEN_1'] = $this->listPage;
79
80 7
        if (!empty($this->listTerm)) {
81 4
            $query['term'] = $this->listTerm;
82
        }
83
84 7
        $query = array_merge($query, $this->listParams);
85
86 7
        $link .= '?' . http_build_query($query);
87
88 7
        return $link;
89
    }
90
91
    /**
92
     * @deprecated
93
     *
94
     * @param int $page
95
     * @param string|null $term
96
     * @param array $params
97
     *
98
     * @return array|bool
99
     */
100 4
    public function lists($page = 1, $term = null, array $params = [])
101
    {
102 4
        $this->listPage = $page;
103 4
        $this->listTerm = $term;
0 ignored issues
show
Documentation Bug introduced by
It seems like $term can also be of type string. However, the property $listTerm is declared as type integer. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
104 4
        $this->listParams = $params;
105
106 4
        return $this->getList();
107
    }
108
109
    /**
110
     * @param $array
111
     *
112
     * @return CatalogElement
113
     */
114 11
    public function parseArrayToEntity($array)
115
    {
116 11
        $element = new CatalogElement();
117 11
        $element->set($array);
118
119 11
        if (isset($array['custom_fields'])) {
120 7
            foreach ($array['custom_fields'] as $customFieldArray) {
121 7
                $customField = new CustomField($customFieldArray['id']);
122 7
                $customField->set($customFieldArray);
123
124 7
                if (isset($customFieldArray['values'])) {
125 7
                    foreach ($customFieldArray['values'] as $value) {
126 7
                        $value = new Value($value);
127 7
                        $customField->addValue($value);
128
                    }
129
                }
130
131 7
                $element->addCustomField($customField);
132
            }
133
        }
134
135 11
        return $element;
136
    }
137
138
    /**
139
     * @return string
140
     */
141 11
    protected function getLink()
142
    {
143 11
        return 'https://' . $this->request->getSubdomain() . '.amocrm.ru/api/v2/catalog_elements';
144
    }
145
146
}