Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 109 additions & 9 deletions classes/Services/MarketplaceService.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
/** @var Translator */
private $translator;

/** @var array<string, Module|MarketplaceApiException> */
private $cache = [];

const ADDONS_API_URL = 'https://api.addons.prestashop.com';

/**
Expand All @@ -42,23 +45,120 @@
}

/**
* @throws MarketplaceApiException
* Fetches raw response bodies for multiple modules.
* Returns a map of module name => response body string, or false on failure.
*
* @param string[] $toFetch
*
* @return array<string, string|false>
*/
public function getModuleDetail(string $module): Module
protected function fetchModulesRaw(array $toFetch): array
{
$response = file_get_contents(self::ADDONS_API_URL . '/v2/products/' . $module);
$multiHandle = curl_multi_init();
$handles = [];

foreach ($toFetch as $name) {
$ch = curl_init(self::ADDONS_API_URL . '/v2/products/' . $name);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_multi_add_handle($multiHandle, $ch);
$handles[$name] = $ch;
}

$running = 0;
do {
curl_multi_exec($multiHandle, $running);
if ($running > 0 && curl_multi_select($multiHandle, 1.0) === -1) {
usleep(100);
}
} while ($running > 0);

$raw = [];
foreach ($handles as $name => $ch) {
$body = curl_multi_getcontent($ch);
$errno = curl_errno($ch);
curl_multi_remove_handle($multiHandle, $ch);
curl_close($ch);
$raw[$name] = ($errno || $body === null) ? false : $body;
}

curl_multi_close($multiHandle);

return $raw;
}

/**
* Fetches details for multiple modules in parallel via curl_multi.
* Returns a map of module name => Module on success, or MarketplaceApiException on failure.
*
* @param string[] $moduleNames
*
* @return array<string, Module|MarketplaceApiException>
*/
public function getModuleDetails(array $moduleNames): array

Check failure on line 99 in classes/Services/MarketplaceService.php

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Refactor this function to reduce its Cognitive Complexity from 17 to the 15 allowed.

