Completed
Push — master ( 38d6aa...018e09 )
by Andrii
11:07
created

CommandFactory::extractData()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
cc 2
nc 1
nop 1
1
<?php
2
declare(strict_types=1);
3
4
namespace hiapi\Core\commands;
5
6
use hiapi\commands\BulkCommand;
7
use hiapi\Core\Endpoint\Endpoint;
8
use hiapi\endpoints\Module\InOutControl\VO\Collection;
9
use hiapi\exceptions\ConfigurationException;
10
use hiapi\exceptions\HiapiException;
11
use Psr\Http\Message\ServerRequestInterface;
12
use yii\base\Model;
13
14
/**
15
 * Creates command by given data: name and request.
16
 * - takes data from POST or (if it is empty) from GET request,
17
 * - trims all the values and tries to load them to the command.
18
 */
19
class CommandFactory
20
{
21
    public function createByEndpoint(Endpoint $endpoint, ServerRequestInterface $request): Model
22
    {
23
        $data = $this->extractData($request);
24
        $command = $this->createCommand($endpoint);
25
        $successLoad = $command->load($data, '');
26
        if (!$successLoad && !empty($data)) {
27
            throw new HiapiException('Failed to load command data');
28
        }
29
30
        return $command;
31
    }
32
33
    private function createCommand(Endpoint $endpoint): Model
34
    {
35
        $inputType = $endpoint->inputType;
36
        if ($inputType instanceof Collection) {
0 ignored issues
show
Bug introduced by
The class hiapi\endpoints\Module\InOutControl\VO\Collection 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...
37
            $command = BulkCommand::of($inputType->getEntriesClass());
38
        } else {
39
            $command = new $inputType();
40
        }
41
        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...
42
            throw new ConfigurationException('This middleware can load only commands of Model class');
43
        }
44
45
        return $command;
46
    }
47
48
    private function extractData(ServerRequestInterface $request): array
49
    {
50
        $data = array_merge($request->getParsedBody(), $request->getQueryParams());
51
        array_walk_recursive($data, static function (&$value) {
52
            if (\is_string($value)) {
53
                $value = trim($value);
54
            }
55
        });
56
57
        return $data;
58
    }
59
}
60