Passed
Push — master ( 7f3591...9d865c )
by Anthony
02:49
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 mixed
35
     */
36
    private $local_token;
37
38
    /**
39
     * @var Package
40
     */
41
    private $package;
42
43
    private $messages = [];
44
45
    /**
46
     * Version constructor.
47
     * @param EntityManagerInterface $em
48
     * @param HttpClientInterface $client
49
     * @param ParameterBagInterface $parameter
50
     */
51
    public function __construct(EntityManagerInterface $em, HttpClientInterface $client, ParameterBagInterface $parameter)
52
    {
53
        $this->em = $em;
54
        $this->client = $client;
55
        $this->parameter = $parameter;
56
        $this->local_token = $parameter->get("ribs_admin.packages_token");
57
    }
58
59
    /**
60
     * @return array
61
     */
62
    public function getMessages(): array
63
    {
64
        return $this->messages;
65
    }
66
67
    /**
68
     * @param Package $package
69
     */
70
    public function setPackageEntity(Package $package) {
71
        $this->package = $package;
72
    }
73
74
    /**
75
     * @return mixed|null
76
     */
77
    private function getToken()
78
    {
79
        $token = null;
80
        $response = $this->client->request("GET", $this->package->getProjectUrl()."ribs-admin/packages/send-token/");
81
        $datas = $response->getStatusCode() == 200 ? $response->getContent() : null;
82
83
        if ($datas) {
84
            $token = json_decode($datas, true)["token"];
85
        }
86
87
        return $token;
88
    }
89
90
    /**
91
     * @return mixed|null
92
     * @throws ClientExceptionInterface
93
     * @throws RedirectionExceptionInterface
94
     * @throws ServerExceptionInterface
95
     * @throws TransportExceptionInterface
96
     */
97
    private function getComposerLockJson()
98
    {
99
        if ($this->package && !$this->package->isIsLocal()) {
100
            $response = $this->client->request("GET", $this->package->getProjectUrl().$this->package->getComposerLockUrl());
101
            $composer_lock = $response->getStatusCode() == 200 ? $response->getContent() : null;
102
        } else {
103
            $composer_lock = file_get_contents('../composer.lock');
104
        }
105
106
        if ($composer_lock) {
107
            return json_decode($composer_lock, true);
108
        }
109
110
        return null;
111
    }
112
113
    /**
114
     * @return mixed|null
115
     * @throws ClientExceptionInterface
116
     * @throws RedirectionExceptionInterface
117
     * @throws ServerExceptionInterface
118
     * @throws TransportExceptionInterface
119
     */
120
    public function getPackage()
121
    {
122
        $composer_lock = $this->getComposerLockJson();
123
        if ($composer_lock) {
124
            $packages = $composer_lock["packages"];
125
            $key = array_search($this->package->getPackageName(), array_column($packages, 'name'));
126
127
            if ($key) {
128
                return $packages[$key];
129
            }
130
        }
131
132
        $this->messages["composer_lock"] = "Composer lock not found at " . $this->package->getProjectUrl();
133
134
        return null;
135
    }
136
137
    /**
138
     * @return mixed|null
139
     */
140
    public function getVersion()
141
    {
142
        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

142
        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...
143
    }
144
145
    /**
146
     * @return DateTime|null
147
     * @throws Exception
148
     */
149
    public function getVersionDate(): ?DateTime
150
    {
151
        $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

151
        $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...
152
        $version_date = null;
153
154
        if ($string_date) {
155
            $version_date = new DateTime($string_date);
156
        }
157
158
        return $version_date;
159
    }
160
161
    /**
162
     * @return false|int|string|null
163
     * @throws ClientExceptionInterface
164
     * @throws RedirectionExceptionInterface
165
     * @throws ServerExceptionInterface
166
     * @throws TransportExceptionInterface
167
     */
168
    public function getLastPackagistVersion()
169
    {
170
        if (!strpos($this->package->getPackageName(), "/")) {
171
            return false;
172
        }
173
174
        $packgist_url = "https://repo.packagist.org/p/".$this->package->getPackageName().".json";
175
176
        $response = $this->client->request("GET", $packgist_url);
177
178
        if ($response->getStatusCode() == 200) {
179
            $content = json_decode($response->getContent(), true);
180
            if (is_array($content) && $content["packages"] && $content["packages"][$this->package->getPackageName()]) {
181
                return array_key_first($content["packages"][$this->package->getPackageName()]);
182
            }
183
        }
184
    }
185
186
    /**
187
     * @param $package_guid
188
     * @throws Exception
189
     */
190
    public function save($package_guid)
191
    {
192
        $package = $this->em->getRepository(Package::class)->findOneBy(["guid" => $package_guid]);
193
194
        if ($package) {
195
            $this->setPackageEntity($package);
196
197
            if (!$this->getToken() || $this->getToken() !== $this->local_token) {
198
                $this->messages["token_error"] = "Token not matching on : " . $this->package->getProjectUrl();
199
                return;
200
            }
201
202
            $package->setVersion($this->getVersion());
203
            $package->setVersionDate($this->getVersionDate());
204
            $package->setLastPackagistVersion($this->getLastPackagistVersion());
205
            $package->setLastCheck(new DateTime());
206
207
            $this->em->persist($package);
208
            $this->em->flush();
209
        }
210
    }
211
}
212