Passed
Push — master ( 8b0281...9e9f0c )
by Anthony
02:44
created

PackagistApi::getAllPackagistVersions()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 3
c 1
b 0
f 1
nc 2
nop 1
dl 0
loc 5
rs 10
1
<?php
2
3
namespace PiouPiou\RibsAdminBundle\Service;
4
5
use Exception;
6
use Symfony\Contracts\HttpClient\HttpClientInterface;
7
8
class PackagistApi
9
{
10
    /**
11
     * @var HttpClientInterface
12
     */
13
    private $client;
14
15
    /**
16
     * @var string
17
     */
18
    private $package_name = null;
19
20
    /**
21
     * @var array
22
     */
23
    private $package_info = [];
24
25
    /**
26
     * PackagistApi constructor.
27
     * @param HttpClientInterface $client
28
     */
29
    public function __construct(HttpClientInterface $client)
30
    {
31
        $this->client = $client;
32
    }
33
34
    /**
35
     * @return false|mixed
36
     */
37
    private function getPackageInformation()
38
    {
39
        if ($this->package_info) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->package_info of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
40
            return $this->package_info;
41
        }
42
43
        if (!$this->package_name || !strpos($this->package_name, "/")) {
44
            return false;
45
        }
46
47
        $packgist_url = "https://repo.packagist.org/p/".$this->package_name.".json";
48
        $response = $this->client->request("GET", $packgist_url);
49
50
        if ($response->getStatusCode() == 200) {
51
            $content = json_decode($response->getContent(), true);
52
            if (is_array($content) && $content["packages"] && $content["packages"][$this->package_name]) {
53
                $this->package_info = $content["packages"][$this->package_name];
54
                return $this->package_info;
55
            }
56
        }
57
58
        return false;
59
    }
60
61
    /**
62
     * @param string $package_name
63
     * @return false|int|string|null
64
     */
65
    public function getLastPackagistVersion(string $package_name)
66
    {
67
        $this->package_name = $package_name;
68
        if ($package = $this->getPackageInformation()) {
69
            return array_key_first($package);
70
        }
71
72
        return false;
73
    }
74
75
    public function getAllPackagistVersions(string $package_name)
76
    {
77
        $this->package_name = $package_name;
78
        if ($package = $this->getPackageInformation()) {
79
            return array_keys($package);
80
        }
81
    }
82
}
83