Completed
Push — master ( 0fb1a1...c53a6f )
by Denis
02:17
created

Report::getUsersVisitsStatistics()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 11
Ratio 100 %

Code Coverage

Tests 6
CRAP Score 1.0028

Importance

Changes 0
Metric Value
dl 11
loc 11
ccs 6
cts 7
cp 0.8571
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 3
crap 1.0028
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
     * @param Client $client Инстанс клиента
48
     *
49
     * @throws Exception
50
     */
51 6
    public function __construct(Client $client)
52
    {
53 6
        if (!$client) {
54
            throw new Exception('Клиент не инициализирован');
55
        }
56
57 6
        $this->client = $client;
58 6
    }
59
60
    /**
61
     * Общая статистика чтения книг
62
     *
63
     * @param string $groupBy Группировка ('day|month|year')
64
     * @param string $periodFrom Период с (формат Y-m-d, например 2017-07-01)
65
     * @param string $periodTo Период с (формат Y-m-d, например 2017-08-28)
66
     *
67
     * @return mixed
68
     *
69
     * @throws Exception
70
     */
71 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...
72
    {
73 1
        return $this->client->getResponse(
74 1
            $this->getUrl(__FUNCTION__),
75
            [
76 1
                'group_by' => $groupBy,
77 1
                'period_range_from' => $periodFrom,
78 1
                'period_range_to' => $periodTo,
79
            ]
80
        )['data'];
81
    }
82
83
    /**
84
     * Получение данных для запроса через API
85
     *
86
     * @param string $method Http-метод запроса
87
     * @param array $params Параметры для формирования урла
88
     *
89
     * @return array
90
     *
91
     * @throws Exception
92
     */
93 6
    public function getUrl($method, array $params = [])
94
    {
95
        switch ($method) {
96 6
            case 'getBooksViewsStatistics':
97
                return [
98 1
                    'url' => '/1.0/report/stat/book',
99
                    'method' => 'GET',
100
                    'code' => 200
101
                ];
102 5
            case 'getJournalsViewsStatistics':
103
                return [
104 1
                    'url' => '/1.0/report/stat/journal',
105
                    'method' => 'GET',
106
                    'code' => 200
107
                ];
108 4
            case 'getUsersVisitsStatistics':
109
                return [
110 1
                    'url' => '/1.0/report/stat/visit',
111
                    'method' => 'GET',
112
                    'code' => 200
113
                ];
114 3
            case 'getAvailablePackets':
115
                return [
116 1
                    'url' => '/1.0/report/available/packet',
117
                    'method' => 'GET',
118
                    'code' => 200
119
                ];
120 2
            case 'getAvailableBooks':
121
                return [
122 1
                    'url' => vsprintf('/1.0/report/available/book/%d', $params),
123 1
                    'method' => 'GET',
124 1
                    'code' => 200
125
                ];
126 1
            case 'getAvailableJournals':
127
                return [
128 1
                    'url' => '/1.0/report/available/journal',
129
                    'method' => 'GET',
130
                    'code' => 200
131
                ];
132
            default:
133
                throw new Exception('Route for ' . $method . ' not found');
134
        }
135
    }
136
137
    /**
138
     * Общая статистика чтения журналов
139
     *
140
     * @param string $groupBy Группировка ('day|month|year')
141
     * @param string $periodFrom Период с (формат Y-m-d, например 2017-07-01)
142
     * @param string $periodTo Период с (формат Y-m-d, например 2017-08-28)
143
     *
144
     * @return mixed
145
     *
146
     * @throws Exception
147
     */
148 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...
149
    {
150 1
        return $this->client->getResponse(
151 1
            $this->getUrl(__FUNCTION__),
152
            [
153 1
                'group_by' => $groupBy,
154 1
                'period_range_from' => $periodFrom,
155 1
                'period_range_to' => $periodTo,
156
            ]
157
        )['data'];
158
    }
159
160
    /**
161
     * Статистика посещаемости
162
     *
163
     * @param string $groupBy Группировка ('day|month|year')
164
     * @param string $periodFrom Период с (формат Y-m-d, например 2017-07-01)
165
     * @param string $periodTo Период с (формат Y-m-d, например 2017-08-28)
166
     *
167
     * @return mixed
168
     *
169
     * @throws Exception
170
     */
171 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...
172
    {
173 1
        return $this->client->getResponse(
174 1
            $this->getUrl(__FUNCTION__),
175
            [
176 1
                'group_by' => $groupBy,
177 1
                'period_range_from' => $periodFrom,
178 1
                'period_range_to' => $periodTo,
179
            ]
180
        )['data'];
181
    }
182
183
    /**
184
     * Доступные пакеты книг
185
     *
186
     * @return mixed
187
     *
188
     * @throws Exception
189
     */
190 1
    public function getAvailablePackets()
191
    {
192 1
        return $this->client->getResponse($this->getUrl(__FUNCTION__))['data'];
193
    }
194
195
    /**
196
     * Доступные книги в пакете
197
     *
198
     * @param int $pdKey Идентификатор пакета
199
     *
200
     * @return mixed
201
     *
202
     * @throws Exception
203
     */
204 1
    public function getAvailableBooks($pdKey)
205
    {
206 1
        return $this->client->getResponse($this->getUrl(__FUNCTION__, ['pdKey' => $pdKey]))['data'];
207
    }
208
209
    /**
210
     * Доступные журналы
211
     *
212
     * @return mixed
213
     *
214
     * @throws Exception
215
     */
216 1
    public function getAvailableJournals()
217
    {
218 1
        return $this->client->getResponse($this->getUrl(__FUNCTION__))['data'];
219
    }
220
}