Completed
Pull Request — master (#136)
by
unknown
24:02
created

ValidatedDescriptionHandler::__invoke()   D

Complexity

Conditions 9
Paths 1

Size

Total Lines 38
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 26
CRAP Score 9

Importance

Changes 0
Metric Value
dl 0
loc 38
ccs 26
cts 26
cp 1
rs 4.909
c 0
b 0
f 0
cc 9
eloc 19
nc 1
nop 1
crap 9
1
<?php namespace GuzzleHttp\Command\Guzzle\Handler;
2
3
use GuzzleHttp\Command\CommandInterface;
4
use GuzzleHttp\Command\Exception\CommandException;
5
use GuzzleHttp\Command\Guzzle\DescriptionInterface;
6
use GuzzleHttp\Command\Guzzle\SchemaValidator;
7
8
/**
9
 * Handler used to validate command input against a service description.
10
 *
11
 * @author Stefano Kowalke <[email protected]>
12
 */
13
class ValidatedDescriptionHandler
14
{
15
    /** @var SchemaValidator $validator */
16
    private $validator;
17
18
    /** @var DescriptionInterface $description */
19
    private $description;
20
21
    /**
22
     * ValidatedDescriptionHandler constructor.
23
     *
24
     * @param DescriptionInterface $description
25
     * @param SchemaValidator|null $schemaValidator
26
     */
27 5
    public function __construct(DescriptionInterface $description, SchemaValidator $schemaValidator = null)
28
    {
29 5
        $this->description = $description;
30 5
        $this->validator = $schemaValidator ?: new SchemaValidator();
31 5
    }
32
33
    /**
34
     * @param callable $handler
35
     * @return \Closure
36
     */
37
    public function __invoke(callable $handler)
38
    {
39 5
        return function (CommandInterface $command) use ($handler) {
40 5
            $errors = [];
41 5
            $operation = $this->description->getOperation($command->getName());
42
43 5
            foreach ($operation->getParams() as $name => $schema) {
44 3
                $value = $command[$name];
45
46 3
                if ($value) {
47 2
                    $value = $schema->filter($value);
48 2
                }
49
50 3
                if (! $this->validator->validate($schema, $value)) {
51 1
                    $errors = array_merge($errors, $this->validator->getErrors());
52 1
                }
53 5
            }
54
55 5
            if ($params = $operation->getAdditionalParameters()) {
56 1
                foreach ($command->toArray() as $name => $value) {
57
                    // It's only additional if it isn't defined in the schema
58 1
                    if (! $operation->hasParam($name)) {
59
                        // Always set the name so that error messages are useful
60 1
                        $params->setName($name);
61 1
                        if (! $this->validator->validate($params, $value)) {
62 1
                            $errors = array_merge($errors, $this->validator->getErrors());
63 1
                        }
64 1
                    }
65 1
                }
66 1
            }
67
68 5
            if ($errors) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $errors of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
69 2
                throw new CommandException('Validation errors: ' . implode("\n", $errors), $command);
70
            }
71
72 3
            return $handler($command);
73 5
        };
74
    }
75
}
76