Completed
Push — master ( 22a756...a8d83f )
by John
02:03
created

src/DefaultPipe.php (1 issue)

Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php declare(strict_types = 1);
2
/*
3
 * This file is part of the KleijnWeb\PhpApi\Descriptions package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
namespace KleijnWeb\PhpApi\Middleware;
9
10
use Equip\Dispatch\MiddlewarePipe;
11
use Interop\Http\ServerMiddleware\DelegateInterface;
12
use KleijnWeb\PhpApi\Descriptions\Description\Repository;
13
use KleijnWeb\PhpApi\Middleware\Body\JsonBodyParser;
14
use KleijnWeb\PhpApi\Middleware\Body\JsonBodySerializer;
15
use KleijnWeb\PhpApi\Descriptions\Request\RequestParameterAssembler;
16
use KleijnWeb\PhpApi\Hydrator\ClassNameResolver;
17
use KleijnWeb\PhpApi\Hydrator\ObjectHydrator;
18
use Psr\Http\Message\ServerRequestInterface;
19
20
class DefaultPipe extends MiddlewarePipe
21
{
22
    /**
23
     * @var ResultSerializer
24
     */
25
    private $resultSerializer;
26
27
    /**
28
     * @var bool
29
     */
30
    private $responding;
31
32
    /**
33
     * @param Repository $repository
34
     * @param array      $commands
35
     * @param array      $complexTypeNs
36
     * @param bool       $responding
37
     */
38
    public function __construct(
39
        Repository $repository,
40
        array $commands,
41
        array $complexTypeNs = [],
42
        bool $responding = true
43
    ) {
44
        $assembler      = new RequestParameterAssembler();
45
        $jsonParser     = new JsonBodyParser();
46
        $jsonSerializer = new JsonBodySerializer();
47
        $hydrator       = new ObjectHydrator(new ClassNameResolver($complexTypeNs));
48
49
        $this->resultSerializer = new ResultSerializer($jsonSerializer);
50
51
        $default = [
52
            new OperationMatcher($repository),
53
            new BodyParsing($jsonParser),
54
            new ParameterAssembler($assembler, $jsonParser),
0 ignored issues
show
The call to ParameterAssembler::__construct() has too many arguments starting with $jsonParser.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
55
            new MessageValidator($assembler, $jsonSerializer),
56
            new ParameterHydrator($hydrator),
57
            new CommandDispatcher($commands),
58
            new ResponseBodyDehydrator($hydrator)
59
        ];
60
61
        parent::__construct($default);
62
63
        $this->responding = $responding;
64
    }
65
66
    /**
67
     * @return boolean
68
     */
69
    public function isResponding(): bool
70
    {
71
        return $this->responding;
72
    }
73
74
    /**
75
     * @param boolean $responding
76
     * @return DefaultPipe
77
     */
78
    public function setResponding(bool $responding): DefaultPipe
79
    {
80
        $this->responding = $responding;
81
82
        return $this;
83
    }
84
85
    /**
86
     * @inheritdoc
87
     */
88
    public function dispatch(ServerRequestInterface $request, callable $default)
89
    {
90
        $this->appendResponderIfResponding();
91
92
        return parent::dispatch($request, $default);
93
    }
94
95
    /**
96
     * @inheritdoc
97
     */
98
    public function process(ServerRequestInterface $request, DelegateInterface $nextContainerDelegate)
99
    {
100
        $this->appendResponderIfResponding();
101
102
        return parent::process($request, $nextContainerDelegate);
103
    }
104
105
    private function appendResponderIfResponding()
106
    {
107
        if ($this->isResponding()) {
108
            $this->append($this->resultSerializer);
109
        }
110
    }
111
}