Completed
Push — master ( 003b5e...76542b )
by Woody
38:06 queued 22:13
created

ActionHandler   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%
Metric Value
wmc 6
lcom 1
cbo 2
dl 0
loc 107
ccs 21
cts 21
cp 1
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A __invoke() 0 12 1
A handle() 0 14 1
A resolve() 0 4 1
A payload() 0 7 1
A response() 0 8 1
1
<?php
2
3
namespace Equip\Handler;
4
5
use Arbiter\Action;
6
use Psr\Http\Message\ServerRequestInterface;
7
use Psr\Http\Message\ResponseInterface;
8
use Relay\ResolverInterface;
9
use Equip\Adr\PayloadInterface;
10
use Equip\Adr\DomainInterface;
11
use Equip\Adr\InputInterface;
12
use Equip\Adr\ResponderInterface;
13
14
class ActionHandler
15
{
16
    const ACTION_ATTRIBUTE = 'equip/adr:action';
17
18
    /**
19
     * @var ResolverInterface
20
     */
21
    private $resolver;
22
23
    /**
24
     * @param ResolverInterface $resolver
25
     */
26 1
    public function __construct(ResolverInterface $resolver)
27
    {
28 1
        $this->resolver = $resolver;
29 1
    }
30
31
    /**
32
     * @param ServerRequestInterface $request
33
     * @param ResponseInterface $response
34
     * @param callable $next
35
     */
36 1
    public function __invoke(
37
        ServerRequestInterface $request,
38
        ResponseInterface $response,
39
        callable $next
40
    ) {
41 1
        $action = $request->getAttribute(self::ACTION_ATTRIBUTE);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
42 1
        $request = $request->withoutAttribute(self::ACTION_ATTRIBUTE);
43
44 1
        $response = $this->handle($action, $request, $response);
45
46 1
        return $next($request, $response);
47
    }
48
49
    /**
50
     * Use the action collaborators to get a response.
51
     *
52
     * @param Action $action
53
     * @param ServerRequestInterface $request
54
     * @param ResponseInterface $response
55
     *
56
     * @return ResponseInterface
57
     */
58 1
    private function handle(
59
        Action $action,
60
        ServerRequestInterface $request,
61
        ResponseInterface $response
62
    ) {
63 1
        $domain = $this->resolve($action->getDomain());
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 4 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
64 1
        $input = $this->resolve($action->getInput());
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 5 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
65 1
        $responder = $this->resolve($action->getResponder());
66
67 1
        $payload = $this->payload($domain, $input, $request);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
68 1
        $response = $this->response($responder, $request, $response, $payload);
69
70 1
        return $response;
71
    }
72
73
    /**
74
     * Resolve the class spec into an object.
75
     *
76
     * @param string $spec
77
     *
78
     * @return object
79
     */
80 1
    private function resolve($spec)
81
    {
82 1
        return call_user_func($this->resolver, $spec);
83
    }
84
85
    /**
86
     * Execute the domain to get a payload using input from the request.
87
     *
88
     * @param DomainInterface $domain
89
     * @param InputInterface $input
90
     * @param ServerRequestInterface $request
91
     *
92
     * @return PayloadInterface
93
     */
94 1
    private function payload(
95
        DomainInterface $domain,
96
        InputInterface $input,
97
        ServerRequestInterface $request
98
    ) {
99 1
        return $domain($input($request));
100
    }
101
102
    /**
103
     * Execute the responder to marshall the payload into the response.
104
     *
105
     * @param ResponderInterface $responder
106
     * @param ServerRequestInterface $request
107
     * @param ResponseInterface $response
108
     * @param PayloadInterface $payload
109
     *
110
     * @return ResponseInterface
111
     */
112 1
    private function response(
113
        ResponderInterface $responder,
114
        ServerRequestInterface $request,
115
        ResponseInterface $response,
116
        PayloadInterface $payload
117
    ) {
118 1
        return $responder($request, $response, $payload);
119
    }
120
}
121