Completed
Push — master ( 3a1096...742fc8 )
by Konstantin
01:36
created

LeadService   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 92.86%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 11
lcom 1
cbo 1
dl 0
loc 93
ccs 26
cts 28
cp 0.9286
rs 10
c 1
b 0
f 1

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A add() 0 6 2
A createLead() 0 12 2
A getResponse() 0 4 1
A checkResponse() 0 8 2
A getLink() 0 4 1
A composeAddFields() 0 10 2
1
<?php
2
3
namespace linkprofit\AmoCRM\services;
4
5
use linkprofit\AmoCRM\entities\EntityInterface;
6
use linkprofit\AmoCRM\entities\Lead;
7
use linkprofit\AmoCRM\RequestHandler;
8
9
class LeadService 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 array Lead
28
     */
29
    protected $lead = [];
30
31
    /**
32
     * LeadService constructor.
33
     * @param RequestHandler $requestHandler
34
     */
35 2
    public function __construct(RequestHandler $requestHandler)
36
    {
37 2
        $this->request = $requestHandler;
38 2
    }
39
40
    /**
41
     * @param Lead $lead
42
     */
43 2
    public function add(EntityInterface $lead)
44
    {
45 2
        if ($lead instanceof Lead) {
46 2
            $this->lead[] = $lead;
47
        }
48 2
    }
49
50 2
    public function createLead()
51
    {
52 2
        $this->composeAddFields();
53 2
        $this->request->performRequest($this->getLink(), $this->fields);
54 2
        $this->response = $this->request->getResponse();
55
56 2
        if ($this->checkResponse()) {
57 2
            return $this->getResponse();
58
        }
59
60
        return false;
61
    }
62
63 2
    public function getResponse()
64
    {
65 2
        return $this->response;
66
    }
67
68
    /**
69
     * @return bool
70
     */
71 2
    protected function checkResponse()
72
    {
73 2
        if (isset($this->response['_embedded']['items'])) {
74 2
            return true;
75
        }
76
77
        return false;
78
    }
79
80
    /**
81
     * @return string
82
     */
83 2
    protected function getLink()
84
    {
85 2
        return 'https://' . $this->request->getSubdomain() . '.amocrm.ru/api/v2/leads';
86
    }
87
88
    /**
89
     * Fill fields for response
90
     */
91 2
    protected function composeAddFields()
92
    {
93 2
       $fields = [];
94
95 2
       foreach ($this->lead as $lead) {
96 2
           $fields[] = $lead->get();
97
       }
98
99 2
        $this->fields['add'] = $fields;
100
    }
101
}