Skip to content

Commit

Permalink
phpstan
Browse files Browse the repository at this point in the history
  • Loading branch information
sowbiba committed Nov 14, 2023
1 parent 3a2df53 commit eeb91f4
Showing 1 changed file with 56 additions and 18 deletions.
74 changes: 56 additions & 18 deletions src/DependencyBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,13 @@ public function areDependenciesMet()
{
$dependencies = $this->getDependencies(false);

foreach ($dependencies as $dependency) {
if (false === $dependency['installed'] || false === $dependency['enabled']) {
foreach ($dependencies as $dependencyName => $dependency) {
if (
!array_key_exists('installed', $dependency)
|| !array_key_exists('enabled', $dependency)
|| false === $dependency['installed']
|| false === $dependency['enabled']
) {
return false;
}
}
Expand Down Expand Up @@ -176,10 +181,14 @@ protected function buildRoutesForModule($moduleName)
{
$urls = [];
foreach (['install', 'enable', 'upgrade'] as $action) {
$urls[$action] = $this->router->generate('admin_module_manage_action', [
$route = $this->router->generate('admin_module_manage_action', [
'action' => $action,
'module_name' => $moduleName,
]);

if (is_string($route)) {
$urls[$action] = $route;
}
}

return $urls;
Expand Down Expand Up @@ -210,30 +219,40 @@ protected function buildRouter()
}

/**
* @return array<string,bool|string|int>|null
* @param bool $addRoutes
*
* @return array{
* "name": string,
* "installed": bool,
* "enabled": bool,
* "current_version": string,
* }|non-empty-array<string, bool|string>|null
*
* @throws \Exception
*/
protected function addMboInDependencies()
protected function addMboInDependencies($addRoutes = false)
{
if (!$this->isMboNeeded()) {
return null;
}

$mboStatus = (new Presenter())->present();

if ((bool) $mboStatus['isEnabled']) {
return null;
}

$mboRoutes = $this->buildRoutesForModule(Installer::MODULE_NAME);

return array_merge([
$specification = [
'current_version' => (string) $mboStatus['version'],
'installed' => (bool) $mboStatus['isInstalled'],
'enabled' => false,
'enabled' => (bool) $mboStatus['isEnabled'],
'name' => Installer::MODULE_NAME,
], $mboRoutes);
];

if (!$addRoutes) {
return $specification;
}

return array_merge(
$specification,
$this->buildRoutesForModule(Installer::MODULE_NAME)
);
}

/**
Expand All @@ -247,7 +266,12 @@ protected function isMboNeeded()
/**
* @param bool $addRoutes
*
* @return array{}|array{ps_mbo: array<string, bool|string|int>}
* @return array<string, array{
* "name": string,
* "installed": bool,
* "enabled": bool,
* "current_version": string,
* }>|array<ps_mbo, non-empty-array<string, bool|string>>
*
* @throws \Exception
*/
Expand All @@ -256,8 +280,13 @@ private function getDependencies($addRoutes = false)
$dependenciesContent = $this->getDependenciesSpecification();

if (empty($dependenciesContent['dependencies'])) {
$mboDependency = $this->addMboInDependencies($addRoutes);
if (null === $mboDependency) {
return [];
}

return [
Installer::MODULE_NAME => $this->addMboInDependencies(),
Installer::MODULE_NAME => $mboDependency,
];
}

Expand All @@ -269,6 +298,10 @@ private function getDependencies($addRoutes = false)

$dependencies = [];
foreach ($dependenciesContent['dependencies'] as $dependency) {
if (!is_array($dependency) || !array_key_exists('name', $dependency)) {
continue;
}

$dependencyData = \DbCore::getInstance()->getRow(
sprintf(
'SELECT `id_module`, `active`, `version` FROM `%smodule` WHERE `name` = "%s"',
Expand Down Expand Up @@ -303,7 +336,7 @@ private function getDependencies($addRoutes = false)

/**
* @return array{
* "dependencies": array{}
* "dependencies": array<string, string|int|bool>
* }
*
* @throws \Exception
Expand All @@ -318,7 +351,12 @@ private function getDependenciesSpecification()
if ($fileContent = file_get_contents($dependencyFile)) {
$dependenciesContent = json_decode($fileContent, true);
}
if (!isset($dependenciesContent) || !is_array($dependenciesContent) || json_last_error() != JSON_ERROR_NONE) {
if (
!isset($dependenciesContent)
|| !is_array($dependenciesContent)
|| !array_key_exists('dependencies', $dependenciesContent)
|| json_last_error() != JSON_ERROR_NONE
) {
throw new \Exception(self::DEPENDENCY_FILENAME . ' file may be malformed.');
}

Expand Down

0 comments on commit eeb91f4

Please sign in to comment.