Passed
Push — master ( 035a69...ef3bdc )
by Kirill
03:22
created

AbstractCore::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 6
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
/**
4
 * Spiral Framework.
5
 *
6
 * @license   MIT
7
 * @author    Anton Titov (Wolfy-J)
8
 */
9
10
declare(strict_types=1);
11
12
namespace Spiral\Core;
13
14
use Psr\Container\ContainerExceptionInterface;
15
use Psr\Container\ContainerInterface;
16
use Spiral\Core\Exception\Container\ArgumentException;
17
use Spiral\Core\Exception\ControllerException;
18
19
/**
20
 * Provides ability to call controllers in IoC scope.
21
 *
22
 * Make sure to bind ScopeInterface in your container.
23
 */
24
abstract class AbstractCore implements CoreInterface
25
{
26
    /** @var ContainerInterface @internal */
27
    protected $container;
28
29
    /** @var ResolverInterface @internal */
30
    protected $resolver;
31
32
    /**
33
     * @param ContainerInterface $container
34
     */
35
    public function __construct(ContainerInterface $container)
36
    {
37
        $this->container = $container;
38
39
        // resolver is usually the container itself
40
        $this->resolver = $container->get(ResolverInterface::class);
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    public function callAction(string $controller, string $action, array $parameters = [])
47
    {
48
        try {
49
            $method = new \ReflectionMethod($controller, $action);
50
        } catch (\ReflectionException $e) {
51
            throw new ControllerException(
52
                "Invalid action `{$controller}`->`{$action}`",
53
                ControllerException::BAD_ACTION,
54
                $e
55
            );
56
        }
57
58
        if ($method->isStatic() || !$method->isPublic()) {
59
            throw new ControllerException(
60
                "Invalid action `{$controller}`->`{$action}`",
61
                ControllerException::BAD_ACTION
62
            );
63
        }
64
65
        try {
66
            // getting the set of arguments should be sent to requested method
67
            $args = $this->resolver->resolveArguments($method, $parameters);
68
        } catch (ArgumentException $e) {
69
            throw new ControllerException(
70
                "Missing/invalid parameter '{$e->getParameter()->name}' of  `{$controller}`->`{$action}`",
71
                ControllerException::BAD_ARGUMENT
72
            );
73
        } catch (ContainerExceptionInterface $e) {
74
            throw new ControllerException(
75
                $e->getMessage(),
0 ignored issues
show
Bug introduced by
The method getMessage() does not exist on Psr\Container\ContainerExceptionInterface. It seems like you code against a sub-type of said class. However, the method does not exist in Psr\Container\NotFoundExceptionInterface. Are you sure you never get one of those? ( Ignorable by Annotation )

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

75
                $e->/** @scrutinizer ignore-call */ 
76
                    getMessage(),
Loading history...
76
                ControllerException::ERROR,
77
                $e
78
            );
79
        }
80
81
        return ContainerScope::runScope($this->container, function () use ($controller, $method, $args) {
82
            return $method->invokeArgs($this->container->get($controller), $args);
83
        });
84
    }
85
}
86