Report   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 214
Duplicated Lines 15.42 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 81.25%

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 1
dl 33
loc 214
ccs 39
cts 48
cp 0.8125
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 2
B getUrl() 0 43 7
A getAvailablePackets() 0 4 1
A getAvailableBooks() 0 4 1
A getAvailableJournals() 0 4 1
A getBooksViewsStatistics() 11 11 1
A getJournalsViewsStatistics() 11 11 1
A getUsersVisitsStatistics() 11 11 1

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
/**
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 5
    public function __construct(Client $client)
63
    {
64 5
        if (!$client) {
65
            throw new Exception('Клиент не инициализирован');
66
        }
67
68 5
        $this->client = $client;
69 5
    }
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
            array(
89 1
                'group_by' => $groupBy,
90 1
                'period_range_from' => $periodFrom,
91 1
                'period_range_to' => $periodTo,
92
            )
93
        )['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 5
    public function getUrl($method, array $params = array())
107
    {
108
        switch ($method) {
109 5
            case 'getBooksViewsStatistics':
110
                return array(
111 1
                    'url' => '/1.0/report/stat/book',
112
                    'method' => 'GET',
113
                    'code' => 200
114
                );
115 4
            case 'getJournalsViewsStatistics':
116
                return array(
117 1
                    'url' => '/1.0/report/stat/journal',
118
                    'method' => 'GET',
119
                    'code' => 200
120
                );
121 3
            case 'getUsersVisitsStatistics':
122
                return array(
123 1
                    'url' => '/1.0/report/stat/visit',
124
                    'method' => 'GET',
125
                    'code' => 200
126
                );
127 2
            case 'getAvailablePackets':
128
                return array(
129 1
                    'url' => '/1.0/report/available/packet',
130
                    'method' => 'GET',
131
                    'code' => 200
132
                );
133 1
            case 'getAvailableBooks':
134
                return array(
135 1
                    'url' => vsprintf('/1.0/report/available/book/%d', $params),
136 1
                    'method' => 'GET',
137 1
                    'code' => 200
138
                );
139
            case 'getAvailableJournals':
140
                return array(
141
                    '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
            array(
166 1
                'group_by' => $groupBy,
167 1
                'period_range_from' => $periodFrom,
168 1
                'period_range_to' => $periodTo,
169
            )
170
        )['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
            array(
189 1
                'group_by' => $groupBy,
190 1
                'period_range_from' => $periodFrom,
191 1
                'period_range_to' => $periodTo,
192
            )
193
        )['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__, array('pdKey' => $pdKey)))['data'];
220
    }
221
222
    /**
223
     * Доступные журналы
224
     *
225
     * @return mixed
226
     *
227
     * @throws Exception
228
     */
229
    public function getAvailableJournals()
230
    {
231
        return $this->client->getResponse($this->getUrl(__FUNCTION__))['data'];
232
    }
233
}