StatRankings   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

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

1 Method

Rating   Name   Duplication   Size   Complexity  
B list() 0 44 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
    public function list($keywordID, Carbon $fromDate, Carbon $toDate): Collection
13
    {
14
        $start = 0;
15
        $rankings = collect();
16
17
        do {
18
            $response = $this->performQuery('rankings/list', ['keyword_id' => $keywordID, 'from_date' => $fromDate->toDateString(), 'to_date' => $toDate->toDateString(), 'start' => 0]);
19
            $start += 30;
20
21
            if ($response['totalresults'] == 0) {
22
                break;
23
            }
24
25
            if ($response['totalresults'] == 1) {
26
                $rankings->push($response['Result']);
27
            }
28
29
            if ($response['totalresults'] > 1) {
30
                $rankings = $rankings->merge($response['Result']);
31
            }
32
33
            if (! isset($response['nextpage'])) {
34
                break;
35
            }
36
        } while ($response['resultsreturned'] < $response['totalresults']);
37
38
        $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...
39
            return new StatKeywordRanking([
40
                'date' => $ranking['date'],
41
                'google' => new StatKeywordEngineRanking([
42
                    'rank' => $ranking['Google']['Rank'],
43
                    'base_rank' => $ranking['Google']['BaseRank'],
44
                    'url' => $ranking['Google']['Url'] ?? '',
45
                ]),
46
                'bing' => new StatKeywordEngineRanking([
47
                    'rank' => $ranking['Bing']['Rank'],
48
                    'url' => $ranking['Bing']['Url'] ?? '',
49
                    'base_rank' => $ranking['Bing']['BaseRank'],
50
                ]),
51
            ]);
52
        });
53
54
        return $rankings;
55
    }
56
}
57