See more on https://sonarcloud.io/project/issues?id=PrestaShop_autoupgrade&issues=AZ0MB-6GHsDk9CgwSFeA&open=AZ0MB-6GHsDk9CgwSFeA&pullRequest=1735
{
$toFetch = [];
$result = [];

foreach ($moduleNames as $name) {
if (isset($this->cache[$name])) {
$result[$name] = $this->cache[$name];
} else {
$toFetch[] = $name;
}
}

if (empty($toFetch)) {
return $result;
}

$raw = $this->fetchModulesRaw($toFetch);

if (!$response) {
throw new MarketplaceApiException($this->translator->trans('Error when retrieving data from Marketplace API'), MarketplaceApiException::API_NOT_CALLABLE_CODE);
// Ensure every requested name gets a result, even if fetchModulesRaw omits it.
foreach ($toFetch as $name) {
if (!array_key_exists($name, $raw)) {
$raw[$name] = false;
}
}

$data = json_decode($response, true);
foreach ($raw as $name => $body) {
if (!$body) {
$value = new MarketplaceApiException(
$this->translator->trans('Error when retrieving data from Marketplace API'),
MarketplaceApiException::API_NOT_CALLABLE_CODE
);
} else {
$data = json_decode($body, true);
if (!$data || !is_array($data)) {
$value = new MarketplaceApiException(
$this->translator->trans('Unable to retrieve module %s information. Ignored.', [$name]),
MarketplaceApiException::EMPTY_DATA_CODE
);
} else {
$value = Module::fromArray($data);
}
}

$this->cache[$name] = $value;
$result[$name] = $value;
}

if (!$data || !is_array($data)) {
throw new MarketplaceApiException($this->translator->trans('Unable to retrieve module %s information. Ignored.', [$module]), MarketplaceApiException::EMPTY_DATA_CODE);
return $result;
}

/**
* @throws MarketplaceApiException
*/
public function getModuleDetail(string $module): Module
{
$results = $this->getModuleDetails([$module]);
$value = $results[$module];
if ($value instanceof MarketplaceApiException) {
throw $value;
}

return Module::fromArray($data);
return $value;
}

/**
Expand Down
31 changes: 17 additions & 14 deletions classes/Task/Update/UninstallModules.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,24 +138,27 @@ private function warmUp(): int

$marketPlaceService = $this->container->getMarketplaceService();

$moduleNames = array_column($modulesList, 'name');
$moduleDetailsMap = $marketPlaceService->getModuleDetails($moduleNames);

$modulesToUninstallList = [];

foreach ($modulesList as $module) {
try {
$moduleDetails = $marketPlaceService->getModuleDetail($module['name']);

$moduleCompatibility = $marketPlaceService->findCompatibleModuleUpgrade(
$moduleDetails,
$targetVersion,
$module['currentVersion']
);

if (!$moduleCompatibility->isCompatible()) {
$modulesToUninstallList[] = $module['name'];
}
} catch (MarketplaceApiException $e) {
$this->logger->warning($e);
$moduleDetails = $moduleDetailsMap[$module['name']];
if ($moduleDetails instanceof MarketplaceApiException) {
$this->logger->warning($moduleDetails);
$this->container->getUpdateState()->setWarningDetected(true);
continue;
}

$moduleCompatibility = $marketPlaceService->findCompatibleModuleUpgrade(
$moduleDetails,
$targetVersion,
$module['currentVersion']
);

if (!$moduleCompatibility->isCompatible()) {
$modulesToUninstallList[] = $module['name'];
}
}

Expand Down
13 changes: 8 additions & 5 deletions classes/UpgradeSelfCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -798,21 +798,24 @@ public function getModulesRequiringAttention($mode = self::COMPLETE_SEARCH): arr
];
$modulesInstalled = $this->moduleAdapter->listModulesPresentInFolderAndInstalled();

$moduleNames = array_column($modulesInstalled, 'name');
$moduleDetailsMap = $this->marketplaceService->getModuleDetails($moduleNames);

foreach ($modulesInstalled as $localModule) {
$localModuleName = $localModule['name'];
$localVersion = $localModule['currentVersion'];
$result['compatibility'][$localModuleName] = null;

try {
$moduleDetails = $this->marketplaceService->getModuleDetail($localModuleName);
} catch (MarketplaceApiException $e) {
$result['uncertain_modules'][] = $localModule['name'];
$moduleDetails = $moduleDetailsMap[$localModuleName];
if ($moduleDetails instanceof MarketplaceApiException) {
$result['uncertain_modules'][] = $localModuleName;

if ($mode === self::QUICK_SEARCH) {
return $result;
}
continue;
}

$moduleCompatibility = $this->marketplaceService->findCompatibleModuleUpgrade(
$moduleDetails,
$this->destinationVersion,
Expand All @@ -822,7 +825,7 @@ public function getModulesRequiringAttention($mode = self::COMPLETE_SEARCH): arr
$result['compatibility'][$localModuleName] = $moduleCompatibility;

if (!$moduleCompatibility->isCompatible()) {
$result['incompatible_modules'][] = $localModule['name'];
$result['incompatible_modules'][] = $localModuleName;

if ($mode === self::QUICK_SEARCH) {
return $result;
Expand Down
156 changes: 156 additions & 0 deletions tests/unit/Services/MarketplaceServiceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
<?php
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License version 3.0
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/AFL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* @author PrestaShop SA and Contributors <contact@prestashop.com>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0
*/

namespace Services;

use PHPUnit\Framework\TestCase;
use PrestaShop\Module\AutoUpgrade\Exceptions\MarketplaceApiException;
use PrestaShop\Module\AutoUpgrade\Models\Module\Marketplace\Module;
use PrestaShop\Module\AutoUpgrade\Services\MarketplaceService;
use PrestaShop\Module\AutoUpgrade\UpgradeTools\Translator;

class TestableMarketplaceService extends MarketplaceService
{
/** @var array<string, string|false> */
public $stubResponses = [];

/** @var int */
public $fetchCallCount = 0;

/**
* @param string[] $toFetch
*
* @return array<string, string|false>
*/
protected function fetchModulesRaw(array $toFetch): array
{
++$this->fetchCallCount;

return array_intersect_key($this->stubResponses, array_flip($toFetch));
}
}

class MarketplaceServiceTest extends TestCase
{
/** @var TestableMarketplaceService */
private $service;

protected function setUp(): void
{
$translator = $this->createMock(Translator::class);
$translator->method('trans')->willReturnArgument(0);

$this->service = new TestableMarketplaceService($translator);
}

private function validModuleJson(): string
{
return json_encode([
'attributes' => [],
'compliance' => [],
'product' => [],
'technical_info' => [],
]);
}

public function testEmptyInputReturnsEmptyArray(): void
{
$result = $this->service->getModuleDetails([]);

$this->assertSame([], $result);
}

public function testSingleModuleValidJsonReturnsModule(): void
{
$this->service->stubResponses = ['foo' => $this->validModuleJson()];

$result = $this->service->getModuleDetails(['foo']);

$this->assertArrayHasKey('foo', $result);
$this->assertInstanceOf(Module::class, $result['foo']);
}

public function testSingleModuleFalseResponseReturnsApiNotCallableException(): void
{
$this->service->stubResponses = ['foo' => false];

$result = $this->service->getModuleDetails(['foo']);

$this->assertArrayHasKey('foo', $result);
$this->assertInstanceOf(MarketplaceApiException::class, $result['foo']);
$this->assertSame(MarketplaceApiException::API_NOT_CALLABLE_CODE, $result['foo']->getCode());
}

public function testSingleModuleInvalidJsonReturnsEmptyDataException(): void
{
$this->service->stubResponses = ['foo' => 'not-json'];

$result = $this->service->getModuleDetails(['foo']);

$this->assertArrayHasKey('foo', $result);
$this->assertInstanceOf(MarketplaceApiException::class, $result['foo']);
$this->assertSame(MarketplaceApiException::EMPTY_DATA_CODE, $result['foo']->getCode());
}

public function testMultipleModulesAllReturnedCorrectly(): void
{
$this->service->stubResponses = [
'foo' => $this->validModuleJson(),
'bar' => false,
'baz' => 'bad-json',
];

$result = $this->service->getModuleDetails(['foo', 'bar', 'baz']);

$this->assertCount(3, $result);
$this->assertInstanceOf(Module::class, $result['foo']);
$this->assertInstanceOf(MarketplaceApiException::class, $result['bar']);
$this->assertSame(MarketplaceApiException::API_NOT_CALLABLE_CODE, $result['bar']->getCode());
$this->assertInstanceOf(MarketplaceApiException::class, $result['baz']);
$this->assertSame(MarketplaceApiException::EMPTY_DATA_CODE, $result['baz']->getCode());
}

public function testSameNameCalledTwiceDoesNotRefetch(): void
{
$this->service->stubResponses = ['foo' => $this->validModuleJson()];

$this->service->getModuleDetails(['foo']);
$this->service->getModuleDetails(['foo']);

$this->assertSame(1, $this->service->fetchCallCount);
}

public function testGetModuleDetailDelegatesToGetModuleDetailsAndReturnsModule(): void
{
$this->service->stubResponses = ['foo' => $this->validModuleJson()];

$result = $this->service->getModuleDetail('foo');

$this->assertInstanceOf(Module::class, $result);
}

public function testGetModuleDetailThrowsWhenMapValueIsException(): void
{
$this->service->stubResponses = ['foo' => false];

$this->expectException(MarketplaceApiException::class);
$this->service->getModuleDetail('foo');
}
}
Loading