-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SDK-4967: Discouraged packages checker (#51)
- Loading branch information
1 parent
e875c3f
commit 4051946
Showing
12 changed files
with
654 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
src/Checker/DiscouragedPackagesChecker/DiscouragedPackageDto.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
|
||
/** | ||
* Copyright © 2016-present Spryker Systems GmbH. All rights reserved. | ||
* Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SprykerSdk\Evaluator\Checker\DiscouragedPackagesChecker; | ||
|
||
class DiscouragedPackageDto | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
protected string $packageName; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
protected string $reason; | ||
|
||
/** | ||
* @param string $packageName | ||
* @param string $reason | ||
*/ | ||
public function __construct(string $packageName, string $reason) | ||
{ | ||
$this->packageName = $packageName; | ||
$this->reason = $reason; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getPackageName(): string | ||
{ | ||
return $this->packageName; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getReason(): string | ||
{ | ||
return $this->reason; | ||
} | ||
} |
92 changes: 92 additions & 0 deletions
92
src/Checker/DiscouragedPackagesChecker/DiscouragedPackagesChecker.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
<?php | ||
|
||
/** | ||
* Copyright © 2016-present Spryker Systems GmbH. All rights reserved. | ||
* Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SprykerSdk\Evaluator\Checker\DiscouragedPackagesChecker; | ||
|
||
use Psr\Http\Client\ClientExceptionInterface; | ||
use SprykerSdk\Evaluator\Checker\AbstractChecker; | ||
use SprykerSdk\Evaluator\Dto\CheckerInputDataDto; | ||
use SprykerSdk\Evaluator\Dto\CheckerResponseDto; | ||
use SprykerSdk\Evaluator\Dto\ViolationDto; | ||
use SprykerSdk\Evaluator\Reader\ComposerReaderInterface; | ||
|
||
class DiscouragedPackagesChecker extends AbstractChecker | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
public const NAME = 'DISCOURAGED_PACKAGES_CHECKER'; | ||
|
||
/** | ||
* @var \SprykerSdk\Evaluator\Checker\DiscouragedPackagesChecker\DiscouragedPackagesFetcherInterface | ||
*/ | ||
protected DiscouragedPackagesFetcherInterface $discouragedPackagesFetcher; | ||
|
||
/** | ||
* @var \SprykerSdk\Evaluator\Reader\ComposerReaderInterface | ||
*/ | ||
protected ComposerReaderInterface $composerReader; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
protected string $checkerDocUrl; | ||
|
||
/** | ||
* @param \SprykerSdk\Evaluator\Checker\DiscouragedPackagesChecker\DiscouragedPackagesFetcherInterface $discouragedPackagesFetcher | ||
* @param \SprykerSdk\Evaluator\Reader\ComposerReaderInterface $composerReader | ||
* @param string $checkerDocUrl | ||
*/ | ||
public function __construct( | ||
DiscouragedPackagesFetcherInterface $discouragedPackagesFetcher, | ||
ComposerReaderInterface $composerReader, | ||
string $checkerDocUrl = '' | ||
) { | ||
$this->discouragedPackagesFetcher = $discouragedPackagesFetcher; | ||
$this->composerReader = $composerReader; | ||
$this->checkerDocUrl = $checkerDocUrl; | ||
} | ||
|
||
/** | ||
* @param \SprykerSdk\Evaluator\Dto\CheckerInputDataDto $inputData | ||
* | ||
* @return \SprykerSdk\Evaluator\Dto\CheckerResponseDto | ||
*/ | ||
public function check(CheckerInputDataDto $inputData): CheckerResponseDto | ||
{ | ||
$projectInstalledPackages = array_keys($this->composerReader->getInstalledPackages()); | ||
|
||
try { | ||
$discouragedPackages = $this->discouragedPackagesFetcher->fetchDiscouragedPackagesByPackageNames( | ||
$projectInstalledPackages, | ||
); | ||
} catch (ClientExceptionInterface | \InvalidArgumentException $clientException) { | ||
return new CheckerResponseDto( | ||
[new ViolationDto(sprintf('Release app api request issue: %s', $clientException->getMessage()))], | ||
$this->checkerDocUrl, | ||
); | ||
} | ||
|
||
$violations = []; | ||
|
||
foreach ($discouragedPackages as $discouragedPackage) { | ||
$violations[] = new ViolationDto($discouragedPackage->getReason(), $discouragedPackage->getPackageName()); | ||
} | ||
|
||
return new CheckerResponseDto($violations, $this->checkerDocUrl); | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getName(): string | ||
{ | ||
return static::NAME; | ||
} | ||
} |
123 changes: 123 additions & 0 deletions
123
src/Checker/DiscouragedPackagesChecker/DiscouragedPackagesFetcher.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
<?php | ||
|
||
/** | ||
* Copyright © 2016-present Spryker Systems GmbH. All rights reserved. | ||
* Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SprykerSdk\Evaluator\Checker\DiscouragedPackagesChecker; | ||
|
||
use GuzzleHttp\Psr7\Request; | ||
use InvalidArgumentException; | ||
use SprykerSdk\Evaluator\External\Http\HttpClientFactoryInterface; | ||
|
||
class DiscouragedPackagesFetcher implements DiscouragedPackagesFetcherInterface | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
protected const RESULT_KEY = 'result'; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
protected const NAME_KEY = 'name'; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
protected const REASON_KEY = 'reason'; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
protected const API_ENDPOINT = '/discouraged-packages-analyze.json'; | ||
|
||
/** | ||
* string | ||
* | ||
* @var string | ||
*/ | ||
protected const API_METHOD = 'POST'; | ||
|
||
/** | ||
* @var \SprykerSdk\Evaluator\External\Http\HttpClientFactoryInterface | ||
*/ | ||
protected HttpClientFactoryInterface $httpClientFactory; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
protected string $releaseAppUrl; | ||
|
||
/** | ||
* @param \SprykerSdk\Evaluator\External\Http\HttpClientFactoryInterface $httpClientFactory | ||
* @param string $releaseAppUrl | ||
*/ | ||
public function __construct(HttpClientFactoryInterface $httpClientFactory, string $releaseAppUrl) | ||
{ | ||
$this->httpClientFactory = $httpClientFactory; | ||
$this->releaseAppUrl = $releaseAppUrl; | ||
} | ||
|
||
/** | ||
* @param array<string> $packageNames | ||
* | ||
* @return array<\SprykerSdk\Evaluator\Checker\DiscouragedPackagesChecker\DiscouragedPackageDto> | ||
*/ | ||
public function fetchDiscouragedPackagesByPackageNames(array $packageNames): array | ||
{ | ||
$request = new Request( | ||
static::API_METHOD, | ||
$this->getReleaseAppUrl() . static::API_ENDPOINT, | ||
['Content-Type' => 'application/json'], | ||
json_encode($packageNames, JSON_THROW_ON_ERROR), | ||
); | ||
|
||
$response = $this->httpClientFactory->createClient()->send($request); | ||
|
||
return $this->buildDiscouragedPackageDtos( | ||
json_decode((string)$response->getBody(), true, 512, \JSON_THROW_ON_ERROR), | ||
); | ||
} | ||
|
||
/** | ||
* @param array<mixed> $responseData | ||
* | ||
* @throws \InvalidArgumentException | ||
* | ||
* @return array<\SprykerSdk\Evaluator\Checker\DiscouragedPackagesChecker\DiscouragedPackageDto> | ||
*/ | ||
protected function buildDiscouragedPackageDtos(array $responseData): array | ||
{ | ||
if (!isset($responseData[static::RESULT_KEY])) { | ||
throw new InvalidArgumentException(sprintf('Unable to find "%s" key in release app response', static::RESULT_KEY)); | ||
} | ||
|
||
if (!is_array($responseData[static::RESULT_KEY])) { | ||
throw new InvalidArgumentException('Result data should be an array'); | ||
} | ||
|
||
$result = []; | ||
|
||
foreach ($responseData[static::RESULT_KEY] as $packageData) { | ||
if (!isset($packageData[static::NAME_KEY], $packageData[static::REASON_KEY])) { | ||
throw new InvalidArgumentException('Invalid package data'); | ||
} | ||
|
||
$result[] = new DiscouragedPackageDto($packageData[static::NAME_KEY], $packageData[static::REASON_KEY]); | ||
} | ||
|
||
return $result; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
protected function getReleaseAppUrl(): string | ||
{ | ||
return rtrim($this->releaseAppUrl); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
src/Checker/DiscouragedPackagesChecker/DiscouragedPackagesFetcherInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
/** | ||
* Copyright © 2016-present Spryker Systems GmbH. All rights reserved. | ||
* Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SprykerSdk\Evaluator\Checker\DiscouragedPackagesChecker; | ||
|
||
interface DiscouragedPackagesFetcherInterface | ||
{ | ||
/** | ||
* @param array<string> $packageNames | ||
* | ||
* @throws \Psr\Http\Client\ClientExceptionInterface | ||
* | ||
* @return array<\SprykerSdk\Evaluator\Checker\DiscouragedPackagesChecker\DiscouragedPackageDto> | ||
*/ | ||
public function fetchDiscouragedPackagesByPackageNames(array $packageNames): array; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.