Completed
Push — master ( ab131e...3a1096 )
by Konstantin
01:35
created

AuthorizationService::add()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 6
ccs 5
cts 5
cp 1
rs 9.4285
cc 2
eloc 3
nc 2
nop 1
crap 2
1
<?php
2
3
namespace linkprofit\AmoCRM\services;
4
5
use linkprofit\AmoCRM\entities\Authorization;
6
use linkprofit\AmoCRM\entities\EntityInterface;
7
use linkprofit\AmoCRM\RequestHandler;
8
9
class AuthorizationService implements ServiceInterface
10
{
11
    /**
12
     * @var RequestHandler
13
     */
14
    protected $request;
15
16
    /**
17
     * @var array
18
     */
19
    protected $fields;
20
21
    /**
22
     * @var
23
     */
24
    protected $response;
25
26
    /**
27
     * @var Authorization
28
     */
29
    protected $authorization;
30
31
    /**
32
     * AuthorizationService constructor.
33
     * @param RequestHandler $request
34
     */
35 2
    public function __construct(RequestHandler $request)
36
    {
37 2
        $this->request = $request;
38 2
    }
39
40
    /**
41
     * @param Authorization $authorization
42
     */
43 2
    public function add(EntityInterface $authorization)
44
    {
45 2
        if ($authorization instanceof Authorization) {
46 2
            $this->authorization = $authorization;
47 2
        }
48 2
    }
49
50
    /**
51
     * @return bool
52
     */
53 2
    public function authorize()
54
    {
55 2
        $this->composeFields();
56 2
        $this->request->performRequest($this->getAuthLink(), $this->fields);
57 2
        $this->response = $this->request->getResponse();
58 2
        $this->response = $this->response['response'];
59
60 2
        return $this->checkResponse();
61
    }
62
63
    /**
64
     * @return bool
65
     */
66 2
    protected function checkResponse()
67
    {
68 2
        if (isset($this->response['auth'])) {
69 1
            return true;
70
        }
71
72 1
        return false;
73
    }
74
75
    /**
76
     * @return string
77
     */
78 2
    protected function getAuthLink()
79
    {
80 2
        return 'https://' . $this->request->getSubdomain() . '.amocrm.ru/private/api/auth.php?type=json';
81
    }
82
83
    /**
84
     * Fill fields for response
85
     */
86 2
    protected function composeFields()
87
    {
88 2
        $this->fields = $this->authorization->get();
89
    }
90
}