Completed
Push — master ( 6d820f...7bdb34 )
by dotzero
02:00
created

Call::apiAdd()   B

Complexity

Conditions 6
Paths 20

Size

Total Lines 30
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 6.0052

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 30
ccs 18
cts 19
cp 0.9474
rs 8.439
cc 6
eloc 16
nc 20
nop 3
crap 6.0052
1
<?php
2
3
namespace AmoCRM\Models;
4
5
/**
6
 * Class Call
7
 *
8
 * Класс модель для работы со Звонками
9
 *
10
 * @package AmoCRM\Models
11
 * @author dotzero <[email protected]>
12
 * @link http://www.dotzero.ru/
13
 * @link https://github.com/dotzero/amocrm-php
14
 *
15
 * For the full copyright and license information, please view the LICENSE
16
 * file that was distributed with this source code.
17
 */
18
class Call extends AbstractModel
19
{
20
    /**
21
     * @var array Список доступный полей для модели (исключая кастомные поля)
22
     */
23
    protected $fields = [
24
        'account_id',
25
        'uuid',
26
        'caller',
27
        'to',
28
        'date',
29
        'type',
30
        'billsec',
31
        'link',
32
    ];
33
34
    /**
35
     * @const string Тип звонка входящий
36
     */
37
    const TYPE_INBOUND = 'inbound';
38
39
    /**
40
     * @const string Тип звонка исходящий
41
     */
42
    const TYPE_OUTBOUND = 'outbound';
43
44
    /**
45
     * Сеттер для даты создания контакта
46
     *
47
     * @param string $date Дата в произвольном формате
48
     * @return $this
49
     */
50 1
    public function setDate($date)
51
    {
52 1
        $this->values['date'] = strtotime($date);
53
54 1
        return $this;
55
    }
56
57
    /**
58
     * Добавление звонков
59
     *
60
     * Метод позволяет добавлять звонки по одному или пакетно
61
     *
62
     * @link https://developers.amocrm.ru/rest_api/calls_set.php
63
     * @param string $code Уникальный идентификатор сервиса
64
     * @param string $key Ключ сервиса, который можно получить написав в техническую поддержку amoCRM
65
     * @param array $calls Массив каталогов для пакетного добавления
66
     * @return string|array Уникальный идентификатор звонка или массив при пакетном добавлении
67
     */
68 1
    public function apiAdd($code, $key, $calls = [])
69
    {
70 1
        $this->getParameters()->addGet('code', $code);
71 1
        $this->getParameters()->addGet('key', $key);
72
73 1
        if (empty($calls)) {
74 1
            $calls = [$this];
75 1
        }
76
77
        $parameters = [
78 1
            'add' => [],
79 1
        ];
80
81 1
        foreach ($calls AS $call) {
82 1
            $parameters['add'][] = $call->getValues();
83 1
        }
84
85 1
        $response = $this->postRequest('/api/calls/add/', $parameters);
86
87 1
        if (!isset($response['calls']['add']['errors'])) {
88
            return false;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type documented by AmoCRM\Models\Call::apiAdd of type string|array.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
89
        }
90
91 1
        $result = [];
92 1
        if (isset($response['calls']['add']['success'])) {
93 1
            $result = $response['calls']['add']['success'];
94 1
        }
95
96 1
        return count($calls) == 1 ? array_shift($result) : $result;
97
    }
98
}
99