Passed
Push — master ( c1490b...95dfbb )
by Anthony
02:45
created

Version::getToken()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 3
eloc 6
c 1
b 0
f 1
nc 4
nop 0
dl 0
loc 11
rs 10
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
    {
79
        $this->package = $package;
80
    }
81
82
    /**
83
     * @return mixed|null
84
     * @throws ClientExceptionInterface
85
     * @throws RedirectionExceptionInterface
86
     * @throws ServerExceptionInterface
87
     * @throws TransportExceptionInterface
88
     */
89
    private function getToken()
90
    {
91
        $token = null;
92
        $response = $this->client->request("GET", $this->package->getProjectUrl()."ribs-admin/packages/send-token/");
93
        $datas = $response->getStatusCode() == 200 ? $response->getContent() : null;
94
95
        if ($datas) {
96
            $token = json_decode($datas, true)["token"];
97
        }
98
99
        return $token;
100
    }
101
102
    /**
103
     * @return mixed|null
104
     * @throws ClientExceptionInterface
105
     * @throws RedirectionExceptionInterface
106
     * @throws ServerExceptionInterface
107
     * @throws TransportExceptionInterface
108
     */
109
    private function getComposerLockJson()
110
    {
111
        if ($this->package && !$this->package->isIsLocal()) {
112
            $response = $this->client->request("GET", $this->package->getProjectUrl().$this->package->getComposerLockUrl());
113
            $composer_lock = $response->getStatusCode() == 200 ? $response->getContent() : null;
114
        } else {
115
            $composer_lock = file_get_contents('../composer.lock');
116
        }
117
118
        if ($composer_lock) {
119
            return json_decode($composer_lock, true);
120
        }
121
122
        return null;
123
    }
124
125
    /**
126
     * @return mixed|null
127
     * @throws ClientExceptionInterface
128
     * @throws RedirectionExceptionInterface
129
     * @throws ServerExceptionInterface
130
     * @throws TransportExceptionInterface
131
     */
132
    public function getPackage()
133
    {
134
        $composer_lock = $this->getComposerLockJson();
135
        if ($composer_lock) {
136
            $packages = $composer_lock["packages"];
137
            $key = array_search($this->package->getPackageName(), array_column($packages, 'name'));
138
139
            if ($key) {
140
                return $packages[$key];
141
            }
142
        }
143
144
        $this->messages["composer_lock"] = "Composer lock not found at " . $this->package->getProjectUrl();
145
146
        return null;
147
    }
148
149
    /**
150
     * @return mixed|null
151
     * @throws ClientExceptionInterface
152
     * @throws RedirectionExceptionInterface
153
     * @throws ServerExceptionInterface
154
     * @throws TransportExceptionInterface
155
     */
156
    public function getVersion()
157
    {
158
        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

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

167
        $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...
168
        $version_date = null;
169
170
        if ($string_date) {
171
            $version_date = new DateTime($string_date);
172
        }
173
174
        return $version_date;
175
    }
176
177
    public function updatePackage(string $guid, string $version)
178
    {
179
        $package = $this->em->getRepository(Package::class)->findOneBy(["guid" => $guid]);
180
181
        if ($package) {
182
            $this->setPackageEntity($package);
183
184
            if (!$this->getToken() || $this->getToken() !== $this->local_token) {
185
                $this->messages["token_error"] = "Token not matching on : " . $this->package->getProjectUrl();
186
                return;
187
            }
188
189
            $this->client->request("GET", $this->package->getProjectUrl().'rpackages/change-version/'.$package->getPackageName().':'.$version);
190
            //$composer_lock = $response->getStatusCode() == 200 ? $response->getContent() : null;
191
192
            $this->save($guid);
193
        }
194
    }
195
196
    /**
197
     * @param string $package_guid
198
     * @throws ClientExceptionInterface
199
     * @throws RedirectionExceptionInterface
200
     * @throws ServerExceptionInterface
201
     * @throws TransportExceptionInterface
202
     */
203
    public function save(string $package_guid)
204
    {
205
        $package = $this->em->getRepository(Package::class)->findOneBy(["guid" => $package_guid]);
206
207
        if ($package) {
208
            $this->setPackageEntity($package);
209
210
            if (!$this->getToken() || $this->getToken() !== $this->local_token) {
211
                $this->messages["token_error"] = "Token not matching on : " . $this->package->getProjectUrl();
212
                return;
213
            }
214
215
            $package->setVersion($this->getVersion());
216
            $package->setVersionDate($this->getVersionDate());
217
            $package->setLastPackagistVersion($this->packagist->getLastPackagistVersion($package->getPackageName()));
218
            $package->setLastCheck(new DateTime());
219
220
            $this->em->persist($package);
221
            $this->em->flush();
222
        }
223
    }
224
}
225