Completed
Push — master ( b06c4a...1da428 )
by Denis
36:46 queued 33:46
created

Book::getUrl()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 19
Code Lines 14

Duplication

Lines 19
Ratio 100 %

Code Coverage

Tests 6
CRAP Score 3.8449

Importance

Changes 0
Metric Value
dl 19
loc 19
ccs 6
cts 11
cp 0.5455
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 14
nc 3
nop 2
crap 3.8449
1
<?php
2
/**
3
 * Class Book
4
 *
5
 * @author       Denis Shestakov <[email protected]>
6
 * @copyright    Copyright (c) 2017, Lan Publishing
7
 * @license      MIT
8
 */
9
10
namespace Lan\Ebs\Sdk\Model;
11
12
use Exception;
13
use Lan\Ebs\Sdk\Classes\Model;
14
use Lan\Ebs\Sdk\Client;
15
16
/**
17
 * Модель книг
18
 *
19
 * @property mixed name
20
 * @property mixed description
21
 * @property mixed isbn
22
 * @property mixed year
23
 * @property mixed edition
24
 * @property mixed pages
25
 * @property mixed specialMarks
26
 * @property mixed classification
27
 * @property mixed authors
28
 * @property mixed authorAdditions
29
 * @property mixed bibliographicRecord
30
 * @property mixed contentQuality
31
 * @property mixed publisher
32
 * @property mixed url
33
 * @property mixed thumb
34
 *
35
 * @package      Lan\Ebs
36
 * @subpackage   Sdk
37
 * @category     Model
38
 */
39
class Book extends Model
40
{
41
    /**
42
     * Наименование книги
43
     */
44
    const FIELD_NAME = 'name';
45
46
    /**
47
     * Описание книги
48
     */
49
    const FIELD_DESCRIPTION = 'description';
50
51
    /**
52
     * ISBN книги
53
     */
54
    const FIELD_ISBN = 'isbn';
55
56
    /**
57
     * Год издания книги
58
     */
59
    const FIELD_YEAR = 'year';
60
61
    /**
62
     * Издание
63
     */
64
    const FIELD_EDITION = 'edition';
65
66
    /**
67
     * Объем книги
68
     */
69
    const FIELD_PAGES = 'pages';
70
71
    /**
72
     * Специальные отметки
73
     */
74
    const FIELD_SPECIAL_MARKS = 'specialMarks';
75
76
    /**
77
     * Гриф
78
     */
79
    const FIELD_CLASSIFICATION = 'classification';
80
81
    /**
82
     * Авторы
83
     */
84
    const FIELD_AUTHORS = 'authors';
85
86
    /**
87
     * Дополнительные авторы
88
     */
89
    const FIELD_AUTHOR_ADDITIONS = 'authorAdditions';
90
91
    /**
92
     * Библиографическая запись
93
     */
94
    const FIELD_BIBLIOGRAPHIC_RECORD = 'bibliographicRecord';
95
96
    /**
97
     * Качество текста книг (процент)
98
     */
99
    const FIELD_CONTENT_QUALITY = 'contentQuality';
100
101
    /**
102
     * Издательство
103
     */
104
    const FIELD_PUBLISHER = 'publisher';
105
106
    /**
107
     * Ссылка на карточку книги
108
     */
109
    const FIELD_URL = 'url';
110
111
    /**
112
     * Ссылка на обложку книги
113
     */
114
    const FIELD_THUMB = 'thumb';
115
116
    /**
117
     * Конструктор модели пользователя
118
     *
119
     * @param Client $client Инстанс клиента
120
     * @param array $fields Поля для выборки
121
     *
122
     * @throws Exception
123
     */
124 2
    public function __construct(Client $client, array $fields = [])
125
    {
126 2
        parent::__construct($client, $fields);
127 2
    }
128
129
    /**
130
     * Получение текстов книги
131
     *
132
     * @param int $id Идентификатор модели
133
     *
134
     * @return array
135
     *
136
     * @throws Exception
137
     */
138 View Code Duplication
    public function text($id = null)
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...
139
    {
140
        if ($id) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $id of type integer|null is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
141
            $this->setId($id);
142
        } else {
143
            $id = $this->getId();
144
        }
145
146
        if (empty($id)) {
147
            throw new Exception(Model::MESSAGE_ID_REQUIRED);
148
        }
149
150
        return $this->getClient()->getResponse($this->getUrl(__FUNCTION__, [$id]))['data'];
151
    }
152
153
    /**
154
     * Получение данных для запроса через API
155
     *
156
     * @param string $method Http-метод запроса
157
     * @param array $params Параметры для формирования урла
158
     *
159
     * ```php
160
     *
161
     *  $url = $this->getUrl('get');
162
     * ```
163
     *
164
     * @return array
165
     *
166
     * @throws Exception
167
     */
168 1 View Code Duplication
    public function getUrl($method, array $params = [])
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...
169
    {
170 1
        switch ($method) {
171 1
            case 'get':
172
                return [
173 1
                    'url' => vsprintf('/1.0/resource/book/get/%d', $params),
174 1
                    'method' => 'GET',
175 1
                    'code' => 200
176
                ];
177
            case 'text':
178
                return [
179
                    'url' => vsprintf('/1.0/resource/book/text/%d', $params),
180
                    'method' => 'GET',
181
                    'code' => 200
182
                ];
183
            default:
184
                throw new Exception('Route for ' . $method . ' not found');
185
        }
186
    }
187
}