Completed
Pull Request — dev (#3)
by
unknown
01:49
created

TaskTypeService   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 116
Duplicated Lines 40.52 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 19
c 1
b 0
f 0
lcom 1
cbo 3
dl 47
loc 116
ccs 44
cts 44
cp 1
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A parseArrayToEntity() 0 7 1
A getLink() 0 4 1
A add() 0 6 2
A save() 12 12 2
B composeFields() 22 22 5
A parseResponseToEntities() 13 13 3
A checkResponse() 0 8 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
namespace linkprofit\AmoCRM\services;
3
4
use linkprofit\AmoCRM\entities\EntityInterface;
5
use linkprofit\AmoCRM\entities\TaskType;
6
use linkprofit\AmoCRM\RequestHandler;
7
8
9
/**
10
 * Class TaskTypeService
11
 *
12
 * @package linkprofit\AmoCRM\services
13
 */
14
class TaskTypeService extends BaseService
15
{
16 4
    public function __construct(\linkprofit\AmoCRM\RequestHandler $requestHandler)
17
    {
18 4
        parent::__construct($requestHandler);
19 4
    }
20
21
    /**
22
     * @var TaskType[]
23
     */
24
    protected $entities = [];
25
26
    /**
27
     * @param $array
28
     *
29
     * @return EntityInterface
30
     */
31 3
    public function parseArrayToEntity($array)
32
    {
33 3
        $taskType = new TaskType();
34 3
        $taskType->set($array);
35
36 3
        return $taskType;
37
    }
38
39
    /**
40
     * @return string
41
     */
42 3
    protected function getLink()
43
    {
44 3
        return 'https://' . $this->request->getSubdomain() . '.amocrm.ru/private/tasks/ajax_task_status_edit.php';
45
    }
46
47
    /**
48
     * @param \linkprofit\AmoCRM\entities\EntityInterface $taskType
49
     *
50
     * @return mixed
51
     */
52 3
    public function add(EntityInterface $taskType)
53
    {
54 3
        if ($taskType instanceof TaskType) {
55 3
            $this->entities[] = $taskType;
56
        }
57 3
    }
58
59
    /**
60
     * @return bool|mixed
61
     */
62 3 View Code Duplication
    public function save()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
63
    {
64 3
        $this->composeFields();
65 3
        $this->request->performRequest($this->getLink(), $this->fields, 'application/x-www-form-urlencoded');
66 3
        $this->response = $this->request->getResponse();
67
68 3
        if ($this->checkResponse()) {
69 2
            return $this->getResponse();
70
        }
71
72 1
        return false;
73
    }
74
75
    /**
76
     * Fill fields for save request
77
     */
78 3 View Code Duplication
    protected function composeFields()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
79
    {
80 3
        $addFields = [];
81 3
        $updateFields = [];
82
83 3
        foreach ($this->entities as $entity) {
84 3
            if ($entity->id) {
85 1
                $updateFields[] = $entity->get();
86
            } else {
87 3
                $addFields[] = $entity->get();
88
            }
89
        }
90 3
        $this->fields['ACTION'] = 'ALL_EDIT';
91
92 3
        if (count($addFields)) {
93 2
            $this->fields['task_types'] = $addFields;
94
        }
95
96 3
        if (count($updateFields)) {
97 1
            $this->fields['task_types'] = $updateFields;
98
        }
99 3
    }
100
101
    /**
102
     * @return array|bool
103
     */
104 3 View Code Duplication
    public function parseResponseToEntities()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
105
    {
106 3
        if (!$this->checkResponse()) {
107 1
            return false;
108
        }
109 2
        $this->entities = [];
110
111 2
        foreach ($this->response['data'] as $item) {
112 2
            $this->entities[] = $this->parseArrayToEntity($item);
113
        }
114
115 2
        return $this->entities;
116
    }
117
118
    /**
119
     * @return bool
120
     */
121 3
    protected function checkResponse()
122
    {
123 3
        if (isset($this->response['status']) && $this->response['status'] === 'OK' && count($this->response['data'])) {
124 2
            return true;
125
        }
126
127 1
        return false;
128
    }
129
}