1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the BenGorFile package. |
5
|
|
|
* |
6
|
|
|
* (c) Beñat Espiña <[email protected]> |
7
|
|
|
* (c) Gorka Laucirica <[email protected]> |
8
|
|
|
* |
9
|
|
|
* For the full copyright and license information, please view the LICENSE |
10
|
|
|
* file that was distributed with this source code. |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace BenGorFile\File\Application\Query; |
14
|
|
|
|
15
|
|
|
use BenGorFile\File\Application\DataTransformer\FileDataTransformer; |
16
|
|
|
use BenGorFile\File\Domain\Model\File; |
17
|
|
|
use BenGorFile\File\Domain\Model\FileRepository; |
18
|
|
|
use BenGorFile\File\Domain\Model\FileSpecificationFactory; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @author Beñat Espiña <[email protected]> |
22
|
|
|
*/ |
23
|
|
View Code Duplication |
class ListFilesOfIdsHandler |
|
|
|
|
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* The file data transformer. |
27
|
|
|
* |
28
|
|
|
* @var FileDataTransformer |
29
|
|
|
*/ |
30
|
|
|
private $dataTransformer; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* The file repository. |
34
|
|
|
* |
35
|
|
|
* @var FileRepository |
36
|
|
|
*/ |
37
|
|
|
private $repository; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* The file specification factory. |
41
|
|
|
* |
42
|
|
|
* @var FileSpecificationFactory |
43
|
|
|
*/ |
44
|
|
|
private $specificationFactory; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Constructor. |
48
|
|
|
* |
49
|
|
|
* @param FileRepository $aRepository The file repository |
50
|
|
|
* @param FileSpecificationFactory $fileSpecificationFactory The file specification factory |
51
|
|
|
* @param FileDataTransformer $aDataTransformer The file data transformer |
52
|
|
|
*/ |
53
|
|
|
public function __construct( |
54
|
|
|
FileRepository $aRepository, |
55
|
|
|
FileSpecificationFactory $fileSpecificationFactory, |
56
|
|
|
FileDataTransformer $aDataTransformer |
57
|
|
|
) { |
58
|
|
|
$this->repository = $aRepository; |
59
|
|
|
$this->specificationFactory = $fileSpecificationFactory; |
60
|
|
|
$this->dataTransformer = $aDataTransformer; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Handles the given query. |
65
|
|
|
* |
66
|
|
|
* @param ListFilesOfIdsQuery $aQuery The query |
67
|
|
|
* |
68
|
|
|
* @return mixed |
69
|
|
|
*/ |
70
|
|
|
public function __invoke(ListFilesOfIdsQuery $aQuery) |
71
|
|
|
{ |
72
|
|
|
$offset = $aQuery->limit() * ($aQuery->page() - 1); |
73
|
|
|
|
74
|
|
|
$files = $this->repository->query( |
75
|
|
|
$this->specificationFactory->buildListOfIdsSpecification( |
76
|
|
|
$aQuery->ids(), |
77
|
|
|
$offset, |
78
|
|
|
$aQuery->limit() |
79
|
|
|
) |
80
|
|
|
); |
81
|
|
|
|
82
|
|
|
return array_map(function (File $file) { |
83
|
|
|
$this->dataTransformer->write($file); |
84
|
|
|
|
85
|
|
|
return $this->dataTransformer->read(); |
86
|
|
|
}, $files); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
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.