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

AuthorizationService   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 8
c 1
b 0
f 1
lcom 1
cbo 2
dl 0
loc 82
ccs 23
cts 23
cp 1
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A add() 0 6 2
A authorize() 0 9 1
A checkResponse() 0 8 2
A getAuthLink() 0 4 1
A composeFields() 0 4 1
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
}