1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-15 23:52:33 +01:00
invoiceninja/app/Services/Subscription/StepService.php

71 lines
2.5 KiB
PHP
Raw Normal View History

<?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
{
2024-06-14 09:09:44 +02:00
$classes = collect(Purchase::$dependencies)->mapWithKeys(fn ($dependency, $class) => [$dependency['id'] => $class])->toArray();
2024-06-14 09:09:44 +02:00
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)),
2024-06-14 09:09:44 +02:00
'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)),
2024-06-14 09:09:44 +02:00
'dependency' => implode(', ', array_map(fn ($dependency) => ctrans('texts.' . self::mapClassNameToString($dependency)), $dependent)),
]);
}
}
}
$auth = collect($dependencies)
->filter(fn ($dependency) => str_starts_with($dependency['id'], 'auth.'))
->keys()
->toArray();
if (count(array_intersect($auth, $steps)) === 0) {
$errors[] = ctrans('texts.step_authentication_fail');
}
return $errors;
}
public static function mapClassNameToString(string $class): string
{
2024-06-14 09:09:44 +02:00
$classes = collect(Purchase::$dependencies)->mapWithKeys(fn ($dependency, $class) => [$class => $dependency['id']])->toArray();
return $classes[$class];
}
2024-06-14 09:09:44 +02:00
}