Completed
Push — master ( ae4bd7...74685b )
by Felix
02:37
created

src/Api/StatSites.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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