PackagistApiServices   A
last analyzed

Complexity

Total Complexity 41

Size/Duplication

Total Lines 298
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 41
eloc 84
c 1
b 0
f 0
dl 0
loc 298
rs 9.1199

15 Methods

Rating   Name   Duplication   Size   Complexity  
A getPackageTotalRepo() 0 3 1
A getPackageMonthlyDownloads() 0 3 1
A getPackageTotalWatchers() 0 3 1
A getPackageTotalOpenIssues() 0 3 1
A getVendorPackagesCount() 0 7 2
A getPackagistVendorRepositoriesList() 0 24 5
A getPackageTotalStars() 0 3 1
A getPackageDailyDownloads() 0 3 1
A getVendorsTotalStars() 0 14 3
B getVendorsPackageDetails() 0 40 11
A getPackageTotalForks() 0 3 1
A getPackageDownloads() 0 13 3
A getPackageTotalDownloads() 0 3 1
A getVendorsTotalDownloads() 0 14 3
A getVendorsPackagesDetails() 0 24 6

How to fix   Complexity   

Complex Class

Complex classes like PackagistApiServices often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use PackagistApiServices, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace jeremykenedy\LaravelPackagist\App\Services;
4
5
use Illuminate\Support\Facades\Cache;
0 ignored issues
show
Bug introduced by
The type Illuminate\Support\Facades\Cache was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use jeremykenedy\LaravelPackagist\App\Traits\PackagistApiTrait;
7
8
class PackagistApiServices
9
{
10
    use PackagistApiTrait;
11
12
    /**
13
     * Gets the package downloads.
14
     *
15
     * @param string $vendorAndPackage The vendor and package
16
     *
17
     * @return int || string || array   The package total downloads.
0 ignored issues
show
Documentation Bug introduced by
The doc comment int || string || array at position 2 could not be parsed: Unknown type name '|' at position 2 in int || string || array.
Loading history...
18
     */
19
    public static function getPackageDownloads($vendorAndPackage = null, $type = null)
20
    {
21
        $downloads = self::getSpecificPackageDetail($vendorAndPackage, 'downloads');
22
23
        if (!is_array($downloads)) {
0 ignored issues
show
introduced by
The condition is_array($downloads) is always false.
Loading history...
24
            return $downloads;
25
        }
26
27
        if ($type) {
28
            return $downloads[$type];
29
        }
30
31
        return $downloads;
32
    }
33
34
    /**
35
     * Gets the package daily downloads.
36
     *
37
     * @param string $vendorAndPackage The vendor and package
38
     *
39
     * @return int || string        The package daily downloads.
0 ignored issues
show
Documentation Bug introduced by
The doc comment int || string at position 2 could not be parsed: Unknown type name '|' at position 2 in int || string.
Loading history...
40
     */
41
    public static function getPackageDailyDownloads($vendorAndPackage = null)
42
    {
43
        return self::getPackageDownloads($vendorAndPackage, 'daily');
44
    }
45
46
    /**
47
     * Gets the package monthly downloads.
48
     *
49
     * @param string $vendorAndPackage The vendor and package
50
     *
51
     * @return int || string        The package monthly downloads.
0 ignored issues
show
Documentation Bug introduced by
The doc comment int || string at position 2 could not be parsed: Unknown type name '|' at position 2 in int || string.
Loading history...
52
     */
53
    public static function getPackageMonthlyDownloads($vendorAndPackage = null)
54
    {
55
        return self::getPackageDownloads($vendorAndPackage, 'monthly');
56
    }
57
58
    /**
59
     * Gets the package total downloads.
60
     *
61
     * @param string $vendorAndPackage The vendor and package
62
     *
63
     * @return int || string        The package total downloads.
0 ignored issues
show
Documentation Bug introduced by
The doc comment int || string at position 2 could not be parsed: Unknown type name '|' at position 2 in int || string.
Loading history...
64
     */
65
    public static function getPackageTotalDownloads($vendorAndPackage = null)
66
    {
67
        return self::getPackageDownloads($vendorAndPackage, 'total');
68
    }
69
70
    /**
71
     * Gets the package total forks.
72
     *
73
     * @param string $vendorAndPackage The vendor and package
74
     *
75
     * @return int || string        The package total forks.
0 ignored issues
show
Documentation Bug introduced by
The doc comment int || string at position 2 could not be parsed: Unknown type name '|' at position 2 in int || string.
Loading history...
76
     */
77
    public static function getPackageTotalForks($vendorAndPackage = null)
78
    {
79
        return self::getSpecificPackageDetail($vendorAndPackage, 'github_forks');
80
    }
81
82
    /**
83
     * Gets the package total open issues.
84
     *
85
     * @param string $vendorAndPackage The vendor and package
86
     *
87
     * @return string The package open issues.
88
     */
89
    public static function getPackageTotalOpenIssues($vendorAndPackage = null)
90
    {
91
        return self::getSpecificPackageDetail($vendorAndPackage, 'github_open_issues');
92
    }
93
94
    /**
95
     * Gets the package repository.
96
     *
97
     * @param string $vendorAndPackage The vendor and package
98
     *
99
     * @return string The package repository.
100
     */
101
    public static function getPackageTotalRepo($vendorAndPackage = null)
102
    {
103
        return self::getSpecificPackageDetail($vendorAndPackage, 'repository');
104
    }
105
106
    /**
107
     * Gets the package total stars.
108
     *
109
     * @param string $vendorAndPackage The vendor and package
110
     *
111
     * @return int || string        The package total stars.
0 ignored issues
show
Documentation Bug introduced by
The doc comment int || string at position 2 could not be parsed: Unknown type name '|' at position 2 in int || string.
Loading history...
112
     */
113
    public static function getPackageTotalStars($vendorAndPackage = null)
114
    {
115
        return self::getSpecificPackageDetail($vendorAndPackage, 'github_stars');
116
    }
117
118
    /**
119
     * Gets the package total watchers.
120
     *
121
     * @param string $vendorAndPackage The vendor and package
122
     *
123
     * @return string The package watchers.
124
     */
125
    public static function getPackageTotalWatchers($vendorAndPackage = null)
126
    {
127
        return self::getSpecificPackageDetail($vendorAndPackage, 'github_watchers');
128
    }
129
130
    /**
131
     * Gets the packagist vendor repositories list.
132
     *
133
     * @param string $vendor The vendor
134
     *
135
     * @return collection The packagist vendor repositories list.
0 ignored issues
show
Bug introduced by
The type jeremykenedy\LaravelPack...App\Services\collection was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
136
     */
137
    public static function getPackagistVendorRepositoriesList($vendor = null)
138
    {
139
        if (!$vendor) {
140
            $vendor = config('laravelpackagist.vendor.default');
0 ignored issues
show
Bug introduced by
The function config was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

140
            $vendor = /** @scrutinizer ignore-call */ config('laravelpackagist.vendor.default');
Loading history...
141
        }
142
143
        $cachingEnabled = config('laravelpackagist.caching.enabled');
144
145
        if ($cachingEnabled) {
146
            $vendorKey = self::assignVendorCacheKey($vendor);
147
            if (self::checkIfItemIsCached($vendorKey)) {
148
                return Cache::get($vendorKey);
149
            }
150
        }
151
152
        $baseUrl = config('laravelpackagist.urls.vendorBase').$vendor;
153
        $response = self::curlPackagist($baseUrl);
154
        $list = collect(json_decode($response)->packageNames);
0 ignored issues
show
Bug introduced by
The function collect was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

154
        $list = /** @scrutinizer ignore-call */ collect(json_decode($response)->packageNames);
Loading history...
155
156
        if ($cachingEnabled) {
157
            Cache::put($vendorKey, $list, self::getVendorListCacheTime());
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $vendorKey does not seem to be defined for all execution paths leading up to this point.
Loading history...
158
        }
159
160
        return $list;
161
    }
162
163
    /**
164
     * Gets the vendor packages count.
165
     *
166
     * @param string $vendor The vendor
167
     *
168
     * @return int The vendor packages count.
169
     */
170
    public static function getVendorPackagesCount($vendor = null)
171
    {
172
        if (!$vendor) {
173
            $vendor = config('laravelpackagist.vendor.default');
0 ignored issues
show
Bug introduced by
The function config was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

173
            $vendor = /** @scrutinizer ignore-call */ config('laravelpackagist.vendor.default');
Loading history...
174
        }
175
176
        return self::getPackagistVendorRepositoriesList($vendor)->count();
177
    }
178
179
    /**
180
     * Gets the vendors packages details.
181
     *
182
     * @param string $vendor The vendor
183
     *
184
     * @return collection The vendors packages details.
185
     */
186
    public static function getVendorsPackagesDetails($vendor = null)
187
    {
188
        if (!$vendor) {
189
            $vendor = config('laravelpackagist.vendor.default');
0 ignored issues
show
Bug introduced by
The function config was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

189
            $vendor = /** @scrutinizer ignore-call */ config('laravelpackagist.vendor.default');
Loading history...
190
        }
191
192
        $projects = self::getPackagistVendorRepositoriesList($vendor);
193
        $vendorProjects = collect([]);
0 ignored issues
show
Bug introduced by
The function collect was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

193
        $vendorProjects = /** @scrutinizer ignore-call */ collect([]);
Loading history...
194
        $cachingEnabled = config('laravelpackagist.caching.enabled');
195
196
        foreach ($projects as $project) {
197
            if (self::checkIfItemIsCached($project) && $cachingEnabled) {
198
                $item = Cache::get($project);
199
            } else {
200
                $baseUrl = config('laravelpackagist.urls.projectPreFix').$project.config('laravelpackagist.urls.projectPostFix');
201
                $item = json_decode(self::curlPackagist($baseUrl))->package;
202
                if ($cachingEnabled) {
203
                    Cache::put($project, $item, self::getVendorListCacheTime());
204
                }
205
            }
206
            $vendorProjects[] = $item;
207
        }
208
209
        return $vendorProjects;
210
    }
211
212
    /**
213
     * Gets the vendors package details.
214
     *
215
     * @param string $vendorAndPackage The vendor and package as 'vendor/package'
216
     * @param bool   $object           Return as object
217
     *
218
     * @return object || Array The vendors package details.
0 ignored issues
show
Documentation Bug introduced by
The doc comment object || Array at position 2 could not be parsed: Unknown type name '|' at position 2 in object || Array.
Loading history...
219
     */
220
    public static function getVendorsPackageDetails($vendorAndPackage = null, $object = false)
221
    {
222
        if (!$vendorAndPackage) {
223
            return trans('laravelpackagist::laravelpackagist.missing-vendor-package');
0 ignored issues
show
Bug introduced by
The function trans was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

223
            return /** @scrutinizer ignore-call */ trans('laravelpackagist::laravelpackagist.missing-vendor-package');
Loading history...
224
        }
225
226
        if ((!strstr($vendorAndPackage, '/')) || (count(explode('/', $vendorAndPackage)) != 2)) {
227
            return trans('laravelpackagist::laravelpackagist.malformed-vendor-package');
228
        }
229
230
        $decode = true;
231
        if ($object === true) {
232
            $decode = false;
233
        }
234
235
        $packageFound = false;
236
        $packageDetails = trans('laravelpackagist::laravelpackagist.package-not-found');
237
        $cachingEnabled = config('laravelpackagist.caching.enabled');
0 ignored issues
show
Bug introduced by
The function config was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

237
        $cachingEnabled = /** @scrutinizer ignore-call */ config('laravelpackagist.caching.enabled');
Loading history...
238
239
        if (self::checkIfItemIsCached($vendorAndPackage) && $cachingEnabled) {
240
            $item = Cache::get($vendorAndPackage);
241
            $packageFound = true;
242
        } else {
243
            $baseUrl = config('laravelpackagist.urls.projectPreFix').$vendorAndPackage.config('laravelpackagist.urls.projectPostFix');
244
            $item = self::curlPackagist($baseUrl);
245
246
            if ($item != '{"status":"error","message":"Package not found"}') {
247
                $packageFound = true;
248
            }
249
250
            if ($packageFound && $cachingEnabled) {
251
                Cache::put($vendorAndPackage, $item, self::getVendorListCacheTime());
252
            }
253
        }
254
255
        if ($packageFound) {
256
            $packageDetails = json_decode(json_encode(json_decode($item)->package), $decode);
257
        }
258
259
        return $packageDetails;
260
    }
261
262
    /**
263
     * Gets the vendors total downloads.
264
     *
265
     * @param string $vendor The vendor
266
     *
267
     * @return int The vendors total downloads.
268
     */
269
    public static function getVendorsTotalDownloads($vendor = null)
270
    {
271
        if (!$vendor) {
272
            $vendor = config('laravelpackagist.vendor.default');
0 ignored issues
show
Bug introduced by
The function config was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

272
            $vendor = /** @scrutinizer ignore-call */ config('laravelpackagist.vendor.default');
Loading history...
273
        }
274
275
        $totalDownloads = 0;
276
        $vendorProjects = self::getVendorsPackagesDetails($vendor);
277
278
        foreach ($vendorProjects as $vendorProject) {
279
            $totalDownloads += $vendorProject->downloads->total;
280
        }
281
282
        return $totalDownloads;
283
    }
284
285
    /**
286
     * Gets the vendors total stars.
287
     *
288
     * @param string $vendor The vendor
289
     *
290
     * @return int The vendors total stars.
291
     */
292
    public static function getVendorsTotalStars($vendor = null)
293
    {
294
        if (!$vendor) {
295
            $vendor = config('laravelpackagist.vendor.default');
0 ignored issues
show
Bug introduced by
The function config was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

295
            $vendor = /** @scrutinizer ignore-call */ config('laravelpackagist.vendor.default');
Loading history...
296
        }
297
298
        $totalStars = 0;
299
        $vendorProjects = self::getVendorsPackagesDetails($vendor);
300
301
        foreach ($vendorProjects as $vendorProject) {
302
            $totalStars += $vendorProject->favers;
303
        }
304
305
        return $totalStars;
306
    }
307
}
308