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