Completed
Push — master ( 9a342a...a162ae )
by Andrii
13:06
created

CommandForEndpointMiddleware::createCommand()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.6666
c 0
b 0
f 0
cc 3
nc 4
nop 1
1
<?php
2
namespace hiapi\Core\Http\Psr15\Middleware;
3
4
use Exception;
5
use hiapi\commands\BulkCommand;
6
use hiapi\Core\Endpoint\Endpoint;
7
use hiapi\endpoints\Module\InOutControl\VO\Collection;
8
use Psr\Http\Message\ResponseInterface;
9
use Psr\Http\Message\ServerRequestInterface;
10
use Psr\Http\Server\MiddlewareInterface;
11
use Psr\Http\Server\RequestHandlerInterface;
12
use yii\base\Model;
13
14
/**
15
 * Class CommandForEndpointMiddleware takes data from POST or (if it is empty) from GET request,
16
 * trims all the values and tries to load them to the command.
17
 *
18
 * @author Dmytro Naumenko <[email protected]>
19
 */
20
class CommandForEndpointMiddleware implements MiddlewareInterface
21
{
22
    /**
23
     * @inheritDoc
24
     */
25
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
26
    {
27
        $data = array_merge($request->getParsedBody() ?: $request->getQueryParams());
28
        array_walk_recursive($data, static function (&$value) {
29
            if (\is_string($value)) {
30
                $value = trim($value);
31
            }
32
        });
33
34
        $command = $this->createCommand($request);
35
        $successLoad = $command->load($data, '');
36
        if (!$successLoad && !empty($data)) {
37
            // TODO: specific exception
38
            throw new Exception('Failed to load command');
39
        }
40
41
        return $handler->handle(
42
            $request->withAttribute(self::class, $command)
43
        );
44
    }
45
46
    private function createCommand(ServerRequestInterface $request): Model
47
    {
48
        /** @var Endpoint $endpoint */
49
        $endpoint = $request->getAttribute(ResolveEndpointMiddleware::class);
50
51
        $inputType = $endpoint->getInputType();
52
        if ($inputType instanceof Collection) {
53
            $command = BulkCommand::of($inputType->getEntriesClass());
54
        } else {
55
            $command = new $inputType();
56
        }
57
        if (!$command instanceof Model) {
0 ignored issues
show
Bug introduced by
The class yii\base\Model does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
58
            // TODO: specific exception
59
            throw new Exception('This middleware can load only commands of Model class');
60
        }
61
62
        return $command;
63
    }
64
}
65