|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PhpGitHooks\Module\JsonLint\Contract\Command; |
|
4
|
|
|
|
|
5
|
|
|
use Bruli\EventBusBundle\CommandBus\CommandHandlerInterface; |
|
6
|
|
|
use Bruli\EventBusBundle\CommandBus\CommandInterface; |
|
7
|
|
|
use Bruli\EventBusBundle\QueryBus\QueryBus; |
|
8
|
|
|
use PhpGitHooks\Module\Files\Contract\Query\JsonFilesExtractor; |
|
9
|
|
|
use PhpGitHooks\Module\JsonLint\Service\JsonLintToolExecutor; |
|
10
|
|
|
|
|
11
|
|
|
class JsonLintToolHandler implements CommandHandlerInterface |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* @var JsonLintToolExecutor |
|
15
|
|
|
*/ |
|
16
|
|
|
private $jsonLintToolExecutor; |
|
17
|
|
|
/** |
|
18
|
|
|
* @var QueryBus |
|
19
|
|
|
*/ |
|
20
|
|
|
private $queryBus; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* JsonLintTool constructor. |
|
24
|
|
|
* |
|
25
|
|
|
* @param JsonLintToolExecutor $jsonLintToolExecutor |
|
26
|
|
|
* @param QueryBus $queryBus |
|
27
|
|
|
*/ |
|
28
|
3 |
|
public function __construct( |
|
29
|
|
|
JsonLintToolExecutor $jsonLintToolExecutor, |
|
30
|
|
|
QueryBus $queryBus |
|
31
|
|
|
) { |
|
32
|
3 |
|
$this->jsonLintToolExecutor = $jsonLintToolExecutor; |
|
33
|
3 |
|
$this->queryBus = $queryBus; |
|
34
|
3 |
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @param array $files |
|
38
|
|
|
* @param string $errorMessage |
|
39
|
|
|
*/ |
|
40
|
3 |
|
private function execute(array $files, $errorMessage) |
|
41
|
|
|
{ |
|
42
|
3 |
|
$jsonFilesResponse = $this->queryBus->handle(new JsonFilesExtractor($files)); |
|
43
|
|
|
|
|
44
|
3 |
|
if (true === $this->jsonFilesExists($jsonFilesResponse->getFiles())) { |
|
45
|
2 |
|
$this->jsonLintToolExecutor->execute($jsonFilesResponse->getFiles(), $errorMessage); |
|
46
|
|
|
} |
|
47
|
2 |
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @param array $files |
|
51
|
|
|
* |
|
52
|
|
|
* @return bool |
|
53
|
|
|
*/ |
|
54
|
3 |
|
private function jsonFilesExists(array $files) |
|
55
|
|
|
{ |
|
56
|
3 |
|
return 0 < count($files); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @param CommandInterface|JsonLintTool $command |
|
61
|
|
|
*/ |
|
62
|
3 |
|
public function handle(CommandInterface $command) |
|
63
|
|
|
{ |
|
64
|
3 |
|
$this->execute($command->getFiles(), $command->getErrorMessage()); |
|
|
|
|
|
|
65
|
2 |
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: