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

Version::getComposerLockJson()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 5
eloc 8
c 2
b 0
f 0
nc 6
nop 0
dl 0
loc 14
rs 9.6111
1
<?php
2
3
namespace PiouPiou\RibsAdminBundle\Service;
4
5
use DateTime;
6
use Doctrine\ORM\EntityManagerInterface;
7
use PiouPiou\RibsAdminBundle\Entity\Package;
8
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
9
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
10
use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
11
use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
12
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
13
use Symfony\Contracts\HttpClient\HttpClientInterface;
14
use Exception;
15
16
class Version
17
{
18
    /**
19
     * @var EntityManagerInterface
20
     */
21
    private $em;
22
23
    /**
24
     * @var HttpClientInterface
25
     */
26
    private $client;
27
28
    /**
29
     * @var ParameterBagInterface
30
     */
31
    private $parameter;
32
33
    /**
34
     * @var PackagistApi
35
     */
36
    private $packagist;
37
38
    /**
39
     * @var mixed
40
     */
41
    private $local_token;
42
43
    /**
44
     * @var Package
45
     */
46
    private $package;
47
48
    private $messages = [];
49
50
    /**
51
     * Version constructor.
52
     * @param EntityManagerInterface $em
53
     * @param HttpClientInterface $client
54
     * @param ParameterBagInterface $parameter
55
     * @param PackagistApi $packagist
56
     */
57
    public function __construct(EntityManagerInterface $em, HttpClientInterface $client, ParameterBagInterface $parameter, PackagistApi $packagist)
58
    {
59
        $this->em = $em;
60
        $this->client = $client;
61
        $this->parameter = $parameter;
62
        $this->packagist = $packagist;
63
        $this->local_token = $parameter->get("ribs_admin.packages_token");
64
    }
65
66
    /**
67
     * @return array
68
     */
69
    public function getMessages(): array
70
    {
71
        return $this->messages;
72
    }
73
74
    /**
75
     * @param Package $package
76
     */
77
    public function setPackageEntity(Package $package) {
78
        $this->package = $package;
79
    }
80
81
    /**
82
     * @return mixed|null
83
     */
84
    private function getToken()
85
    {
86
        $token = null;
87
        $response = $this->client->request("GET", $this->package->getProjectUrl()."ribs-admin/packages/send-token/");
88
        $datas = $response->getStatusCode() == 200 ? $response->getContent() : null;
89
90
        if ($datas) {
91
            $token = json_decode($datas, true)["token"];
92
        }
93
94
        return $token;
95
    }
96
97
    /**
98
     * @return mixed|null
99
     * @throws ClientExceptionInterface
100
     * @throws RedirectionExceptionInterface
101
     * @throws ServerExceptionInterface
102
     * @throws TransportExceptionInterface
103
     */
104
    private function getComposerLockJson()
105
    {
106
        if ($this->package && !$this->package->isIsLocal()) {
107
            $response = $this->client->request("GET", $this->package->getProjectUrl().$this->package->getComposerLockUrl());
108
            $composer_lock = $response->getStatusCode() == 200 ? $response->getContent() : null;
109
        } else {
110
            $composer_lock = file_get_contents('../composer.lock');
111
        }
112
113
        if ($composer_lock) {
114
            return json_decode($composer_lock, true);
115
        }
116
117
        return null;
118
    }
119
120
    /**
121
     * @return mixed|null
122
     * @throws ClientExceptionInterface
123
     * @throws RedirectionExceptionInterface
124
     * @throws ServerExceptionInterface
125
     * @throws TransportExceptionInterface
126
     */
127
    public function getPackage()
128
    {
129
        $composer_lock = $this->getComposerLockJson();
130
        if ($composer_lock) {
131
            $packages = $composer_lock["packages"];
132
            $key = array_search($this->package->getPackageName(), array_column($packages, 'name'));
133
134
            if ($key) {
135
                return $packages[$key];
136
            }
137
        }
138
139
        $this->messages["composer_lock"] = "Composer lock not found at " . $this->package->getProjectUrl();
140
141
        return null;
142
    }
143
144
    /**
145
     * @return mixed|null
146
     */
147
    public function getVersion()
148
    {
149
        return $this->getPackage($this->package->getPackageName()) ? $this->getPackage($this->package->getPackageName())["version"] : null;
0 ignored issues
show
Unused Code introduced by
The call to PiouPiou\RibsAdminBundle...e\Version::getPackage() has too many arguments starting with $this->package->getPackageName(). ( Ignorable by Annotation )

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

149
        return $this->/** @scrutinizer ignore-call */ getPackage($this->package->getPackageName()) ? $this->getPackage($this->package->getPackageName())["version"] : null;

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
150
    }
151
152
    /**
153
     * @return DateTime|null
154
     * @throws Exception
155
     */
156
    public function getVersionDate(): ?DateTime
157
    {
158
        $string_date = $this->getPackage($this->package->getPackageName()) ? explode("T", $this->getPackage($this->package->getPackageName())["time"])[0] : null;
0 ignored issues
show
Unused Code introduced by
The call to PiouPiou\RibsAdminBundle...e\Version::getPackage() has too many arguments starting with $this->package->getPackageName(). ( Ignorable by Annotation )

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

158
        $string_date = $this->/** @scrutinizer ignore-call */ getPackage($this->package->getPackageName()) ? explode("T", $this->getPackage($this->package->getPackageName())["time"])[0] : null;

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
159
        $version_date = null;
160
161
        if ($string_date) {
162
            $version_date = new DateTime($string_date);
163
        }
164
165
        return $version_date;
166
    }
167
168
    /**
169
     * @param $package_guid
170
     * @throws Exception
171
     */
172
    public function save($package_guid)
173
    {
174
        $package = $this->em->getRepository(Package::class)->findOneBy(["guid" => $package_guid]);
175
176
        if ($package) {
177
            $this->setPackageEntity($package);
178
179
            if (!$this->getToken() || $this->getToken() !== $this->local_token) {
180
                $this->messages["token_error"] = "Token not matching on : " . $this->package->getProjectUrl();
181
                return;
182
            }
183
184
            $package->setVersion($this->getVersion());
185
            $package->setVersionDate($this->getVersionDate());
186
            $package->setLastPackagistVersion($this->packagist->getLastPackagistVersion($package->getPackageName()));
187
            $package->setLastCheck(new DateTime());
188
189
            $this->em->persist($package);
190
            $this->em->flush();
191
        }
192
    }
193
}
194