Passed
Push — develop ( eacc0b...67462a )
by Andrew
02:45
created

SettingsStorage   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 169
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 15
dl 0
loc 169
ccs 41
cts 41
cp 1
rs 10
c 0
b 0
f 0

13 Methods

Rating   Name   Duplication   Size   Complexity  
A setUserAgent() 0 4 1
A getMethodPath() 0 7 2
A setMethodsPaths() 0 4 1
A setCookiePath() 0 4 1
A getScheme() 0 3 1
A getDomain() 0 3 1
A getCookiePath() 0 3 1
A __construct() 0 3 1
A setScheme() 0 4 1
A getMethodsPaths() 0 3 1
A getUserAgent() 0 3 1
A setDomain() 0 4 1
A getMethodCodeByType() 0 7 2
1
<?php
2
3
4
namespace ddlzz\AmoAPI;
5
6
use ddlzz\AmoAPI\Exceptions\InvalidArgumentException;
7
use ddlzz\AmoAPI\Validators\SettingsValidator;
8
9
10
/**
11
 * Class SettingsStorage. All amo- and library related variables are stored here.
12
 * @package ddlzz\AmoAPI
13
 * @author ddlzz
14
 */
15
class SettingsStorage
16
{
17
    const LIB_PATH = __DIR__ . '/..';
18
19
    const NAMESPACE_PREFIX = '\ddlzz\AmoAPI';
20
21
    const SENDER_HTTP_HEADER = 'Content-Type: hal/json';
22
23
    /** @var array */
24
    private $methodsPaths = [
25
        'auth' => '/private/api/auth.php?type=json',
26
        'current' => '/api/v2/account?with=users,custom_fields',
27
        'leads' => '/api/v2/leads',
28
        'contacts' => '/api/v2/contacts',
29
        'companies' => '/api/v2/companies',
30
        'tasks' => '/api/v2/tasks',
31
        'customers' => '/api/v2/customers',
32
        'notes' => '/api/v2/notes',
33
    ];
34
35
    /** @var array */
36
    private $entitiesTypes = [
37
        'lead' => 'leads',
38
        'contact' => 'contacts',
39
        'company' => 'companies',
40
        'customer' => 'customers',
41
        'task' => 'tasks',
42
        'note' => 'notes',
43
    ];
44
45
    /** @var SettingsValidator */
46
    private $validator;
47
48
    /** @var string */
49
    private $scheme = 'https';
50
51
    /** @var string */
52
    private $domain = 'amocrm.ru';
53
54
    /** @var string */
55
    private $userAgent = 'amoAPI PHP Client';
56
57
    /** @var string */
58
    private $cookiePath = self::LIB_PATH . '/var/cookie.txt';
59
60
    /**
61
     * SettingsStorage constructor.
62
     */
63 35
    public function __construct()
64
    {
65 35
        $this->validator = new SettingsValidator(); // Composition
66 35
    }
67
68
    /**
69
     * @return string
70
     */
71 1
    public function getScheme()
72
    {
73 1
        return $this->scheme;
74
    }
75
76
    /**
77
     * @param string $scheme
78
     * @throws InvalidArgumentException
79
     */
80 5
    public function setScheme($scheme)
81
    {
82 5
        $this->validator->validateScheme($scheme);
83 1
        $this->scheme = $scheme;
84 1
    }
85
86
    /**
87
     * @return string
88
     */
89 1
    public function getDomain()
90
    {
91 1
        return $this->domain;
92
    }
93
94
    /**
95
     * @param string $domain
96
     * @throws InvalidArgumentException
97
     */
98 5
    public function setDomain($domain)
99
    {
100 5
        $this->validator->validateDomain($domain);
101 1
        $this->domain = $domain;
102 1
    }
103
104
    /**
105
     * @return string
106
     */
107 1
    public function getUserAgent()
108
    {
109 1
        return $this->userAgent;
110
    }
111
112
    /**
113
     * @param string $userAgent
114
     * @throws InvalidArgumentException
115
     */
116 4
    public function setUserAgent($userAgent)
117
    {
118 4
        $this->validator->validateUserAgent($userAgent);
119 1
        $this->userAgent = $userAgent;
120 1
    }
121
122
    /**
123
     * @return array
124
     */
125 2
    public function getMethodsPaths()
126
    {
127 2
        return $this->methodsPaths;
128
    }
129
130
    /**
131
     * @param string $code
132
     * @return string mixed
133
     * @throws InvalidArgumentException
134
     */
135 2
    public function getMethodPath($code)
136
    {
137 2
        if (!isset($this->methodsPaths[$code])) {
138 1
            throw new InvalidArgumentException("The method with code \"$code\" doesn't exist");
139
        }
140
141 1
        return $this->methodsPaths[$code];
142
    }
143
144
    /**
145
     * @param array $paths
146
     * @throws InvalidArgumentException
147
     */
148 8
    public function setMethodsPaths(array $paths)
149
    {
150 8
        $this->validator->validateMethodsPaths($paths);
151 2
        $this->methodsPaths = $paths;
152 2
    }
153
154
    /**
155
     * @return string
156
     */
157 1
    public function getCookiePath()
158
    {
159 1
        return $this->cookiePath;
160
    }
161
162
    /**
163
     * @param string $path
164
     * @throws InvalidArgumentException
165
     */
166 5
    public function setCookiePath($path)
167
    {
168 5
        $this->validator->validateCookiePath($path);
169 1
        $this->cookiePath = $path;
170 1
    }
171
172
    /**
173
     * @param string $type
174
     * @return string
175
     * @throws InvalidArgumentException
176
     */
177 7
    public function getMethodCodeByType($type)
178
    {
179 7
        if (!isset($this->entitiesTypes[$type])) {
180 1
            throw new InvalidArgumentException("The entity with type \"$type\" doesn't exist");
181
        }
182
183 6
        return $this->entitiesTypes[$type];
184
    }
185
}