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
|
4 |
|
public function __construct(DescriptionInterface $description, SchemaValidator $schemaValidator = null) |
28
|
|
|
{ |
29
|
4 |
|
$this->description = $description; |
30
|
4 |
|
$this->validator = $schemaValidator ?: new SchemaValidator(); |
31
|
4 |
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param callable $handler |
35
|
|
|
* @return \Closure |
36
|
|
|
*/ |
37
|
|
|
public function __invoke(callable $handler) |
38
|
|
|
{ |
39
|
4 |
|
return function (CommandInterface $command) use ($handler) { |
40
|
4 |
|
$errors = []; |
41
|
4 |
|
$operation = $this->description->getOperation($command->getName()); |
42
|
|
|
|
43
|
4 |
|
foreach ($operation->getParams() as $name => $schema) { |
44
|
2 |
|
$value = $command[$name]; |
45
|
|
|
|
46
|
2 |
|
if ($value) { |
47
|
1 |
|
$value = $schema->filter($value); |
48
|
1 |
|
} |
49
|
|
|
|
50
|
2 |
View Code Duplication |
if (! $this->validator->validate($schema, $value)) { |
|
|
|
|
51
|
1 |
|
$errors = array_merge($errors, $this->validator->getErrors()); |
52
|
2 |
|
} elseif ($value !== $command[$name]) { |
53
|
|
|
// Update the config value if it changed and no validation errors were encountered. |
54
|
|
|
// This happen when the user extending an operation |
55
|
|
|
// See https://github.com/guzzle/guzzle-services/issues/145 |
56
|
1 |
|
$command[$name] = $value; |
57
|
1 |
|
} |
58
|
4 |
|
} |
59
|
|
|
|
60
|
4 |
|
if ($params = $operation->getAdditionalParameters()) { |
61
|
1 |
|
foreach ($command->toArray() as $name => $value) { |
62
|
|
|
// It's only additional if it isn't defined in the schema |
63
|
1 |
|
if (! $operation->hasParam($name)) { |
64
|
|
|
// Always set the name so that error messages are useful |
65
|
1 |
|
$params->setName($name); |
66
|
1 |
View Code Duplication |
if (! $this->validator->validate($params, $value)) { |
|
|
|
|
67
|
1 |
|
$errors = array_merge($errors, $this->validator->getErrors()); |
68
|
1 |
|
} elseif ($value !== $command[$name]) { |
69
|
|
|
$command[$name] = $value; |
70
|
|
|
} |
71
|
1 |
|
} |
72
|
1 |
|
} |
73
|
1 |
|
} |
74
|
|
|
|
75
|
4 |
|
if ($errors) { |
|
|
|
|
76
|
2 |
|
throw new CommandException('Validation errors: ' . implode("\n", $errors), $command); |
77
|
|
|
} |
78
|
|
|
|
79
|
2 |
|
return $handler($command); |
80
|
4 |
|
}; |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.