Issues (62)

Service/PackagistApi.php (1 issue)

1
<?php
2
3
namespace PiouPiou\RibsAdminBundle\Service;
4
5
use Exception;
6
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
7
use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
8
use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
9
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
10
use Symfony\Contracts\HttpClient\HttpClientInterface;
11
12
class PackagistApi
13
{
14
    /**
15
     * @var HttpClientInterface
16
     */
17
    private $client;
18
19
    /**
20
     * @var string
21
     */
22
    private $package_name = null;
23
24
    /**
25
     * @var array
26
     */
27
    private $package_info = [];
28
29
    /**
30
     * PackagistApi constructor.
31
     * @param HttpClientInterface $client
32
     */
33
    public function __construct(HttpClientInterface $client)
34
    {
35
        $this->client = $client;
36
    }
37
38
    /**
39
     * @return false|mixed
40
     * @throws ClientExceptionInterface
41
     * @throws RedirectionExceptionInterface
42
     * @throws ServerExceptionInterface
43
     * @throws TransportExceptionInterface
44
     */
45
    private function getPackageInformation()
46
    {
47
        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...
48
            return $this->package_info;
49
        }
50
51
        if (!$this->package_name || !strpos($this->package_name, "/")) {
52
            return false;
53
        }
54
55
        $packgist_url = "https://repo.packagist.org/p/".$this->package_name.".json";
56
        $response = $this->client->request("GET", $packgist_url);
57
58
        if ($response->getStatusCode() == 200) {
59
            $content = json_decode($response->getContent(), true);
60
            if (is_array($content) && $content["packages"] && $content["packages"][$this->package_name]) {
61
                $this->package_info = $content["packages"][$this->package_name];
62
                return $this->package_info;
63
            }
64
        }
65
66
        return false;
67
    }
68
69
    /**
70
     * @param string $package_name
71
     * @return false|int|string|null
72
     * @throws ClientExceptionInterface
73
     * @throws RedirectionExceptionInterface
74
     * @throws ServerExceptionInterface
75
     * @throws TransportExceptionInterface
76
     */
77
    public function getLastPackagistVersion(string $package_name)
78
    {
79
        $this->package_name = $package_name;
80
        if ($package = $this->getPackageInformation()) {
81
            return array_key_first($package);
82
        }
83
84
        return false;
85
    }
86
87
    /**
88
     * @param string $package_name
89
     * @return array
90
     * @throws ClientExceptionInterface
91
     * @throws RedirectionExceptionInterface
92
     * @throws ServerExceptionInterface
93
     * @throws TransportExceptionInterface
94
     */
95
    public function getAllPackagistVersions(string $package_name)
96
    {
97
        $this->package_name = $package_name;
98
        if ($package = $this->getPackageInformation()) {
99
            return array_keys($package);
100
        }
101
    }
102
}
103