Passed
Push — master ( fbbe3c...8b0281 )
by Anthony
03:02
created

PackagistApi::getPackageInformation()   B

Complexity

Conditions 7
Paths 5

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 12
c 1
b 0
f 0
nc 5
nop 1
dl 0
loc 22
rs 8.8333
1
<?php
2
3
namespace PiouPiou\RibsAdminBundle\Service;
4
5
use Symfony\Contracts\HttpClient\HttpClientInterface;
6
7
class PackagistApi
8
{
9
    /**
10
     * @var HttpClientInterface
11
     */
12
    private $client;
13
14
    /**
15
     * PackagistApi constructor.
16
     * @param HttpClientInterface $client
17
     */
18
    public function __construct(HttpClientInterface $client)
19
    {
20
        $this->client = $client;
21
    }
22
23
    /**
24
     * @param string $package_name
25
     * @return false|mixed
26
     */
27
    public function getPackageInformation(string $package_name)
28
    {
29
        if ($this->package_info) {
30
            return $this->package_info;
31
        }
32
33
        if (!strpos($package_name, "/")) {
34
            return false;
35
        }
36
37
        $packgist_url = "https://repo.packagist.org/p/".$package_name.".json";
38
        $response = $this->client->request("GET", $packgist_url);
39
40
        if ($response->getStatusCode() == 200) {
41
            $content = json_decode($response->getContent(), true);
42
            if (is_array($content) && $content["packages"] && $content["packages"][$this->package->getPackageName()]) {
0 ignored issues
show
Bug introduced by
The property package does not exist on PiouPiou\RibsAdminBundle\Service\PackagistApi. Did you mean package_info?
Loading history...
43
                $this->package_info = $content["packages"][$this->package->getPackageName()];
0 ignored issues
show
Bug Best Practice introduced by
The property package_info does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
44
                return $this->package_info;
45
            }
46
        }
47
48
        return false;
49
    }
50
51
    /**
52
     * @param string $package_name
53
     * @return false|int|string|null
54
     */
55
    public function getLastPackagistVersion(string $package_name)
56
    {
57
        if (!strpos($package_name, "/")) {
58
            return false;
59
        }
60
61
        $packgist_url = "https://repo.packagist.org/p/".$package_name.".json";
62
63
        $response = $this->client->request("GET", $packgist_url);
64
65
        if ($response->getStatusCode() == 200) {
66
            $content = json_decode($response->getContent(), true);
67
            if (is_array($content) && $content["packages"] && $content["packages"][$this->package->getPackageName()]) {
0 ignored issues
show
Bug introduced by
The property package does not exist on PiouPiou\RibsAdminBundle\Service\PackagistApi. Did you mean package_info?
Loading history...
68
                return array_key_first($content["packages"][$this->package->getPackageName()]);
69
            }
70
        }
71
    }
72
}
73