Completed
Push — master ( ce9410...27e308 )
by Denis
06:09
created

Report   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 141
Duplicated Lines 31.91 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 85.53%

Importance

Changes 0
Metric Value
wmc 21
lcom 1
cbo 1
dl 45
loc 141
ccs 65
cts 76
cp 0.8553
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 2
A getSecretKey() 12 12 4
C getUrl() 0 52 8
A getBooksViewsStatistics() 11 11 1
A getJournalsViewsStatistics() 11 11 1
A getUsersVisitsSatistics() 11 11 1
A getAvailablePackets() 0 4 1
A getAvailableBooks() 0 4 1
A getAvailableJournals() 0 4 1
A getFormReportEBooks() 0 4 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
 * Created by PhpStorm.
4
 * User: dp
5
 * Date: 16.08.17
6
 * Time: 13:09
7
 */
8
9
namespace Lan\Ebs\Sdk;
10
11
use Exception;
12
13
final class Report implements Common
14
{
15
    private $client;
16
17
    /**
18
     * Security constructor.
19
     *
20
     * @param  Client $client
21
     * @throws Exception
22
     */
23 7
    public function __construct(Client $client)
24
    {
25 7
        if (!$client) {
26
            throw new Exception('Client not defined');
27
        }
28
29 7
        $this->client = $client;
30 7
    }
31
32 View Code Duplication
    public function getSecretKey($date)
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...
Coding Style introduced by
getSecretKey uses the super-global variable $_SESSION which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

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

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
33
    {
34
        if (is_string($date) && strlen($date) >= 8) {
35
            $date = substr($date, 0, 8);
36
37
            if (!empty($_SESSION[$date])) {
38
                return $_SESSION[$date];
39
            }
40
        }
41
42
        return $_SESSION[$date] = $this->client->getResponse($this->getUrl(__FUNCTION__), ['date' => $date])['data'];
43
    }
44
45 7
    public function getUrl($method, array $params = [])
46
    {
47
        switch ($method) {
48 7
            case 'getBooksViewsStatistics':
49
                return [
50 1
                    'url' => '/1.0/report/stat/book',
51 1
                    'method' => 'GET',
52
                    'code' => 200
53 1
                ];
54 6
            case 'getJournalsViewsStatistics':
55
                return [
56 1
                    'url' => '/1.0/report/stat/journal',
57 1
                    'method' => 'GET',
58
                    'code' => 200
59 1
                ];
60 5
            case 'getUsersVisitsSatistics':
61
                return [
62 1
                    'url' => '/1.0/report/stat/visit',
63 1
                    'method' => 'GET',
64
                    'code' => 200
65 1
                ];
66 4
            case 'getAvailablePackets':
67
                return [
68 1
                    'url' => '/1.0/report/available/packet',
69 1
                    'method' => 'GET',
70 1
                    'params' => [],
71
                    'code' => 200
72 1
                ];
73 3
            case 'getAvailableBooks':
74
                return [
75 1
                    'url' => vsprintf('/1.0/report/available/book/%d', $params),
76 1
                    'method' => 'GET',
77
                    'code' => 200
78 1
                ];
79 2
            case 'getAvailableJournals':
80
                return [
81 1
                    'url' => '/1.0/report/available/journal',
82 1
                    'params' => [],
83 1
                    'method' => 'GET',
84
                    'code' => 200
85 1
                ];
86 1
            case 'getFormReportEBooks':
87
                return [
88 1
                    'url' => '/1.0/report/form/eBooks',
89 1
                    'params' => [],
90 1
                    'method' => 'GET',
91
                    'code' => 200
92 1
                ];
93
            default:
94
                throw new Exception('Route for ' . $method . ' not found');
95
        }
96
    }
97
98 1 View Code Duplication
    public function getBooksViewsStatistics($groupBy = null, $periodFrom = null, $periodTo = 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...
99
    {
100 1
        return $this->client->getResponse(
101 1
            $this->getUrl(__FUNCTION__),
102
            [
103 1
                'group_by' => $groupBy,
104 1
                'period_range_from' => $periodFrom,
105 1
                'period_range_to' => $periodTo,
106
            ]
107 1
        )['data'];
108
    }
109
110 1 View Code Duplication
    public function getJournalsViewsStatistics($groupBy = null, $periodFrom = null, $periodTo = 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...
111
    {
112 1
        return $this->client->getResponse(
113 1
            $this->getUrl(__FUNCTION__),
114
            [
115 1
                'group_by' => $groupBy,
116 1
                'period_range_from' => $periodFrom,
117 1
                'period_range_to' => $periodTo,
118
            ]
119 1
        )['data'];
120
    }
121
122 1 View Code Duplication
    public function getUsersVisitsSatistics($groupBy = null, $periodFrom = null, $periodTo = 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...
123
    {
124 1
        return $this->client->getResponse(
125 1
            $this->getUrl(__FUNCTION__),
126
            [
127 1
                'group_by' => $groupBy,
128 1
                'period_range_from' => $periodFrom,
129 1
                'period_range_to' => $periodTo,
130
            ]
131 1
        )['data'];
132
    }
133
134 1
    public function getAvailablePackets()
135
    {
136 1
        return $this->client->getResponse($this->getUrl(__FUNCTION__))['data'];
137
    }
138
139 1
    public function getAvailableBooks($pdKey)
140
    {
141 1
        return $this->client->getResponse($this->getUrl(__FUNCTION__, ['pdKey' => $pdKey]))['data'];
142
    }
143
144 1
    public function getAvailableJournals()
145
    {
146 1
        return $this->client->getResponse($this->getUrl(__FUNCTION__))['data'];
147
    }
148
149 1
    public function getFormReportEBooks()
150
    {
151 1
        return $this->client->getResponse($this->getUrl(__FUNCTION__))['data'];
152
    }
153
}