Conditions | 40 |
Paths | > 20000 |
Total Lines | 228 |
Code Lines | 181 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
34 | public function __invoke(RequestInterface $request, ResponseInterface $response): AppDetail |
||
35 | { |
||
36 | $url = $request->getUri()->__toString(); |
||
37 | $urlComponents = parse_url($url); |
||
38 | $query = parse_query($urlComponents['query']); |
||
39 | $appId = $query[GPlayApps::REQ_PARAM_APP_ID]; |
||
40 | $url = $urlComponents['scheme'] . '://' |
||
41 | . $urlComponents['host'] |
||
42 | . $urlComponents['path'] |
||
43 | . '?' . http_build_query([GPlayApps::REQ_PARAM_APP_ID => $appId]); |
||
44 | $locale = $query[GPlayApps::REQ_PARAM_LOCALE] ?? GPlayApps::DEFAULT_LOCALE; |
||
45 | |||
46 | $scriptData = ScraperUtil::extractScriptData($response->getBody()->getContents()); |
||
47 | |||
48 | $scriptDataInfo = null; |
||
49 | $scriptDataRating = null; |
||
50 | $scriptDataPrice = null; |
||
51 | $scriptDataVersion = null; |
||
52 | $scriptDataReviews = []; |
||
53 | |||
54 | foreach ($scriptData as $key => $scriptValue) { |
||
55 | if (isset($scriptValue[0][12][5][5][4][2])) { |
||
56 | $scriptDataInfo = $scriptValue; |
||
57 | } elseif (isset($scriptValue[0][2][0][0][0][1][0][0])) { |
||
58 | $scriptDataPrice = $scriptValue; |
||
59 | } elseif (isset($scriptValue[0][0][0]) |
||
60 | && is_string($scriptValue[0][0][0]) |
||
61 | && strpos($scriptValue[0][0][0], 'gp:') === 0) { |
||
62 | $scriptDataReviews = $scriptValue; |
||
63 | } elseif (isset($scriptValue[0][6][3][1])) { |
||
64 | $scriptDataRating = $scriptValue; |
||
65 | } elseif (isset($scriptValue[0]) |
||
66 | && is_string($scriptValue[0]) |
||
67 | && count($scriptValue) === 3) { |
||
68 | $scriptDataVersion = $scriptValue; |
||
69 | } |
||
70 | } |
||
71 | |||
72 | if ( |
||
73 | $scriptDataInfo === null || |
||
74 | $scriptDataRating === null || |
||
75 | $scriptDataPrice === null || |
||
76 | $scriptDataVersion === null |
||
|
|||
77 | ) { |
||
78 | throw (new GooglePlayException('Unable to get data for this application.'))->setUrl($url); |
||
79 | } |
||
80 | |||
81 | $name = $scriptDataInfo[0][0][0]; |
||
82 | $descriptionHTML = $scriptDataInfo[0][10][0][1]; |
||
83 | $description = ScraperUtil::html2text($descriptionHTML); |
||
84 | |||
85 | $developerPage = GPlayApps::GOOGLE_PLAY_URL . $scriptDataInfo[0][12][5][5][4][2]; |
||
86 | $developerId = parse_query(parse_url($developerPage, PHP_URL_QUERY))['id']; |
||
87 | $developerName = $scriptDataInfo[0][12][5][1]; |
||
88 | $developerEmail = $scriptDataInfo[0][12][5][2][0]; |
||
89 | $developerWebsite = $scriptDataInfo[0][12][5][3][5][2]; |
||
90 | $developerAddress = $scriptDataInfo[0][12][5][4][0]; |
||
91 | // $developerInternalID = (int)$scriptDataInfo[0][12][5][0][0]; |
||
92 | |||
93 | $genreId = $scriptDataInfo[0][12][13][0][2]; |
||
94 | $genreName = $scriptDataInfo[0][12][13][0][0]; |
||
95 | |||
96 | $summary = empty($scriptDataInfo[0][10][1][1]) ? |
||
97 | null : |
||
98 | ScraperUtil::html2text($scriptDataInfo[0][10][1][1]); |
||
99 | |||
100 | $installs = $scriptDataInfo[0][12][9][2] ?? 0; |
||
101 | $score = (float)($scriptDataRating[0][6][0][1] ?? 0); |
||
102 | $numberVoters = (int)($scriptDataRating[0][6][2][1] ?? 0); |
||
103 | $reviewsCount = (int)($scriptDataRating[0][6][3][1] ?? 0); |
||
104 | $histogram = $scriptDataRating[0][6][1] ?? null; |
||
105 | |||
106 | $histogramRating = new HistogramRating( |
||
107 | $histogram[5][1] ?? 0, |
||
108 | $histogram[4][1] ?? 0, |
||
109 | $histogram[3][1] ?? 0, |
||
110 | $histogram[2][1] ?? 0, |
||
111 | $histogram[1][1] ?? 0 |
||
112 | ); |
||
113 | |||
114 | $price = isset($scriptDataPrice[0][2][0][0][0][1][0][0]) ? |
||
115 | (float)($scriptDataPrice[0][2][0][0][0][1][0][0] / 1000000) : |
||
116 | 0; |
||
117 | $currency = $scriptDataPrice[0][2][0][0][0][1][0][1]; |
||
118 | $priceText = $scriptDataPrice[0][2][0][0][0][1][0][2] ?: 'Free'; |
||
119 | $offersIAPCost = $scriptDataInfo[0][12][12][0] ?? null; |
||
120 | $adSupported = (bool)$scriptDataInfo[0][12][14][0]; |
||
121 | |||
122 | [$size, $appVersion, $androidVersion] = $scriptDataVersion; |
||
123 | if (LocaleHelper::isDependOnDevice($locale, $size)) { |
||
124 | $size = null; |
||
125 | } |
||
126 | if (LocaleHelper::isDependOnDevice($locale, $appVersion)) { |
||
127 | $appVersion = null; |
||
128 | } |
||
129 | if (LocaleHelper::isDependOnDevice($locale, $androidVersion)) { |
||
130 | $androidVersion = null; |
||
131 | $minAndroidVersion = null; |
||
132 | } else { |
||
133 | $minAndroidVersion = preg_replace('~.*?(\d+(\.\d+)?).*~', '$1', $androidVersion); |
||
134 | } |
||
135 | |||
136 | $editorsChoice = !empty($scriptDataInfo[0][12][15][1][1]); |
||
137 | $privacyPoliceUrl = $scriptDataInfo[0][12][7][2]; |
||
138 | |||
139 | $familyGenreId = null; |
||
140 | $familyGenreName = null; |
||
141 | if ( |
||
142 | isset($scriptDataInfo[0][12][13][1][0]) && |
||
143 | $scriptDataInfo[0][12][13][1][0] !== null && |
||
144 | $scriptDataInfo[0][12][13][1][2] !== null |
||
145 | ) { |
||
146 | $familyGenreId = (string)$scriptDataInfo[0][12][13][1][2]; |
||
147 | $familyGenreName = (string)$scriptDataInfo[0][12][13][1][0]; |
||
148 | } |
||
149 | |||
150 | $icon = empty($scriptDataInfo[0][12][1][3][2]) ? |
||
151 | null : |
||
152 | new GoogleImage($scriptDataInfo[0][12][1][3][2]); |
||
153 | |||
154 | $headerImage = empty($scriptDataInfo[0][12][2][3][2]) ? |
||
155 | null : |
||
156 | new GoogleImage($scriptDataInfo[0][12][2][3][2]); |
||
157 | |||
158 | $screenshots = !empty($scriptDataInfo[0][12][0]) ? array_map(static function (array $v) { |
||
159 | return new GoogleImage($v[3][2]); |
||
160 | }, $scriptDataInfo[0][12][0]) : []; |
||
161 | |||
162 | $videoThumb = null; |
||
163 | $videoUrl = null; |
||
164 | if ( |
||
165 | isset($scriptDataInfo[0][12][3][0][3][2]) && |
||
166 | $scriptDataInfo[0][12][3][0][3][2] !== null && |
||
167 | $scriptDataInfo[0][12][3][1][3][2] !== null |
||
168 | ) { |
||
169 | $videoThumb = (string)$scriptDataInfo[0][12][3][1][3][2]; |
||
170 | $videoUrl = (string)$scriptDataInfo[0][12][3][0][3][2]; |
||
171 | } |
||
172 | |||
173 | $contentRating = $scriptDataInfo[0][12][4][0]; |
||
174 | $released = null; |
||
175 | if (isset($scriptDataInfo[0][12][36]) && $scriptDataInfo[0][12][36] !== null) { |
||
176 | $released = DateStringFormatter::formatted($locale, $scriptDataInfo[0][12][36]); |
||
177 | } |
||
178 | try { |
||
179 | $updated = !empty($scriptDataInfo[0][12][8][0]) ? |
||
180 | new \DateTimeImmutable('@' . $scriptDataInfo[0][12][8][0]) |
||
181 | : null; |
||
182 | } catch (\Exception $e) { |
||
183 | $updated = null; |
||
184 | } |
||
185 | |||
186 | $recentChanges = empty($scriptDataInfo[0][12][6][1]) ? |
||
187 | null : |
||
188 | ScraperUtil::html2text($scriptDataInfo[0][12][6][1]); |
||
189 | |||
190 | $translatedFromLanguage = null; |
||
191 | $translatedDescription = null; |
||
192 | if (isset($scriptDataInfo[0][19][1])) { |
||
193 | $translatedFromLanguage = LocaleHelper::findPreferredLanguage( |
||
194 | $locale, |
||
195 | $scriptDataInfo[0][19][1] |
||
196 | ); |
||
197 | $translatedDescription = ScraperUtil::html2text($scriptDataInfo[0][19][0][0][1]); |
||
198 | } |
||
199 | |||
200 | $reviews = $this->extractReviews($url, $scriptDataReviews); |
||
201 | |||
202 | $developerBuilder = Developer::newBuilder() |
||
203 | ->setId($developerId) |
||
204 | ->setUrl($developerPage) |
||
205 | ->setName($developerName) |
||
206 | ->setEmail($developerEmail) |
||
207 | ->setAddress($developerAddress) |
||
208 | ->setWebsite($developerWebsite); |
||
209 | |||
210 | $appBuilder = AppDetail::newBuilder() |
||
211 | ->setId($appId) |
||
212 | ->setLocale($locale) |
||
213 | ->setName($name) |
||
214 | ->setDescription($description) |
||
215 | ->setTranslated($translatedDescription, $translatedFromLanguage) |
||
216 | ->setSummary($summary) |
||
217 | ->setIcon($icon) |
||
218 | ->setHeaderImage($headerImage) |
||
219 | ->setScreenshots($screenshots) |
||
220 | ->setDeveloper(new Developer($developerBuilder)) |
||
221 | ->setCategory(new Category( |
||
222 | $genreId, |
||
223 | $genreName |
||
224 | )) |
||
225 | ->setCategoryFamily( |
||
226 | $familyGenreId !== null && $familyGenreName !== null ? |
||
227 | new Category($familyGenreId, $familyGenreName) : |
||
228 | null |
||
229 | ) |
||
230 | ->setVideo( |
||
231 | $videoThumb !== null && $videoUrl !== null ? |
||
232 | new Video($videoThumb, $videoUrl) : |
||
233 | null |
||
234 | ) |
||
235 | ->setRecentChanges($recentChanges) |
||
236 | ->setEditorsChoice($editorsChoice) |
||
237 | ->setPrivacyPoliceUrl($privacyPoliceUrl) |
||
238 | ->setInstalls($installs) |
||
239 | ->setScore($score) |
||
240 | ->setRecentChanges($recentChanges) |
||
241 | ->setEditorsChoice($editorsChoice) |
||
242 | ->setPrivacyPoliceUrl($privacyPoliceUrl) |
||
243 | ->setInstalls($installs) |
||
244 | ->setScore($score) |
||
245 | ->setNumberVoters($numberVoters) |
||
246 | ->setHistogramRating($histogramRating) |
||
247 | ->setPrice($price) |
||
248 | ->setCurrency($currency) |
||
249 | ->setPriceText($priceText) |
||
250 | ->setOffersIAPCost($offersIAPCost) |
||
251 | ->setAdSupported($adSupported) |
||
252 | ->setAppSize($size) |
||
253 | ->setAppVersion($appVersion) |
||
254 | ->setAndroidVersion($androidVersion) |
||
255 | ->setMinAndroidVersion($minAndroidVersion) |
||
256 | ->setContentRating($contentRating) |
||
257 | ->setReleased($released) |
||
258 | ->setUpdated($updated) |
||
259 | ->setReviewsCount($reviewsCount) |
||
260 | ->setReviews($reviews); |
||
261 | return new AppDetail($appBuilder); |
||
262 | } |
||
324 |