Completed
Push — master ( ec18ae...5b38d7 )
by Felix
10:07
created

BaseStat   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 152
Duplicated Lines 42.11 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 6
dl 64
loc 152
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A performQuery() 0 18 4
A checkMaximumDateRange() 0 7 2
A getCollection() 0 10 2
B transformRankDistribution() 64 75 5

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 namespace SchulzeFelix\Stat\Api;
2
3
use Carbon\Carbon;
4
use GuzzleHttp\Exception\ClientException;
5
use SchulzeFelix\Stat\Exceptions\ApiException;
6
use SchulzeFelix\Stat\Exceptions\RequestException;
7
use SchulzeFelix\Stat\Objects\StatEngineRankDistribution;
8
use SchulzeFelix\Stat\Objects\StatRankDistribution;
9
use SchulzeFelix\Stat\StatClient;
10
11
class BaseStat
12
{
13
    /**
14
     * @var StatClient
15
     */
16
    protected $statClient;
17
18
    /**
19
     * BaseStat constructor.
20
     * @param StatClient $statClient
21
     */
22
    public function __construct(StatClient $statClient)
23
    {
24
        $this->statClient = $statClient;
25
    }
26
27
28
    /**
29
     * @param $method
30
     * @param array $parameters
31
     * @return mixed
32
     * @throws ApiException
33
     */
34
    public function performQuery($method, $parameters = [])
35
    {
36
        try
37
        {
38
            $response = $this->statClient->performQuery($method, $parameters);
39
        }
40
        catch(ClientException $e)
41
        {
42
            $xml = simplexml_load_string($e->getResponse()->getBody()->getContents());
43
            throw ApiException::requestException($xml->__toString());
44
        }
45
46
        if (isset($response['Response']['responsecode']) && $response['Response']['responsecode'] == '200') {
47
            return $response['Response'];
48
        }
49
50
        throw ApiException::resultError($response['Result']);
51
    }
52
53
    /**
54
     * @param Carbon $fromDate
55
     * @param Carbon $toDate
56
     * @param int $maxDays
57
     * @throws ApiException
58
     */
59
    protected function checkMaximumDateRange(Carbon $fromDate, Carbon $toDate, $maxDays = 31)
60
    {
61
62
        if($fromDate->diffInDays($toDate) > $maxDays) {
63
            throw ApiException::resultError('The maximum date range between from_date and to_date is '.$maxDays.' days.');
64
        }
65
    }
66
67
    /**
68
     * @param $element
69
     * @param string $identifier
70
     * @return \Illuminate\Support\Collection
71
     */
72
    protected function getCollection($element, $identifier = 'Id')
73
    {
74
        if(isset($element[$identifier])){
75
            $collection = collect([$element]);
76
        } else {
77
            $collection = collect($element);
78
        }
79
80
        return $collection;
81
    }
82
83
    /**
84
     * @param $distribution
85
     * @return StatRankDistribution
86
     */
87
    protected function transformRankDistribution($distribution)
88
    {
89
        $rankDistribution = new StatRankDistribution();
90
        $rankDistribution->date = $distribution['date'];
91
92 View Code Duplication
        if(array_key_exists('Google', $distribution)){
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
93
            $rankDistribution->google = new StatEngineRankDistribution([
94
                'one' => $distribution['Google']['One'],
95
                'two' => $distribution['Google']['Two'],
96
                'three' => $distribution['Google']['Three'],
97
                'four' => $distribution['Google']['Four'],
98
                'five' => $distribution['Google']['Five'],
99
                'six_to_ten' => $distribution['Google']['SixToTen'],
100
                'eleven_to_twenty' => $distribution['Google']['ElevenToTwenty'],
101
                'twenty_one_to_thirty' => $distribution['Google']['TwentyOneToThirty'],
102
                'thirty_one_to_forty' => $distribution['Google']['ThirtyOneToForty'],
103
                'forty_one_to_fifty' => $distribution['Google']['FortyOneToFifty'],
104
                'fifty_one_to_hundred' => $distribution['Google']['FiftyOneToHundred'],
105
                'non_ranking' => $distribution['Google']['NonRanking'],
106
            ]);
107
        }
108
109 View Code Duplication
        if(array_key_exists('GoogleBaseRank', $distribution)){
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
110
            $rankDistribution->google_base_rank = new StatEngineRankDistribution([
111
                'one' => $distribution['GoogleBaseRank']['One'],
112
                'two' => $distribution['GoogleBaseRank']['Two'],
113
                'three' => $distribution['GoogleBaseRank']['Three'],
114
                'four' => $distribution['GoogleBaseRank']['Four'],
115
                'five' => $distribution['GoogleBaseRank']['Five'],
116
                'six_to_ten' => $distribution['GoogleBaseRank']['SixToTen'],
117
                'eleven_to_twenty' => $distribution['GoogleBaseRank']['ElevenToTwenty'],
118
                'twenty_one_to_thirty' => $distribution['GoogleBaseRank']['TwentyOneToThirty'],
119
                'thirty_one_to_forty' => $distribution['GoogleBaseRank']['ThirtyOneToForty'],
120
                'forty_one_to_fifty' => $distribution['GoogleBaseRank']['FortyOneToFifty'],
121
                'fifty_one_to_hundred' => $distribution['GoogleBaseRank']['FiftyOneToHundred'],
122
                'non_ranking' => $distribution['GoogleBaseRank']['NonRanking'],
123
            ]);
124
        }
125
126 View Code Duplication
        if(array_key_exists('Yahoo', $distribution)){
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
127
            $rankDistribution->yahoo = new StatEngineRankDistribution([
128
                'one' => $distribution['Yahoo']['One'],
129
                'two' => $distribution['Yahoo']['Two'],
130
                'three' => $distribution['Yahoo']['Three'],
131
                'four' => $distribution['Yahoo']['Four'],
132
                'five' => $distribution['Yahoo']['Five'],
133
                'six_to_ten' => $distribution['Yahoo']['SixToTen'],
134
                'eleven_to_twenty' => $distribution['Yahoo']['ElevenToTwenty'],
135
                'twenty_one_to_thirty' => $distribution['Yahoo']['TwentyOneToThirty'],
136
                'thirty_one_to_forty' => $distribution['Yahoo']['ThirtyOneToForty'],
137
                'forty_one_to_fifty' => $distribution['Yahoo']['FortyOneToFifty'],
138
                'fifty_one_to_hundred' => $distribution['Yahoo']['FiftyOneToHundred'],
139
                'non_ranking' => $distribution['Yahoo']['NonRanking'],
140
            ]);
141
        }
142
143 View Code Duplication
        if(array_key_exists('Bing', $distribution)){
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
144
            $rankDistribution->bing = new StatEngineRankDistribution([
145
                'one' => $distribution['Bing']['One'],
146
                'two' => $distribution['Bing']['Two'],
147
                'three' => $distribution['Bing']['Three'],
148
                'four' => $distribution['Bing']['Four'],
149
                'five' => $distribution['Bing']['Five'],
150
                'six_to_ten' => $distribution['Bing']['SixToTen'],
151
                'eleven_to_twenty' => $distribution['Bing']['ElevenToTwenty'],
152
                'twenty_one_to_thirty' => $distribution['Bing']['TwentyOneToThirty'],
153
                'thirty_one_to_forty' => $distribution['Bing']['ThirtyOneToForty'],
154
                'forty_one_to_fifty' => $distribution['Bing']['FortyOneToFifty'],
155
                'fifty_one_to_hundred' => $distribution['Bing']['FiftyOneToHundred'],
156
                'non_ranking' => $distribution['Bing']['NonRanking'],
157
            ]);
158
        }
159
160
        return $rankDistribution;
161
    }
162
}
163