Passed
Pull Request — master (#137)
by Zhukov
03:58
created

ActionCaller   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 17
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

2 Methods

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