StatSites::list()   A
last analyzed

Complexity

Conditions 4
Paths 5

Size

Total Lines 34

Duplication

Lines 14
Ratio 41.18 %

Importance

Changes 0
Metric Value
dl 14
loc 34
rs 9.376
c 0
b 0
f 0
cc 4
nc 5
nop 1
1
<?php
2
3
namespace SchulzeFelix\Stat\Api;
4
5
use Carbon\Carbon;
6
use Illuminate\Support\Arr;
7
use Illuminate\Support\Collection;
8
use SchulzeFelix\Stat\Objects\StatFrequentDomain;
9
use SchulzeFelix\Stat\Objects\StatShareOfVoice;
10
use SchulzeFelix\Stat\Objects\StatShareOfVoiceSite;
11
use SchulzeFelix\Stat\Objects\StatSite;
12
13
class StatSites extends BaseStat
14
{
15
    /**
16
     * @return Collection
17
     */
18
    public function all(): Collection
19
    {
20
        $start = 0;
21
        $sites = collect();
22
23
        do {
24
            $response = $this->performQuery('sites/all', ['start' => $start, 'results' => 5000]);
25
            $start += 5000;
26
27
            if ($response['totalresults'] == 1) {
28
                $response['Result'] = [$response['Result']];
29
            }
30
31
            $sites = $sites->merge($response['Result']);
32
33
            if (! isset($response['nextpage'])) {
34
                break;
35
            }
36
        } while ($response['resultsreturned'] < $response['totalresults']);
37
38 View Code Duplication
        $sites->transform(function ($site) {
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...
39
            return new StatSite([
40
                'id' => $site['Id'],
41
                'project_id' => $site['ProjectId'],
42
                'folder_id' => $site['FolderId'],
43
                'folder_name' => $site['FolderName'],
44
                'title' => $site['Title'],
45
                'url' => $site['Url'],
46
                'synced' => $site['Synced'],
47
                'total_keywords' => $site['TotalKeywords'],
48
                'created_at' => $site['CreatedAt'],
49
                'updated_at' => $site['UpdatedAt'],
50
            ]);
51
        });
52
53
        return $sites;
54
    }
55
56
    /**
57
     * @param $projectID
58
     * @return Collection
59
     */
60
    public function list($projectID): Collection
61
    {
62
        $response = $this->performQuery('sites/list', ['project_id' => $projectID]);
63
64
        $sites = collect();
65
        if ($response['resultsreturned'] == 0) {
66
            return $sites;
67
        }
68
69
        if ($response['resultsreturned'] == 1) {
70
            $sites->push($response['Result']);
71
        }
72
73
        if ($response['resultsreturned'] > 1) {
74
            $sites = collect($response['Result']);
75
        }
76
77 View Code Duplication
        $sites->transform(function ($site) use ($projectID) {
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...
78
            return new StatSite([
79
                'id' => $site['Id'],
80
                'project_id' => $projectID,
81
                'folder_id' => $site['FolderId'],
82
                'folder_name' => $site['FolderName'],
83
                'title' => $site['Title'],
84
                'url' => $site['Url'],
85
                'synced' => $site['Synced'],
86
                'total_keywords' => $site['TotalKeywords'],
87
                'created_at' => $site['CreatedAt'],
88
                'updated_at' => $site['UpdatedAt'],
89
            ]);
90
        });
91
92
        return $sites;
93
    }
94
95
    /**
96
     * @param $siteID
97
     * @param Carbon $fromDate
98
     * @param Carbon $toDate
99
     * @return Collection
100
     */
101 View Code Duplication
    public function rankingDistributions($siteID, Carbon $fromDate, Carbon $toDate): Collection
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...
102
    {
103
        $this->checkMaximumDateRange($fromDate, $toDate);
104
105
        $response = $this->performQuery('sites/ranking_distributions', ['id' => $siteID, 'from_date' => $fromDate->toDateString(), 'to_date' => $toDate->toDateString()]);
106
107
        $rankDistribution = collect($response['RankDistribution']);
108
109
        if (isset($response['RankDistribution']['date'])) {
110
            $rankDistribution = collect([$response['RankDistribution']]);
111
        }
112
113
        $rankDistribution->transform(function ($distribution) {
114
            return $this->transformRankDistribution($distribution);
115
        });
116
117
        return $rankDistribution;
118
    }
119
120
    /**
121
     * @param $projectID
122
     * @param $url
123
     * @param bool $dropWWWprefix
124
     * @param bool $dropDirectories
125
     * @return StatSite
126
     */
127
    public function create($projectID, $url, $dropWWWprefix = true, $dropDirectories = true)
128
    {
129
        $response = $this->performQuery('sites/create', ['project_id' => $projectID, 'url' => rawurlencode($url), 'drop_www_prefix' => ($dropWWWprefix) ?: 0, 'drop_directories' => ($dropDirectories) ?: 0]);
130
131
        return new StatSite([
132
            'id' => $response['Result']['Id'],
133
            'project_id' => $response['Result']['ProjectId'],
134
            'title' => $response['Result']['Title'],
135
            'url' => $response['Result']['Url'],
136
            'drop_www_prefix' => $response['Result']['DropWWWPrefix'],
137
            'drop_directories' => $response['Result']['DropDirectories'],
138
            'created_at' => $response['Result']['CreatedAt'],
139
        ]);
140
    }
141
142
    /**
143
     * @param $siteID
144
     * @param string|null $title
145
     * @param string|null $url
146
     * @param bool|null $dropWWWprefix
147
     * @param bool|null $dropDirectories
148
     * @return StatSite
149
     */
150
    public function update($siteID, $title = null, $url = null, $dropWWWprefix = null, $dropDirectories = null)
151
    {
152
        $arguments = [];
153
        $arguments['id'] = $siteID;
154
155
        if (! is_null($title)) {
156
            $arguments['title'] = rawurlencode($title);
157
        }
158
159
        if (! is_null($url)) {
160
            $arguments['url'] = rawurlencode($url);
161
        }
162
163
        if (! is_null($dropWWWprefix)) {
164
            $arguments['drop_www_prefix'] = ($dropWWWprefix) ?: 0;
165
        }
166
167
        if (! is_null($dropDirectories)) {
168
            $arguments['drop_directories'] = ($dropDirectories) ?: 0;
169
        }
170
171
        $response = $this->performQuery('sites/update', $arguments);
172
173
        return new StatSite([
174
            'id' => $response['Result']['Id'],
175
            'project_id' => $response['Result']['ProjectId'],
176
            'title' => $response['Result']['Title'],
177
            'url' => $response['Result']['Url'],
178
            'drop_www_prefix' => $response['Result']['DropWWWPrefix'],
179
            'drop_directories' => $response['Result']['DropDirectories'],
180
            'created_at' => $response['Result']['CreatedAt'],
181
            'updated_at' => (isset($response['Result']['UpdatedAt'])) ? $response['Result']['UpdatedAt'] : $response['Result']['CreatedAt'],
182
        ]);
183
    }
184
185
    /**
186
     * @param $siteID
187
     * @return int
188
     */
189
    public function delete($siteID)
190
    {
191
        $response = $this->performQuery('sites/delete', ['id' => $siteID]);
192
193
        return (int) $response['Result']['Id'];
194
    }
195
196
    /**
197
     * @param $siteID
198
     * @param Carbon $fromDate
199
     * @param Carbon $toDate
200
     * @return Collection
201
     */
202 View Code Duplication
    public function sov($siteID, Carbon $fromDate, Carbon $toDate): Collection
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...
203
    {
204
        $start = 0;
205
        $sovSites = collect();
206
207
        do {
208
            $response = $this->performQuery('sites/sov', ['id' => $siteID, 'from_date' => $fromDate->toDateString(), 'to_date' => $toDate->toDateString(), 'start' => $start, 'results' => 5000]);
209
            $start += 5000;
210
            $sovSites = $sovSites->merge($response['ShareOfVoice']);
211
212
            if (! isset($response['nextpage'])) {
213
                break;
214
            }
215
        } while ($response['resultsreturned'] < $response['totalresults']);
216
217
        $sovSites->transform(function ($sov) {
218
            $shareOfVoice = new StatShareOfVoice([
219
                'date' => $sov['date'],
220
                'sites' => collect($sov['Site'])->transform(function ($site) {
221
                    return new StatShareOfVoiceSite([
222
                        'domain' => $site['Domain'],
223
                        'share' => (float) $site['Share'],
224
                        'pinned' => filter_var(Arr::get($site, 'Pinned'), FILTER_VALIDATE_BOOLEAN),
225
                    ]);
226
                }),
227
            ]);
228
229
            return $shareOfVoice;
230
        });
231
232
        return $sovSites;
233
    }
234
235
    /**
236
     * @param $siteID
237
     * @param string $engine
238
     * @return Collection
239
     */
240 View Code Duplication
    public function mostFrequentDomains($siteID, $engine = 'google')
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...
241
    {
242
        $response = $this->performQuery('sites/most_frequent_domains', ['id' => $siteID, 'engine' => $engine]);
243
244
        $domains = collect($response['Site'])->transform(function ($site) {
245
            return new StatFrequentDomain([
246
                'domain'           => Arr::get($site, 'Domain'),
247
                'top_ten_results'  => Arr::get($site, 'TopTenResults'),
248
                'results_analyzed' => Arr::get($site, 'ResultsAnalyzed'),
249
                'coverage'         => Arr::get($site, 'Coverage'),
250
                'analyzed_on'      => Arr::get($site, 'AnalyzedOn'),
251
            ]);
252
        });
253
254
        return $domains;
255
    }
256
}
257