Completed
Push — master ( 7cb621...ebe87d )
by dotzero
11s
created

Client   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 90.91%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 6
c 2
b 0
f 0
lcom 0
cbo 4
dl 0
loc 78
ccs 20
cts 22
cp 0.9091
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 20 3
A __get() 0 13 2
A toCamelCase() 0 4 1
1
<?php
2
3
namespace AmoCRM;
4
5
use AmoCRM\Models\ModelInterface;
6
use AmoCRM\Request\CurlHandle;
7
use AmoCRM\Request\ParamsBag;
8
use AmoCRM\Helpers\Fields;
9
10
/**
11
 * Class Client
12
 *
13
 * Основной класс для получения доступа к моделям amoCRM API
14
 *
15
 * @package AmoCRM
16
 * @author dotzero <[email protected]>
17
 * @link http://www.dotzero.ru/
18
 * @link https://github.com/dotzero/amocrm-php
19
 * @property \AmoCRM\Models\Account $account
20
 * @property \AmoCRM\Models\Call $call
21
 * @property \AmoCRM\Models\Catalog $catalog
22
 * @property \AmoCRM\Models\CatalogElement $catalog_element
23
 * @property \AmoCRM\Models\Company $company
24
 * @property \AmoCRM\Models\Contact $contact
25
 * @property \AmoCRM\Models\Customer $customer
26
 * @property \AmoCRM\Models\CustomersPeriods $customers_periods
27
 * @property \AmoCRM\Models\CustomField $custom_field
28
 * @property \AmoCRM\Models\Lead $lead
29
 * @property \AmoCRM\Models\Links $links
30
 * @property \AmoCRM\Models\Note $note
31
 * @property \AmoCRM\Models\Pipelines $pipelines
32
 * @property \AmoCRM\Models\Task $task
33
 * @property \AmoCRM\Models\Transaction $transaction
34
 * @property \AmoCRM\Models\Unsorted $unsorted
35
 * @property \AmoCRM\Models\Webhooks $webhooks
36
 * @property \AmoCRM\Models\Widgets $widgets
37
 *
38
 * For the full copyright and license information, please view the LICENSE
39
 * file that was distributed with this source code.
40
 */
41
class Client
42
{
43
    /**
44
     * @var Fields|null Экземпляр Fields для хранения номеров полей
45
     */
46
    public $fields = null;
47
48
    /**
49
     * @var ParamsBag|null Экземпляр ParamsBag для хранения аргументов
50
     */
51
    public $parameters = null;
52
53
    /**
54
     * @var CurlHandle Экземпляр CurlHandle для повторного использования
55
     */
56
    private $curlHandle;
57
58
    /**
59
     * Client constructor
60
     *
61
     * @param string $domain Поддомен или домен amoCRM
62
     * @param string $login Логин amoCRM
63
     * @param string $apikey Ключ пользователя amoCRM
64
     * @param string|null $proxy Прокси сервер для отправки запроса
65
     */
66 27
    public function __construct($domain, $login, $apikey, $proxy = null)
67
    {
68
        // Разернуть поддомен в полный домен
69 27
        if (strpos($domain, '.') === false) {
70 23
            $domain = sprintf('%s.amocrm.ru', $domain);
71 23
        }
72
73 27
        $this->parameters = new ParamsBag();
74 27
        $this->parameters->addAuth('domain', $domain);
75 27
        $this->parameters->addAuth('login', $login);
76 27
        $this->parameters->addAuth('apikey', $apikey);
77
78 27
        if ($proxy !== null) {
79
            $this->parameters->addProxy($proxy);
80
        }
81
82 27
        $this->fields = new Fields();
83
84 27
        $this->curlHandle = new CurlHandle();
85 27
    }
86
87
    /**
88
     * Возвращает экземпляр модели для работы с amoCRM API
89
     *
90
     * @param string $name Название модели
91
     * @return ModelInterface
92
     * @throws ModelException
93
     */
94 19
    public function __get($name)
95
    {
96 19
        $classname = '\\AmoCRM\\Models\\' . $this->toCamelCase($name);
97
98 19
        if (!class_exists($classname)) {
99 1
            throw new ModelException('Model not exists: ' . $name);
100
        }
101
102
        // Чистим GET и POST от предыдущих вызовов
103 18
        $this->parameters->clearGet()->clearPost();
104
105 18
        return new $classname($this->parameters, $this->curlHandle);
106
    }
107
108
    /**
109
     * Приведение under_score к CamelCase
110
     *
111
     * @param string $string Строка
112
     * @return string Строка
113
     */
114 19
    private function toCamelCase($string)
115
    {
116 19
        return str_replace(' ', '', ucwords(str_replace('_', ' ', $string)));
117
    }
118
}
119