Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
54.55% |
12 / 22 |
|
66.67% |
2 / 3 |
CRAP | |
0.00% |
0 / 1 |
DuplicationHydrator | |
54.55% |
12 / 22 |
|
66.67% |
2 / 3 |
16.61 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
hydrate | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
hydrateDataField | |
37.50% |
6 / 16 |
|
0.00% |
0 / 1 |
18.96 |
1 | <?php |
2 | |
3 | declare(strict_types=1); |
4 | |
5 | namespace App\Domain\Admin\Hydrator; |
6 | |
7 | use App\Domain\Admin\Dictionary\DuplicationTypeDictionary; |
8 | use App\Domain\Admin\Model; |
9 | use App\Domain\Admin\Model\Duplication; |
10 | use App\Domain\Registry\Repository as RegistryRepository; |
11 | |
12 | class DuplicationHydrator |
13 | { |
14 | /** |
15 | * @var RegistryRepository\Treatment |
16 | */ |
17 | private $treatmentRepository; |
18 | |
19 | /** |
20 | * @var RegistryRepository\Contractor |
21 | */ |
22 | private $contractorRepository; |
23 | |
24 | /** |
25 | * @var RegistryRepository\Mesurement |
26 | */ |
27 | private $mesurementRepository; |
28 | |
29 | /** |
30 | * @var RegistryRepository\Tool |
31 | */ |
32 | private $toolRepository; |
33 | |
34 | /** |
35 | * DuplicationDTOTransformer constructor. |
36 | */ |
37 | public function __construct( |
38 | RegistryRepository\Treatment $treatmentRepository, |
39 | RegistryRepository\Contractor $contractorRepository, |
40 | RegistryRepository\Mesurement $mesurementRepository, |
41 | RegistryRepository\Tool $toolRepository, |
42 | ) { |
43 | $this->treatmentRepository = $treatmentRepository; |
44 | $this->contractorRepository = $contractorRepository; |
45 | $this->mesurementRepository = $mesurementRepository; |
46 | $this->toolRepository = $toolRepository; |
47 | } |
48 | |
49 | /** |
50 | * Hydrate a Duplication Model thanks to it related fields. |
51 | */ |
52 | public function hydrate(Duplication $duplication): Duplication |
53 | { |
54 | $this->hydrateDataField($duplication); |
55 | |
56 | return $duplication; |
57 | } |
58 | |
59 | /** |
60 | * Add every data ids (converted as object) from DuplicationFormDTO to Duplication. |
61 | */ |
62 | protected function hydrateDataField(Duplication $model): void |
63 | { |
64 | switch ($model->getType()) { |
65 | case DuplicationTypeDictionary::KEY_TREATMENT: |
66 | $repository = $this->treatmentRepository; |
67 | break; |
68 | case DuplicationTypeDictionary::KEY_CONTRACTOR: |
69 | $repository = $this->contractorRepository; |
70 | break; |
71 | case DuplicationTypeDictionary::KEY_MESUREMENT: |
72 | $repository = $this->mesurementRepository; |
73 | break; |
74 | case DuplicationTypeDictionary::KEY_TOOL: |
75 | $repository = $this->toolRepository; |
76 | break; |
77 | default: |
78 | throw new \RuntimeException('Data object type is not supported'); |
79 | } |
80 | |
81 | foreach ($model->getDataIds() as $dataId) { |
82 | $model->addData($repository->findOneById($dataId)); |
83 | } |
84 | } |
85 | } |