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

StatRankings   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 5
dl 0
loc 63
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B list() 0 54 6
1
<?php
2
3
namespace SchulzeFelix\Stat\Api;
4
5
use Carbon\Carbon;
6
use Illuminate\Support\Collection;
7
use SchulzeFelix\Stat\Objects\StatKeywordEngineRanking;
8
use SchulzeFelix\Stat\Objects\StatKeywordRanking;
9
10
class StatRankings extends BaseStat
11
{
12
13
14
    public function list($keywordID, Carbon $fromDate, Carbon $toDate) : Collection
15
    {
16
17
        $start = 0;
18
        $rankings = collect();
19
20
        do {
21
            $response = $this->performQuery('rankings/list', ['keyword_id' => $keywordID, 'from_date' => $fromDate->toDateString(), 'to_date' => $toDate->toDateString(), 'start' => 0]);
22
            $start += 30;
23
24
            if($response['totalresults'] == 0) {
25
                break;
26
            }
27
28
            if($response['totalresults'] == 1) {
29
                $rankings->push($response['Result']);
30
            }
31
32
            if($response['totalresults'] > 1) {
33
                $rankings = $rankings->merge($response['Result']);
34
            }
35
36
37
            if (!isset($response['nextpage'])) {
38
                break;
39
            }
40
41
        } while ($response['resultsreturned'] < $response['totalresults']);
42
43
44
        $rankings = $rankings->transform(function ($ranking, $key) {
0 ignored issues
show
Unused Code introduced by
The parameter $key is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
45
46
            return new StatKeywordRanking([
47
                'date' => $ranking['date'],
48
                'google' => new StatKeywordEngineRanking([
49
                    'rank' => $ranking['Google']['Rank'],
50
                    'base_rank' => $ranking['Google']['BaseRank'],
51
                    'url' => $ranking['Google']['Url'] ?? '',
52
                ]),
53
                'yahoo' => new StatKeywordEngineRanking([
54
                    'rank' => $ranking['Yahoo']['Rank'],
55
                    'url' => $ranking['Yahoo']['Url'] ?? '',
56
                    'base_rank' => $ranking['Yahoo']['BaseRank'],
57
                ]),
58
                'bing' => new StatKeywordEngineRanking([
59
                    'rank' => $ranking['Bing']['Rank'],
60
                    'url' => $ranking['Bing']['Url'] ?? '',
61
                    'base_rank' => $ranking['Bing']['BaseRank'],
62
                ]),
63
            ]);
64
        });
65
66
        return $rankings;
67
    }
68
69
70
71
72
}
73