Passed
Push — master ( 07a5d2...98935d )
by Anthony
02:57
created

Version::setVersionEntity()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 2
rs 10
c 1
b 0
f 1
1
<?php
2
3
namespace PiouPiou\RibsAdminBundle\Service;
4
5
use Doctrine\ORM\EntityManagerInterface;
6
use PiouPiou\RibsAdminBundle\Entity\Package;
7
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
8
use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
9
use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
10
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
11
use Symfony\Contracts\HttpClient\HttpClientInterface;
12
use Exception;
13
14
class Version
15
{
16
    /**
17
     * @var EntityManagerInterface
18
     */
19
    private $em;
20
21
    /**
22
     * @var HttpClientInterface
23
     */
24
    private $client;
25
26
    /**
27
     * @var Package
28
     */
29
    private $package;
30
31
    /**
32
     * Version constructor.
33
     * @param EntityManagerInterface $em
34
     * @param HttpClientInterface $client
35
     */
36
    public function __construct(EntityManagerInterface $em, HttpClientInterface $client)
37
    {
38
        $this->em = $em;
39
        $this->client = $client;
40
    }
41
42
    /**
43
     * @param Package $package
44
     */
45
    public function setPackageEntity(Package $package) {
46
        $this->package = $package;
47
    }
48
49
    /**
50
     * @return mixed|null
51
     * @throws ClientExceptionInterface
52
     * @throws RedirectionExceptionInterface
53
     * @throws ServerExceptionInterface
54
     * @throws TransportExceptionInterface
55
     */
56
    private function getComposerLockJson()
57
    {
58
        if ($this->package && !$this->package->isIsLocal()) {
59
            $response = $this->client->request("GET", $this->package->getProjectUrl().$this->package->getComposerLockUrl());
60
            $composer_lock = $response->getStatusCode() == 200 ? $response->getContent() : null;
61
        } else {
62
            $composer_lock = file_get_contents('../composer.lock');
63
        }
64
65
        if ($composer_lock) {
66
            return json_decode($composer_lock, true);
67
        }
68
69
        return null;
70
    }
71
72
    /**
73
     * @param $package_name
74
     * @return mixed|null
75
     * @throws ClientExceptionInterface
76
     * @throws RedirectionExceptionInterface
77
     * @throws ServerExceptionInterface
78
     * @throws TransportExceptionInterface
79
     */
80
    public function getPackage($package_name)
81
    {
82
        $composer_lock = $this->getComposerLockJson();
83
        if ($composer_lock) {
84
            $packages = $composer_lock["packages"];
85
            $key = array_search($package_name, array_column($packages, 'name'));
86
87
            if ($key) {
88
                return $packages[$key];
89
            }
90
        }
91
92
        return null;
93
    }
94
95
    /**
96
     * @param $package_name
97
     * @return mixed|null
98
     */
99
    public function getVersion($package_name)
100
    {
101
        return $this->getPackage($package_name) ? $this->getPackage($package_name)["version"] : null;
102
    }
103
104
    /**
105
     * @param $package_name
106
     * @return mixed|null
107
     * @throws Exception
108
     */
109
    public function getVersionDate($package_name)
110
    {
111
        $string_date = $this->getPackage($package_name) ? explode("T", $this->getPackage($package_name)["time"])[0] : null;
112
        $version_date = null;
113
114
        if ($string_date) {
115
            $version_date = new \DateTime($string_date);
116
        }
117
118
        return $version_date;
119
    }
120
121
    /**
122
     * @param $package_name
123
     * @return false|int|string|null
124
     * @throws ClientExceptionInterface
125
     * @throws RedirectionExceptionInterface
126
     * @throws ServerExceptionInterface
127
     * @throws TransportExceptionInterface
128
     */
129
    public function getLastPackagistVersion($package_name)
130
    {
131
        if (!strpos($package_name, "/")) {
132
            return false;
133
        }
134
135
        $packgist_url = "https://repo.packagist.org/p/".$package_name.".json";
136
137
        $response = $this->client->request("GET", $packgist_url);
138
139
        if ($response->getStatusCode() == 200) {
140
            $content = json_decode($response->getContent(), true);
141
            if (is_array($content) && $content["packages"] && $content["packages"][$package_name]) {
142
                return array_key_first($content["packages"][$package_name]);
143
            }
144
        }
145
    }
146
147
    /**
148
     * @param $package_name
149
     * @throws Exception
150
     */
151
    public function save($package_name)
152
    {
153
        $package = $this->em->getRepository(Package::class)->findOneBy(["package_name" => $package_name]);
154
155
        if (!$package) {
156
            $package = new Package();
157
            $package->setProjectName($package_name);
158
            $package->setPackageName($package_name);
159
            $package->setProjectUrl($package_name);
160
            $package->setCheckVersionUrl($package_name);
161
        }
162
163
        $package->setVersion($this->getVersion($package_name));
164
        $package->setVersionDate($this->getVersionDate($package_name));
165
        $package->setLastPackagistVersion($this->getLastPackagistVersion($package_name));
0 ignored issues
show
Bug introduced by
It seems like $this->getLastPackagistVersion($package_name) can also be of type false; however, parameter $last_packagist_version of PiouPiou\RibsAdminBundle...tLastPackagistVersion() does only seem to accept null|string, 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

165
        $package->setLastPackagistVersion(/** @scrutinizer ignore-type */ $this->getLastPackagistVersion($package_name));
Loading history...
166
        $package->setLastCheck(new \DateTime());
167
168
        $this->em->persist($package);
169
        $this->em->flush();
170
    }
171
}
172