1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* (c) Tomasz Kunicki <[email protected]> |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
*/ |
8
|
|
|
namespace Domain\Material\UseCase\ListMaterial; |
9
|
|
|
|
10
|
|
|
use Domain\Common\UseCase\ResponderAwareTrait; |
11
|
|
|
use Domain\Common\UseCase\ResponderInterface; |
12
|
|
|
use Domain\Material\Enitity\MaterialInterface; |
13
|
|
|
use Domain\Material\Repository\MaterialRepositoryInterface; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Class ListMaterialsUseCase |
17
|
|
|
* |
18
|
|
|
* @package Domain\Material\UseCase\ListMaterial |
19
|
|
|
*/ |
20
|
|
|
class ListMaterialUseCase implements ResponderInterface |
21
|
|
|
{ |
22
|
|
|
use ResponderAwareTrait; |
23
|
|
|
|
24
|
|
|
/** @var MaterialRepositoryInterface */ |
25
|
|
|
private $materialsRepository; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* ListMaterialsUseCase constructor. |
29
|
|
|
* |
30
|
|
|
* @param MaterialRepositoryInterface $materialsRepository |
31
|
|
|
*/ |
32
|
|
|
public function __construct(MaterialRepositoryInterface $materialsRepository) |
33
|
|
|
{ |
34
|
|
|
$this->materialsRepository = $materialsRepository; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function execute() |
38
|
|
|
{ |
39
|
|
|
$materials = $this->materialsRepository->findAll(); |
40
|
|
|
$items = $this->fetchItems($materials); |
41
|
|
|
$this->listFetched($items); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param $materials |
46
|
|
|
* |
47
|
|
|
* @return ListMaterialItem[] |
48
|
|
|
*/ |
49
|
|
|
private function fetchItems($materials) |
50
|
|
|
{ |
51
|
|
|
$list = []; |
52
|
|
|
/** @var MaterialInterface $material */ |
53
|
|
|
foreach ($materials as $material) { |
54
|
|
|
$unitName = null; |
55
|
|
|
$unit = $material->getUnit(); |
56
|
|
|
if (!is_null($unit)) { |
57
|
|
|
$unitName = $unit->getName(); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
$list[] = new ListMaterialItem( |
61
|
|
|
$material->getId(), |
62
|
|
|
$material->getName(), |
63
|
|
|
$material->getCode(), |
64
|
|
|
$unitName, |
65
|
|
|
$material->getCategory()->getName() |
66
|
|
|
); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return $list; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param array $items |
74
|
|
|
*/ |
75
|
|
|
private function listFetched(array $items) |
76
|
|
|
{ |
77
|
|
|
/** @var ListMaterialResponderInterface $responder */ |
78
|
|
|
foreach ($this->responders as $responder) { |
79
|
|
|
$responder->listFetched(new ListMaterialResponse($items)); |
|
|
|
|
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
} |
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: