PackagistApiTrait   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 104
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 11
eloc 36
c 1
b 0
f 0
dl 0
loc 104
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getVendorListCacheTime() 0 7 2
A getSpecificPackageDetail() 0 9 2
A curlPackagist() 0 28 3
A checkIfItemIsCached() 0 13 3
A assignVendorCacheKey() 0 5 1
1
<?php
2
3
namespace jeremykenedy\LaravelPackagist\App\Traits;
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 Illuminate\Support\Facades\Log;
0 ignored issues
show
Bug introduced by
The type Illuminate\Support\Facades\Log 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...
7
8
trait PackagistApiTrait
9
{
10
    /**
11
     * Curl the Packagist API.
12
     *
13
     * @param string $baseUrl The base url
14
     *
15
     * @return object || string || null description_of_the_return_value
0 ignored issues
show
Documentation Bug introduced by
The doc comment object || string || null at position 2 could not be parsed: Unknown type name '|' at position 2 in object || string || null.
Loading history...
16
     */
17
    private static function curlPackagist($baseUrl)
18
    {
19
        $curl = curl_init();
20
        curl_setopt_array($curl, [
0 ignored issues
show
Bug introduced by
It seems like $curl can also be of type false; however, parameter $ch of curl_setopt_array() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

20
        curl_setopt_array(/** @scrutinizer ignore-type */ $curl, [
Loading history...
21
            CURLOPT_URL            => $baseUrl,
22
            CURLOPT_RETURNTRANSFER => true,
23
            CURLOPT_ENCODING       => '',
24
            CURLOPT_MAXREDIRS      => config('laravelpackagist.curl.maxredirects'),
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

24
            CURLOPT_MAXREDIRS      => /** @scrutinizer ignore-call */ config('laravelpackagist.curl.maxredirects'),
Loading history...
25
            CURLOPT_TIMEOUT        => config('laravelpackagist.curl.timeout'),
26
            CURLOPT_HTTP_VERSION   => CURL_HTTP_VERSION_1_1,
27
            CURLOPT_CUSTOMREQUEST  => 'GET',
28
            CURLOPT_HTTPHEADER     => [
29
                'Accept: application/json',
30
                'cache-control: no-cache',
31
            ],
32
        ]);
33
        $response = curl_exec($curl);
0 ignored issues
show
Bug introduced by
It seems like $curl can also be of type false; however, parameter $ch of curl_exec() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

33
        $response = curl_exec(/** @scrutinizer ignore-type */ $curl);
Loading history...
34
        $err = curl_error($curl);
0 ignored issues
show
Bug introduced by
It seems like $curl can also be of type false; however, parameter $ch of curl_error() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

34
        $err = curl_error(/** @scrutinizer ignore-type */ $curl);
Loading history...
35
        curl_close($curl);
0 ignored issues
show
Bug introduced by
It seems like $curl can also be of type false; however, parameter $ch of curl_close() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

35
        curl_close(/** @scrutinizer ignore-type */ $curl);
Loading history...
36
        if ($err) {
37
            if (config('laravelpackagist.logging.curlErrors')) {
38
                Log::error($err);
39
            }
40
41
            return;
42
        }
43
44
        return $response;
45
    }
46
47
    /**
48
     * Check if packagist vendor list exists in the cache.
49
     *
50
     * @return bool
51
     */
52
    private static function checkIfItemIsCached($key = null)
53
    {
54
        $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

54
        $cachingEnabled = /** @scrutinizer ignore-call */ config('laravelpackagist.caching.enabled');
Loading history...
55
56
        if (!$cachingEnabled) {
57
            return false;
58
        }
59
60
        if (Cache::has($key)) {
61
            return true;
62
        }
63
64
        return false;
65
    }
66
67
    /**
68
     * Set the vendor cache key.
69
     *
70
     * @param string $key The key
71
     */
72
    private static function assignVendorCacheKey($key)
73
    {
74
        $keyPlug = 'packagistVendorKey';
75
76
        return $key.$keyPlug;
77
    }
78
79
    /**
80
     * Gets the specific package detail.
81
     *
82
     * @param string $vendorAndPackage The vendor and package
83
     * @param string $detail           The detail
84
     *
85
     * @return string The specific package detail.
86
     */
87
    private static function getSpecificPackageDetail($vendorAndPackage, $detail = null)
88
    {
89
        $packageDetails = self::getVendorsPackageDetails($vendorAndPackage);
90
91
        if (!is_array($packageDetails)) {
92
            return $packageDetails;
93
        }
94
95
        return $packageDetails[$detail];
96
    }
97
98
    /**
99
     * Gets the vendor list cache time.
100
     *
101
     * @param int $minutes The Minutes
102
     *
103
     * @return dateTime The vendor list cache time.
0 ignored issues
show
Bug introduced by
The type jeremykenedy\LaravelPackagist\App\Traits\dateTime was not found. Did you mean dateTime? If so, make sure to prefix the type with \.
Loading history...
104
     */
105
    private static function getVendorListCacheTime($minutes = null)
106
    {
107
        if ($minutes === null) {
108
            $minutes = config('laravelpackagist.caching.vendorListCacheTime');
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

108
            $minutes = /** @scrutinizer ignore-call */ config('laravelpackagist.caching.vendorListCacheTime');
Loading history...
109
        }
110
111
        return now()->addMinutes($minutes);
0 ignored issues
show
Bug introduced by
The function now 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

111
        return /** @scrutinizer ignore-call */ now()->addMinutes($minutes);
Loading history...
112
    }
113
}
114