Completed
Push — master ( 44179c...b97fa1 )
by Denis
04:19 queued 02:26
created

Report::getUrl()   C

Complexity

Conditions 7
Paths 7

Size

Total Lines 43
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 7.0119

Importance

Changes 0
Metric Value
dl 0
loc 43
ccs 15
cts 16
cp 0.9375
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 34
nc 7
nop 2
crap 7.0119
1
<?php
2
/**
3
 * Class Report
4
 *
5
 * @author       Emil Limarenko <[email protected]>
6
 * @copyright    Copyright (c) 2017, Lan Publishing
7
 * @license      MIT
8
 */
9
10
namespace Lan\Ebs\Sdk;
11
12
use Exception;
13
14
/**
15
 * SDK для общих отчетов
16
 *
17
 * @package      Lan\Ebs
18
 * @subpackage   Sdk
19
 */
20
class Report implements Common
21
{
22
    /**
23
     * Группировка по дням
24
     */
25
    const GROUP_BY_DAY = 'day';
26
27
    /**
28
     * Группировка по месяцам
29
     */
30
    const GROUP_BY_MONTH = 'month';
31
32
    /**
33
     * Группировка по годам
34
     */
35
    const GROUP_BY_YEAR = 'year';
36
37
    /**
38
     * Инстанс клиента API
39
     *
40
     * @var Client
41
     */
42
    private $client;
43
44
    /**
45
     * Конструктор общего отчета
46
     *
47
     * Экземпляр класса Report нужен для осуществления запросов к API для получения отчетных данных ЭБС Лань.
48
     *
49
     * @param Client $client Инстанс клиента
50
     *
51
     * Пример:
52
     * ```php
53
     *      $token = '7c0c2193d27108a509abd8ea84a8750c82b3a520'; // токен для тестового подписчика
54
     *
55
     *      $client = new Client($token); // инициализация клиента
56
     *
57
     *      $report = new Report($client):
58
     * ```
59
     *
60
     * @throws Exception
61
     */
62 6
    public function __construct(Client $client)
63
    {
64 6
        if (!$client) {
65
            throw new Exception('Клиент не инициализирован');
66
        }
67
68 6
        $this->client = $client;
69 6
    }
70
71
    /**
72
     * Общая статистика чтения книг
73
     *
74
     * @param string $groupBy Группировка ('day|month|year')
75
     * @param string $periodFrom Период с (формат Y-m-d, например 2017-10-01)
76
     * @param string $periodTo Период с (формат Y-m-d, например 2017-11-01)
77
     *
78
     *
79
     *
80
     * @return mixed
81
     *
82
     * @throws Exception
83
     */
84 1 View Code Duplication
    public function getBooksViewsStatistics($groupBy, $periodFrom, $periodTo)
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...
85
    {
86 1
        return $this->client->getResponse(
87 1
            $this->getUrl(__FUNCTION__),
88
            [
89 1
                'group_by' => $groupBy,
90 1
                'period_range_from' => $periodFrom,
91 1
                'period_range_to' => $periodTo,
92
            ]
93 1
        )['data'];
94
    }
95
96
    /**
97
     * Получение данных для запроса через API
98
     *
99
     * @param string $method Http-метод запроса
100
     * @param array $params Параметры для формирования урла
101
     *
102
     * @return array
103
     *
104
     * @throws Exception
105
     */
106 6
    public function getUrl($method, array $params = [])
107
    {
108
        switch ($method) {
109 6
            case 'getBooksViewsStatistics':
110
                return [
111 1
                    'url' => '/1.0/report/stat/book',
112
                    'method' => 'GET',
113
                    'code' => 200
114
                ];
115 5
            case 'getJournalsViewsStatistics':
116
                return [
117 1
                    'url' => '/1.0/report/stat/journal',
118
                    'method' => 'GET',
119
                    'code' => 200
120
                ];
121 4
            case 'getUsersVisitsStatistics':
122
                return [
123 1
                    'url' => '/1.0/report/stat/visit',
124
                    'method' => 'GET',
125
                    'code' => 200
126
                ];
127 3
            case 'getAvailablePackets':
128
                return [
129 1
                    'url' => '/1.0/report/available/packet',
130
                    'method' => 'GET',
131
                    'code' => 200
132
                ];
133 2
            case 'getAvailableBooks':
134
                return [
135 1
                    'url' => vsprintf('/1.0/report/available/book/%d', $params),
136 1
                    'method' => 'GET',
137 1
                    'code' => 200
138
                ];
139 1
            case 'getAvailableJournals':
140
                return [
141 1
                    'url' => '/1.0/report/available/journal',
142
                    'method' => 'GET',
143
                    'code' => 200
144
                ];
145
            default:
146
                throw new Exception('Route for ' . $method . ' not found');
147
        }
148
    }
149
150
    /**
151
     * Общая статистика чтения журналов
152
     *
153
     * @param string $groupBy Группировка ('day|month|year')
154
     * @param string $periodFrom Период с (формат Y-m-d, например 2017-10-01)
155
     * @param string $periodTo Период с (формат Y-m-d, например 2017-11-01)
156
     *
157
     * @return mixed
158
     *
159
     * @throws Exception
160
     */
161 1 View Code Duplication
    public function getJournalsViewsStatistics($groupBy, $periodFrom, $periodTo)
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...
162
    {
163 1
        return $this->client->getResponse(
164 1
            $this->getUrl(__FUNCTION__),
165
            [
166 1
                'group_by' => $groupBy,
167 1
                'period_range_from' => $periodFrom,
168 1
                'period_range_to' => $periodTo,
169
            ]
170 1
        )['data'];
171
    }
172
173
    /**
174
     * Статистика посещаемости
175
     *
176
     * @param string $groupBy Группировка ('day|month|year')
177
     * @param string $periodFrom Период с (формат Y-m-d, например 2017-10-01)
178
     * @param string $periodTo Период с (формат Y-m-d, например 2017-11-01)
179
     *
180
     * @return mixed
181
     *
182
     * @throws Exception
183
     */
184 1 View Code Duplication
    public function getUsersVisitsStatistics($groupBy, $periodFrom, $periodTo)
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...
185
    {
186 1
        return $this->client->getResponse(
187 1
            $this->getUrl(__FUNCTION__),
188
            [
189 1
                'group_by' => $groupBy,
190 1
                'period_range_from' => $periodFrom,
191 1
                'period_range_to' => $periodTo,
192
            ]
193 1
        )['data'];
194
    }
195
196
    /**
197
     * Доступные пакеты книг
198
     *
199
     * @return mixed
200
     *
201
     * @throws Exception
202
     */
203 1
    public function getAvailablePackets()
204
    {
205 1
        return $this->client->getResponse($this->getUrl(__FUNCTION__))['data'];
206
    }
207
208
    /**
209
     * Доступные книги в пакете
210
     *
211
     * @param int $pdKey Идентификатор пакета
212
     *
213
     * @return mixed
214
     *
215
     * @throws Exception
216
     */
217 1
    public function getAvailableBooks($pdKey)
218
    {
219 1
        return $this->client->getResponse($this->getUrl(__FUNCTION__, ['pdKey' => $pdKey]))['data'];
220
    }
221
222
    /**
223
     * Доступные журналы
224
     *
225
     * @return mixed
226
     *
227
     * @throws Exception
228
     */
229 1
    public function getAvailableJournals()
230
    {
231 1
        return $this->client->getResponse($this->getUrl(__FUNCTION__))['data'];
232
    }
233
}