Version   A
last analyzed

Complexity

Total Complexity 28

Size/Duplication

Total Lines 224
Duplicated Lines 0 %

Importance

Changes 14
Bugs 1 Features 7
Metric Value
eloc 69
dl 0
loc 224
rs 10
c 14
b 1
f 7
wmc 28

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A getVersionDate() 0 10 3
A updatePackage() 0 25 5
A getComposerLockJson() 0 14 5
A getMessages() 0 3 1
A getToken() 0 11 3
A save() 0 19 4
A setPackageEntity() 0 3 1
A getVersion() 0 3 2
A getPackage() 0 15 3
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\Component\Mime\Part\Multipart\FormDataPart;
10
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
11
use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
12
use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
13
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
14
use Symfony\Contracts\HttpClient\HttpClientInterface;
15
use Exception;
16
17
class Version
18
{
19
    /**
20
     * @var EntityManagerInterface
21
     */
22
    private $em;
23
24
    /**
25
     * @var HttpClientInterface
26
     */
27
    private $client;
28
29
    /**
30
     * @var ParameterBagInterface
31
     */
32
    private $parameter;
33
34
    /**
35
     * @var PackagistApi
36
     */
37
    private $packagist;
38
39
    /**
40
     * @var mixed
41
     */
42
    private $local_token;
43
44
    /**
45
     * @var Package
46
     */
47
    private $package;
48
49
    private $messages = [];
50
51
    /**
52
     * Version constructor.
53
     * @param EntityManagerInterface $em
54
     * @param HttpClientInterface $client
55
     * @param ParameterBagInterface $parameter
56
     * @param PackagistApi $packagist
57
     */
58
    public function __construct(EntityManagerInterface $em, HttpClientInterface $client, ParameterBagInterface $parameter, PackagistApi $packagist)
59
    {
60
        $this->em = $em;
61
        $this->client = $client;
62
        $this->parameter = $parameter;
63
        $this->packagist = $packagist;
64
        $this->local_token = $parameter->get("ribs_admin.packages_token");
65
    }
66
67
    /**
68
     * @return array
69
     */
70
    public function getMessages(): array
71
    {
72
        return $this->messages;
73
    }
74
75
    /**
76
     * @param Package $package
77
     */
78
    public function setPackageEntity(Package $package)
79
    {
80
        $this->package = $package;
81
    }
82
83
    /**
84
     * @return mixed|null
85
     * @throws ClientExceptionInterface
86
     * @throws RedirectionExceptionInterface
87
     * @throws ServerExceptionInterface
88
     * @throws TransportExceptionInterface
89
     */
90
    private function getToken()
91
    {
92
        $token = null;
93
        $response = $this->client->request("GET", $this->package->getProjectUrl()."ribs-admin/packages/dist/send-token/");
94
        $datas = $response->getStatusCode() == 200 ? $response->getContent() : null;
95
96
        if ($datas) {
97
            $token = json_decode($datas, true)["token"];
98
        }
99
100
        return $token;
101
    }
102
103
    /**
104
     * @return mixed|null
105
     * @throws ClientExceptionInterface
106
     * @throws RedirectionExceptionInterface
107
     * @throws ServerExceptionInterface
108
     * @throws TransportExceptionInterface
109
     */
110
    private function getComposerLockJson()
111
    {
112
        if ($this->package && !$this->package->isIsLocal()) {
113
            $response = $this->client->request("GET", $this->package->getProjectUrl().$this->package->getComposerLockUrl());
114
            $composer_lock = $response->getStatusCode() == 200 ? $response->getContent() : null;
115
        } else {
116
            $composer_lock = file_get_contents('../composer.lock');
117
        }
118
119
        if ($composer_lock) {
120
            return json_decode($composer_lock, true);
121
        }
122
123
        return null;
124
    }
125
126
    /**
127
     * @return mixed|null
128
     * @throws ClientExceptionInterface
129
     * @throws RedirectionExceptionInterface
130
     * @throws ServerExceptionInterface
131
     * @throws TransportExceptionInterface
132
     */
133
    public function getPackage()
134
    {
135
        $composer_lock = $this->getComposerLockJson();
136
        if ($composer_lock) {
137
            $packages = $composer_lock["packages"];
138
            $key = array_search($this->package->getPackageName(), array_column($packages, 'name'));
139
140
            if ($key) {
141
                return $packages[$key];
142
            }
143
        }
144
145
        $this->messages["composer_lock"] = "Composer lock not found at " . $this->package->getProjectUrl();
146
147
        return null;
148
    }
149
150
    /**
151
     * @return mixed|null
152
     * @throws ClientExceptionInterface
153
     * @throws RedirectionExceptionInterface
154
     * @throws ServerExceptionInterface
155
     * @throws TransportExceptionInterface
156
     */
157
    public function getVersion()
158
    {
159
        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

159
        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...
160
    }
161
162
    /**
163
     * @return DateTime|null
164
     * @throws Exception
165
     */
166
    public function getVersionDate(): ?DateTime
167
    {
168
        $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

168
        $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...
169
        $version_date = null;
170
171
        if ($string_date) {
172
            $version_date = new DateTime($string_date);
173
        }
174
175
        return $version_date;
176
    }
177
178
    /**
179
     * @param string $guid
180
     * @param string $version
181
     * @return string|void|null
182
     * @throws ClientExceptionInterface
183
     * @throws RedirectionExceptionInterface
184
     * @throws ServerExceptionInterface
185
     * @throws TransportExceptionInterface
186
     */
187
    public function updatePackage(string $guid, string $version)
188
    {
189
        $package = $this->em->getRepository(Package::class)->findOneBy(["guid" => $guid]);
190
191
        if ($package) {
192
            $this->setPackageEntity($package);
193
194
            if (!$this->getToken() || $this->getToken() !== $this->local_token) {
195
                $this->messages["token_error"] = "Token not matching on : " . $this->package->getProjectUrl();
196
                return;
197
            }
198
199
            $form_data = new FormDataPart([
200
                "package_name" => $package->getPackageName(),
201
                "version" => $version,
202
            ]);
203
204
            $response = $this->client->request("POST", $this->package->getProjectUrl().'ribs-admin/packages/dist/change-version/', [
205
                "headers" => $form_data->getPreparedHeaders()->toArray(),
206
                "body" => $form_data->bodyToIterable()
207
            ]);
208
209
            $this->save($guid);
210
211
            return $response->getStatusCode() == 200 ? $response->getContent() : null;
212
        }
213
    }
214
215
    /**
216
     * @param string $package_guid
217
     * @throws ClientExceptionInterface
218
     * @throws RedirectionExceptionInterface
219
     * @throws ServerExceptionInterface
220
     * @throws TransportExceptionInterface
221
     */
222
    public function save(string $package_guid)
223
    {
224
        $package = $this->em->getRepository(Package::class)->findOneBy(["guid" => $package_guid]);
225
226
        if ($package) {
227
            $this->setPackageEntity($package);
228
229
            if (!$this->getToken() || $this->getToken() !== $this->local_token) {
230
                $this->messages["token_error"] = "Token not matching on : " . $this->package->getProjectUrl();
231
                return;
232
            }
233
234
            $package->setVersion($this->getVersion());
235
            $package->setVersionDate($this->getVersionDate());
236
            $package->setLastPackagistVersion($this->packagist->getLastPackagistVersion($package->getPackageName()));
237
            $package->setLastCheck(new DateTime());
238
239
            $this->em->persist($package);
240
            $this->em->flush();
241
        }
242
    }
243
}
244