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\Category\UseCase\ListCategory; |
9
|
|
|
|
10
|
|
|
use Domain\Category\Entity\CategoryInterface; |
11
|
|
|
use Domain\Category\Repository\CategoryRepositoryInterface; |
12
|
|
|
use Domain\Common\UseCase\ResponderAwareInterface; |
13
|
|
|
use Domain\Common\UseCase\ResponderAwareTrait; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Class ListCategoryUseCase |
17
|
|
|
* |
18
|
|
|
* @package Domain\Category\UseCase\ListCategory |
19
|
|
|
*/ |
20
|
|
|
class ListCategoryUseCase implements ResponderAwareInterface |
21
|
|
|
{ |
22
|
|
|
use ResponderAwareTrait; |
23
|
|
|
|
24
|
|
|
/** @var CategoryRepositoryInterface */ |
25
|
|
|
private $categoryRepository; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* ListCategoryUseCase constructor. |
29
|
|
|
* |
30
|
|
|
* @param CategoryRepositoryInterface $categoryRepository |
31
|
|
|
*/ |
32
|
|
|
public function __construct(CategoryRepositoryInterface $categoryRepository) |
33
|
|
|
{ |
34
|
|
|
$this->categoryRepository = $categoryRepository; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* |
39
|
|
|
*/ |
40
|
|
|
public function execute() |
41
|
|
|
{ |
42
|
|
|
$categories = $this->categoryRepository->findAll(); |
43
|
|
|
$items = $this->fetchCategoriesItems($categories); |
44
|
|
|
$this->listFetched($items); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param $categories |
49
|
|
|
* @return CategoryListItem[] |
50
|
|
|
*/ |
51
|
|
|
private function fetchCategoriesItems($categories) |
52
|
|
|
{ |
53
|
|
|
$list = []; |
54
|
|
|
/** @var CategoryInterface $category */ |
55
|
|
|
foreach ($categories as $category) { |
56
|
|
|
$list[] = new CategoryListItem($category->getId(), $category->getName()); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
return $list; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param $items |
64
|
|
|
*/ |
65
|
|
|
private function listFetched($items) |
66
|
|
|
{ |
67
|
|
|
/** @var ListCategoryResponderInterface $responder */ |
68
|
|
|
foreach ($this->responders as $responder) { |
69
|
|
|
$responder->listFetched(new ListCategoryResponse($items)); |
|
|
|
|
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
} |
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: