1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-14 15:13:29 +01:00
invoiceninja/app/Services/Subscription/StepService.php
2024-03-01 13:43:11 +01:00

61 lines
2.1 KiB
PHP

<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2022. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Services\Subscription;
use App\Livewire\BillingPortal\Purchase;
class StepService
{
public static function mapToClassNames(string $steps): array
{
$classes = collect(Purchase::$dependencies)->mapWithKeys(fn($dependency, $class) => [$dependency['id'] => $class])->toArray();
return array_map(fn($step) => $classes[$step], explode(',', $steps));
}
public static function check(array $steps): array
{
$dependencies = Purchase::$dependencies;
$step_order = array_flip($steps);
$errors = [];
foreach ($steps as $step) {
$dependent = $dependencies[$step]['dependencies'] ?? [];
if (!empty($dependent) && !array_intersect($dependent, $steps)) {
$errors[] = ctrans('texts.step_dependency_fail', [
'step' => ctrans('texts.' . self::mapClassNameToString($step)),
'dependencies' => implode(', ', array_map(fn($dependency) => ctrans('texts.' . self::mapClassNameToString($dependency)), $dependent)),
]);
}
foreach ($dependent as $dependency) {
if (in_array($dependency, $steps) && $step_order[$dependency] > $step_order[$step]) {
$errors[] = ctrans('texts.step_dependency_order_fail', [
'step' => ctrans('texts.' . self::mapClassNameToString($step)),
'dependency' => implode(', ', array_map(fn($dependency) => ctrans('texts.' . self::mapClassNameToString($dependency)), $dependent)),
]);
}
}
}
return $errors;
}
public static function mapClassNameToString(string $class): string
{
$classes = collect(Purchase::$dependencies)->mapWithKeys(fn($dependency, $class) => [$class => $dependency['id']])->toArray();
return $classes[$class];
}
}