1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SchulzeFelix\Stat\Api; |
4
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
6
|
|
|
use Illuminate\Support\Collection; |
7
|
|
|
use SchulzeFelix\Stat\Objects\StatSite; |
8
|
|
|
|
9
|
|
|
class StatSites extends BaseStat |
10
|
|
|
{ |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @return Collection |
14
|
|
|
*/ |
15
|
|
|
public function all() : Collection |
16
|
|
|
{ |
17
|
|
|
$start = 0; |
18
|
|
|
$sites = collect(); |
19
|
|
|
|
20
|
|
|
do { |
21
|
|
|
$response = $this->performQuery('sites/all', ['start' => $start, 'results' => 5000]); |
22
|
|
|
$start += 5000; |
23
|
|
|
$sites = $sites->merge($response['Result']); |
24
|
|
|
|
25
|
|
|
if (!isset($response['nextpage'])) { |
26
|
|
|
break; |
27
|
|
|
} |
28
|
|
|
} while ($response['resultsreturned'] < $response['totalresults']); |
29
|
|
|
|
30
|
|
|
|
31
|
|
View Code Duplication |
$sites->transform(function ($site) { |
|
|
|
|
32
|
|
|
return new StatSite([ |
33
|
|
|
'id' => $site['Id'], |
34
|
|
|
'project_id' => $site['ProjectId'], |
35
|
|
|
'folder_id' => $site['FolderId'], |
36
|
|
|
'folder_name' => $site['FolderName'], |
37
|
|
|
'title' => $site['Title'], |
38
|
|
|
'url' => $site['Url'], |
39
|
|
|
'synced' => $site['Synced'], |
40
|
|
|
'total_keywords' => $site['TotalKeywords'], |
41
|
|
|
'created_at' => $site['CreatedAt'], |
42
|
|
|
'updated_at' => $site['UpdatedAt'], |
43
|
|
|
]); |
44
|
|
|
}); |
45
|
|
|
|
46
|
|
|
return $sites; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param $projectID |
51
|
|
|
* @return Collection |
52
|
|
|
*/ |
53
|
|
|
public function list($projectID) : Collection |
54
|
|
|
{ |
55
|
|
|
$response = $this->performQuery('sites/list', ['project_id' => $projectID]); |
56
|
|
|
|
57
|
|
|
$sites = collect(); |
58
|
|
|
if ($response['resultsreturned'] == 0) { |
59
|
|
|
return $sites; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
if ($response['resultsreturned'] == 1) { |
63
|
|
|
$sites->push($response['Result']); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
if ($response['resultsreturned'] > 1) { |
67
|
|
|
$sites = collect($response['Result']); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
View Code Duplication |
$sites->transform(function ($site) use ($projectID) { |
|
|
|
|
71
|
|
|
return new StatSite([ |
72
|
|
|
'id' => $site['Id'], |
73
|
|
|
'project_id' => $projectID, |
74
|
|
|
'folder_id' => $site['FolderId'], |
75
|
|
|
'folder_name' => $site['FolderName'], |
76
|
|
|
'title' => $site['Title'], |
77
|
|
|
'url' => $site['Url'], |
78
|
|
|
'synced' => $site['Synced'], |
79
|
|
|
'total_keywords' => $site['TotalKeywords'], |
80
|
|
|
'created_at' => $site['CreatedAt'], |
81
|
|
|
'updated_at' => $site['UpdatedAt'], |
82
|
|
|
]); |
83
|
|
|
}); |
84
|
|
|
|
85
|
|
|
return $sites; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param $siteID |
90
|
|
|
* @param Carbon $fromDate |
91
|
|
|
* @param Carbon $toDate |
92
|
|
|
* @return Collection |
93
|
|
|
*/ |
94
|
|
View Code Duplication |
public function rankingDistributions($siteID, Carbon $fromDate, Carbon $toDate) : Collection |
|
|
|
|
95
|
|
|
{ |
96
|
|
|
$this->checkMaximumDateRange($fromDate, $toDate); |
97
|
|
|
|
98
|
|
|
$response = $this->performQuery('sites/ranking_distributions', ['id' => $siteID, 'from_date' => $fromDate->toDateString(), 'to_date' => $toDate->toDateString()]); |
99
|
|
|
|
100
|
|
|
$rankDistribution = collect($response['RankDistribution']); |
101
|
|
|
|
102
|
|
|
if (isset($response['RankDistribution']['date'])) { |
103
|
|
|
$rankDistribution = collect([$response['RankDistribution']]); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
$rankDistribution->transform(function ($distribution) { |
107
|
|
|
return $this->transformRankDistribution($distribution); |
108
|
|
|
}); |
109
|
|
|
|
110
|
|
|
return $rankDistribution; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @param $projectID |
115
|
|
|
* @param $url |
116
|
|
|
* @param bool $dropWWWprefix |
117
|
|
|
* @param bool $dropDirectories |
118
|
|
|
* @return StatSite |
119
|
|
|
*/ |
120
|
|
|
public function create($projectID, $url, $dropWWWprefix = true, $dropDirectories = true) |
121
|
|
|
{ |
122
|
|
|
$response = $this->performQuery('sites/create', ['project_id' => $projectID, 'url' => rawurlencode($url), 'drop_www_prefix' => ($dropWWWprefix) ?: 0, 'drop_directories' => ($dropDirectories) ?: 0]); |
123
|
|
|
|
124
|
|
|
return new StatSite([ |
125
|
|
|
'id' => $response['Result']['Id'], |
126
|
|
|
'project_id' => $response['Result']['ProjectId'], |
127
|
|
|
'title' => $response['Result']['Title'], |
128
|
|
|
'url' => $response['Result']['Url'], |
129
|
|
|
'drop_www_prefix' => $response['Result']['DropWWWPrefix'], |
130
|
|
|
'drop_directories' => $response['Result']['DropDirectories'], |
131
|
|
|
'created_at' => $response['Result']['CreatedAt'], |
132
|
|
|
]); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @param $siteID |
137
|
|
|
* @param string|null $title |
138
|
|
|
* @param string|null $url |
139
|
|
|
* @param bool|null $dropWWWprefix |
140
|
|
|
* @param bool|null $dropDirectories |
141
|
|
|
* @return StatSite |
142
|
|
|
*/ |
143
|
|
|
public function update($siteID, $title = null, $url = null, $dropWWWprefix = null, $dropDirectories = null) |
144
|
|
|
{ |
145
|
|
|
$arguments = []; |
146
|
|
|
$arguments['id'] = $siteID; |
147
|
|
|
|
148
|
|
|
if(!is_null($title)){ |
149
|
|
|
$arguments['title'] = rawurlencode($title); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
if(!is_null($url)){ |
153
|
|
|
$arguments['url'] = rawurlencode($url); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
if(!is_null($dropWWWprefix)){ |
157
|
|
|
$arguments['drop_www_prefix'] = ($dropWWWprefix) ?: 0; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
if(!is_null($dropDirectories)){ |
161
|
|
|
$arguments['drop_directories'] = ($dropDirectories) ?: 0; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
$response = $this->performQuery('sites/update', $arguments); |
165
|
|
|
|
166
|
|
|
return new StatSite([ |
167
|
|
|
'id' => $response['Result']['Id'], |
168
|
|
|
'project_id' => $response['Result']['ProjectId'], |
169
|
|
|
'title' => $response['Result']['Title'], |
170
|
|
|
'url' => $response['Result']['Url'], |
171
|
|
|
'drop_www_prefix' => $response['Result']['DropWWWPrefix'], |
172
|
|
|
'drop_directories' => $response['Result']['DropDirectories'], |
173
|
|
|
'created_at' => $response['Result']['CreatedAt'], |
174
|
|
|
'updated_at' => (isset($response['Result']['UpdatedAt'])) ? $response['Result']['UpdatedAt'] : $response['Result']['CreatedAt'], |
175
|
|
|
]); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* @param $siteID |
180
|
|
|
* @return int |
181
|
|
|
*/ |
182
|
|
|
public function delete($siteID) |
183
|
|
|
{ |
184
|
|
|
$response = $this->performQuery('sites/delete', ['id' => $siteID]); |
185
|
|
|
|
186
|
|
|
return (int)$response['Result']['Id']; |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
|
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.