Passed
Pull Request — master (#1095)
by Aleksei
25:10
created

AbstractCore::callAction()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 37
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 28
CRAP Score 5

Importance

Changes 0
Metric Value
eloc 24
c 0
b 0
f 0
dl 0
loc 37
ccs 28
cts 28
cp 1
rs 9.2248
cc 5
nc 4
nop 3
crap 5
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Spiral\Core;
6
7
use Psr\Container\ContainerExceptionInterface;
8
use Psr\Container\ContainerInterface;
9
use Spiral\Core\Exception\ControllerException;
10
use Spiral\Core\Exception\Resolver\ArgumentResolvingException;
11
use Spiral\Core\Exception\Resolver\InvalidArgumentException;
12
use Spiral\Interceptors\Internal\ActionResolver;
13
14
/**
15
 * Provides ability to call controllers in IoC scope.
16
 *
17
 * Make sure to bind ScopeInterface in your container.
18
 *
19
 * @deprecated will be removed in Spiral v4.0
20
 */
21
abstract class AbstractCore implements CoreInterface
0 ignored issues
show
Deprecated Code introduced by
The interface Spiral\Core\CoreInterface has been deprecated: Use {@see HandlerInterface} instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

21
abstract class AbstractCore implements /** @scrutinizer ignore-deprecated */ CoreInterface

This interface has been deprecated. The supplier of the interface has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the interface will be removed and what other interface to use instead.

Loading history...
22
{
23
    /** @internal */
24
    protected ResolverInterface $resolver;
25
26 378
    public function __construct(
27
        /** @internal */
28
        protected ContainerInterface $container
29
    ) {
30
        // resolver is usually the container itself
31
        /** @psalm-suppress MixedAssignment */
32 378
        $this->resolver = $container->get(ResolverInterface::class);
33
    }
34
35
    /**
36
     * @psalm-assert class-string $controller
37
     * @psalm-assert non-empty-string $action
38
     */
39 79
    public function callAction(string $controller, string $action, array $parameters = []): mixed
40
    {
41 79
        $method = ActionResolver::pathToReflection($controller, $action);
42
43
        // Validate method
44 69
        if ($method->isStatic() || !$method->isPublic()) {
45 2
            throw new ControllerException(
0 ignored issues
show
Deprecated Code introduced by
The class Spiral\Core\Exception\ControllerException has been deprecated: will be removed in Spiral v4.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

45
            throw /** @scrutinizer ignore-deprecated */ new ControllerException(
Loading history...
46 2
                \sprintf(
47 2
                    'Invalid action `%s`->`%s`',
48 2
                    $method->getDeclaringClass()->getName(),
49 2
                    $method->getName(),
50 2
                ),
51 2
                ControllerException::BAD_ACTION
52 2
            );
53
        }
54
55
        try {
56 67
            $args = $this->resolveArguments($method, $parameters);
57 6
        } catch (ArgumentResolvingException|InvalidArgumentException $e) {
58 4
            throw new ControllerException(
0 ignored issues
show
Deprecated Code introduced by
The class Spiral\Core\Exception\ControllerException has been deprecated: will be removed in Spiral v4.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

58
            throw /** @scrutinizer ignore-deprecated */ new ControllerException(
Loading history...
59 4
                \sprintf('Missing/invalid parameter %s of `%s`->`%s`', $e->getParameter(), $controller, $action),
60 4
                ControllerException::BAD_ARGUMENT,
61 4
                $e,
62 4
            );
63 2
        } catch (ContainerExceptionInterface $e) {
64 1
            throw new ControllerException(
0 ignored issues
show
Deprecated Code introduced by
The class Spiral\Core\Exception\ControllerException has been deprecated: will be removed in Spiral v4.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

64
            throw /** @scrutinizer ignore-deprecated */ new ControllerException(
Loading history...
65 1
                $e->getMessage(),
66 1
                ControllerException::ERROR,
67 1
                $e,
68 1
            );
69
        }
70
71 61
        $container = $this->container;
72 61
        return ContainerScope::runScope(
73 61
            $container,
74
            /** @psalm-suppress MixedArgument */
75 61
            static fn (): mixed => $method->invokeArgs($container->get($controller), $args)
76 61
        );
77
    }
78
79 67
    protected function resolveArguments(\ReflectionMethod $method, array $parameters): array
80
    {
81 67
        foreach ($method->getParameters() as $parameter) {
82 39
            $name = $parameter->getName();
83
            if (
84 39
                \array_key_exists($name, $parameters) &&
85 39
                $parameters[$name] === null &&
86 39
                $parameter->isDefaultValueAvailable()
87
            ) {
88
                /** @psalm-suppress MixedAssignment */
89 2
                $parameters[$name] = $parameter->getDefaultValue();
90
            }
91
        }
92
93
        // getting the set of arguments should be sent to requested method
94 67
        return $this->resolver->resolveArguments($method, $parameters);
95
    }
96
}
97