Completed
Push — master ( 762599...5bbb37 )
by Alexander
25:00 queued 10:37
created

ActionCaller::process()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
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
class ActionCaller implements MiddlewareInterface
18
{
19
    private $class;
20
    private $method;
21
    private $container;
22
23
    public function __construct(string $class, string $method, ContainerInterface $container)
24
    {
25
        $this->class = $class;
26
        $this->method = $method;
27
        $this->container = $container;
28
    }
29
30
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
31
    {
32
        $controller = $this->container->get($this->class);
33
        return (new Injector($this->container))->invoke([$controller, $this->method], [$request, $handler]);
34
    }
35
36
    public static function __set_state(array $state): self
37
    {
38
        return new self($state['class'], $state['method'], $state['container']);
39
    }
40
}
41