Passed
Push — master ( 70c68f...c76078 )
by Alexander
06:58
created

ActionCaller   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 17
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 2
eloc 9
c 1
b 0
f 0
dl 0
loc 17
ccs 7
cts 7
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A process() 0 4 1
1
<?php
2
3
namespace Yiisoft\Router\Middleware;
4
5
use Psr\Container\ContainerInterface;
6
use Psr\Http\Message\ResponseInterface;
7
use Psr\Http\Message\ServerRequestInterface;
8
use Psr\Http\Server\MiddlewareInterface;
9
use Psr\Http\Server\RequestHandlerInterface;
10
use Yiisoft\Injector\Injector;
11
12
/**
13
 * ActionCaller maps a route to specified class instance and method.
14
 *
15
 * Dependencies are automatically injected into both method
16
 * and constructor based on types specified.
17
 */
18
final class ActionCaller implements MiddlewareInterface
19
{
20
    private string $class;
21
    private string $method;
22
    private ContainerInterface $container;
23
24 2
    public function __construct(string $class, string $method, ContainerInterface $container)
25
    {
26 2
        $this->class = $class;
27 2
        $this->method = $method;
28 2
        $this->container = $container;
29
    }
30
31 2
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
32
    {
33 2
        $controller = $this->container->get($this->class);
34 2
        return (new Injector($this->container))->invoke([$controller, $this->method], [$request, $handler]);
35
    }
36
}
37