AmoCrmManager::getClient()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 10
cts 10
cp 1
rs 9.8666
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 2
1
<?php
2
3
/*
4
 * This file is part of Laravel AmoCrm.
5
 *
6
 * (c) dotzero <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Dotzero\LaravelAmoCrm;
13
14
use AmoCRM\Client;
15
use AmoCRM\Helpers\Fields;
16
use AmoCRM\Helpers\B2BFamily;
17
use Illuminate\Contracts\Config\Repository;
18
19
/**
20
 * This is the AmoCrm manager class.
21
 *
22
 * @package Dotzero\LaravelAmoCrm
23
 * @author dotzero <[email protected]>
24
 * @link http://www.dotzero.ru/
25
 * @link https://github.com/dotzero/laravel-amocrm
26
 * @property \AmoCRM\Models\Account $account
27
 * @property \AmoCRM\Models\Call $call
28
 * @property \AmoCRM\Models\Catalog $catalog
29
 * @property \AmoCRM\Models\CatalogElement $catalog_element
30
 * @property \AmoCRM\Models\Company $company
31
 * @property \AmoCRM\Models\Contact $contact
32
 * @property \AmoCRM\Models\Customer $customer
33
 * @property \AmoCRM\Models\CustomersPeriods $customers_periods
34
 * @property \AmoCRM\Models\CustomField $custom_field
35
 * @property \AmoCRM\Models\Lead $lead
36
 * @property \AmoCRM\Models\Links $links
37
 * @property \AmoCRM\Models\Note $note
38
 * @property \AmoCRM\Models\Pipelines $pipelines
39
 * @property \AmoCRM\Models\Task $task
40
 * @property \AmoCRM\Models\Transaction $transaction
41
 * @property \AmoCRM\Models\Unsorted $unsorted
42
 * @property \AmoCRM\Models\Webhooks $webhooks
43
 * @property \AmoCRM\Models\Widgets $widgets
44
 */
45
class AmoCrmManager
46
{
47
    /**
48
     * The config instance.
49
     *
50
     * @var \Illuminate\Contracts\Config\Repository
51
     */
52
    protected $config;
53
54
    /**
55
     * The AmoCRM client instance.
56
     *
57
     * @var \AmoCRM\Client
58
     */
59
    protected $client;
60
61
    /**
62
     * Create a new manager instance.
63
     *
64
     * @param \Illuminate\Contracts\Config\Repository $config
65
     */
66 23
    public function __construct(Repository $config)
67
    {
68 23
        $this->config = $config;
69 23
    }
70
71
    /**
72
     * Get the config instance.
73
     *
74
     * @return \Illuminate\Contracts\Config\Repository
75
     */
76 20
    public function getConfig()
77
    {
78 20
        return $this->config;
79
    }
80
81
    /**
82
     * Get the AmoCRM client instance.
83
     *
84
     * @return \AmoCRM\Client
85
     */
86 20
    public function getClient()
87
    {
88 20
        if (!$this->client instanceof Client) {
89 20
            $this->client = new Client(
90 20
                $this->config->get('amocrm.domain'),
91 20
                $this->config->get('amocrm.login'),
92 20
                $this->config->get('amocrm.hash')
93 20
            );
94 20
        }
95
96 20
        return $this->client;
97 1
    }
98
99
    /**
100
     * Get the AmoCRM Fields helper instance.
101
     *
102
     * @return \AmoCRM\Helpers\Fields
103
     */
104 1
    public function getFields()
105
    {
106 1
        return new Fields();
107
    }
108
109
    /**
110
     * Get the AmoCRM B2BFamily helper instance.
111
     *
112
     * @return \AmoCRM\Helpers\B2BFamily
113
     */
114 1
    public function getB2BFamily()
115
    {
116 1
        $client = $this->getClient();
117
118 1
        return new B2BFamily($client,
119 1
            $this->config->get('amocrm.b2bfamily.appkey'),
120 1
            $this->config->get('amocrm.b2bfamily.secret'),
121 1
            $this->config->get('amocrm.b2bfamily.email'),
122 1
            $this->config->get('amocrm.b2bfamily.password')
123 1
        );
124
    }
125
126
    /**
127
     * Dynamically pass methods to AmoCRM client instance.
128
     *
129
     * @param string $property
130
     * @return \AmoCRM\Models\ModelInterface
131
     */
132 18
    public function __get($property)
133
    {
134 18
        return call_user_func_array([$this->getClient(), '__get'], [$property]);
135
    }
136
}
137