Skip to content

Commit

Permalink
Implement retrieving latest module version from GitHub
Browse files Browse the repository at this point in the history
CS-5024
  • Loading branch information
MarijaIv committed Jan 19, 2024
1 parent aa83df9 commit e57f2e6
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 1 deletion.
17 changes: 17 additions & 0 deletions src/classes/Bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
use Adyen\Core\Infrastructure\ServiceRegister;
use Adyen\Core\Infrastructure\TaskExecution\Process;
use Adyen\Core\Infrastructure\TaskExecution\QueueItem;
use AdyenPayment\Classes\Proxies\GithubProxy;
use AdyenPayment\Classes\Repositories\AdyenGivingRepository;
use AdyenPayment\Classes\Repositories\BaseRepository;
use AdyenPayment\Classes\Repositories\BaseRepositoryWithConditionalDelete;
Expand Down Expand Up @@ -118,6 +119,22 @@ public static function init(): void
self::initRepositories();
}

protected static function initProxies(): void
{
parent::initProxies();

ServiceRegister::registerService(
GithubProxy::class,
static function () {
return new GithubProxy(
ServiceRegister::getService(HttpClient::class),
'https://api.github.com',
''
);
}
);
}

/**
* @inerhitDoc
*/
Expand Down
37 changes: 37 additions & 0 deletions src/classes/Proxies/GithubProxy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace AdyenPayment\Classes\Proxies;

use Adyen\Core\BusinessLogic\AdyenAPI\Http\Proxy;
use Adyen\Core\BusinessLogic\AdyenAPI\Http\Requests\HttpRequest;
use Adyen\Core\Infrastructure\Http\Exceptions\HttpRequestException;

/**
* Class GithubProxy
*
* @package AdyenPayment\Classes\Proxies
*/
class GithubProxy extends Proxy
{
/**
* Retrieves latest version from gitHub.
*
* @return string
*
* @throws HttpRequestException
*/
public function getLatestVersion(): string
{
$response = $this->get(new HttpRequest('repos/Adyen/adyen-prestashop/releases/latest'));
$body = $response->decodeBodyToArray();

return $body['tag_name'] ?? '';
}

protected function getRequestUrl(HttpRequest $request): string
{
$sanitizedEndpoint = ltrim($request->getEndpoint(), '/');

return "$this->baseUrl/$sanitizedEndpoint";
}
}
25 changes: 24 additions & 1 deletion src/classes/Services/Integration/VersionInfoService.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

use Adyen\Core\BusinessLogic\Domain\Integration\Version\VersionService as VersionServiceInterface;
use Adyen\Core\BusinessLogic\Domain\Version\Models\VersionInfo;
use Adyen\Core\Infrastructure\Http\Exceptions\HttpRequestException;
use Adyen\Core\Infrastructure\ServiceRegister;
use AdyenPayment\Classes\Proxies\GithubProxy;

/**
* Class VersionInfoService
Expand All @@ -14,11 +17,31 @@ class VersionInfoService implements VersionServiceInterface
{
/**
* @inheritDoc
*
* @throws HttpRequestException
*/
public function getVersionInfo(): VersionInfo
{
$version = \Module::getInstanceByName('adyenofficial')->version;

return new VersionInfo($version, $version);
return new VersionInfo($version, $this->getLatestVersion());
}

/**
* @return string
*
* @throws HttpRequestException
*/
private function getLatestVersion(): string
{
return $this->getGithubProxy()->getLatestVersion();
}

/**
* @return GithubProxy
*/
private function getGithubProxy(): GithubProxy
{
return ServiceRegister::getService(GithubProxy::class);
}
}

0 comments on commit e57f2e6

Please sign in to comment.