BaseStat::transformRankDistribution()   B
last analyzed

Complexity

Conditions 4
Paths 8

Size

Total Lines 58

Duplication

Lines 48
Ratio 82.76 %

Importance

Changes 0
Metric Value
dl 48
loc 58
rs 8.9163
c 0
b 0
f 0
cc 4
nc 8
nop 1

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