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

Report::getUrl()   C

Complexity

Conditions 8
Paths 8

Size

Total Lines 52
Code Lines 42

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 32
CRAP Score 8.0402

Importance

Changes 0
Metric Value
dl 0
loc 52
ccs 32
cts 35
cp 0.9143
rs 6.8493
c 0
b 0
f 0
cc 8
eloc 42
nc 8
nop 2
crap 8.0402

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
}