|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of forecast.it.fill project. |
|
7
|
|
|
* (c) Patrick Jaja <[email protected]> |
|
8
|
|
|
* This source file is subject to the MIT license that is bundled |
|
9
|
|
|
* with this source code in the file LICENSE. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace ForecastAutomation\PeriodicalActivity\Business; |
|
13
|
|
|
|
|
14
|
|
|
use ForecastAutomation\Log\LogFacade; |
|
15
|
|
|
use ForecastAutomation\PeriodicalActivity\Shared\Dto\PeriodicalActivityConfigDto; |
|
16
|
|
|
use JsonSchema\Validator; |
|
17
|
|
|
|
|
18
|
|
|
class PeriodicalActivityConfigReader |
|
19
|
|
|
{ |
|
20
|
|
|
public function __construct( |
|
21
|
|
|
private string $periodicalActivityConfig, |
|
22
|
|
|
private string $periodicalActivitySchemaPath, |
|
23
|
|
|
private Validator $validator, |
|
24
|
|
|
private LogFacade $logFacade, |
|
25
|
|
|
) { |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @return PeriodicalActivityConfigDto[] array |
|
30
|
|
|
*/ |
|
31
|
|
|
public function readPeriodicalConfig(string $periodicalDate): array |
|
32
|
|
|
{ |
|
33
|
|
|
$periodicalConfigDtoCollection = []; |
|
34
|
|
|
|
|
35
|
|
|
try { |
|
36
|
|
|
//ToDo: Move to separate Plugins in Business Layer to reduce class complexity and responsibility |
|
37
|
|
|
$periodicalConfigCollection = json_decode( |
|
38
|
|
|
$this->periodicalActivityConfig, |
|
39
|
|
|
null, |
|
40
|
|
|
JSON_PARTIAL_OUTPUT_ON_ERROR, |
|
41
|
|
|
JSON_THROW_ON_ERROR |
|
42
|
|
|
); |
|
43
|
|
|
|
|
44
|
|
|
$this->validator->validate( |
|
45
|
|
|
$periodicalConfigCollection, |
|
46
|
|
|
(object) ['$ref' => $this->periodicalActivitySchemaPath] |
|
47
|
|
|
); |
|
48
|
|
|
|
|
49
|
|
|
if ($this->validator->isValid()) { |
|
50
|
|
|
$periodicalConfigDtoCollection = $this->filterPeriodicalConfigDto( |
|
51
|
|
|
$this->periodicalConfigDtoMapper($periodicalConfigCollection), |
|
52
|
|
|
$periodicalDate |
|
53
|
|
|
); |
|
54
|
|
|
} else { |
|
55
|
|
|
$this->logFacade->error( |
|
56
|
|
|
\sprintf( |
|
57
|
|
|
'JSON (%s) is not validate. %s', |
|
58
|
|
|
$this->periodicalActivityConfig, |
|
59
|
|
|
json_encode($this->validator->getErrors(), JSON_THROW_ON_ERROR) |
|
60
|
|
|
) |
|
61
|
|
|
); |
|
62
|
|
|
} |
|
63
|
|
|
} catch (\JsonException $exception) { |
|
64
|
|
|
$this->logFacade->error('Invalid JSON.', $exception); |
|
65
|
|
|
|
|
66
|
|
|
throw $exception; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
return $periodicalConfigDtoCollection; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @return PeriodicalActivityConfigDto[] array |
|
74
|
|
|
*/ |
|
75
|
|
|
private function periodicalConfigDtoMapper(array $periodicalConfigCollection): array |
|
76
|
|
|
{ |
|
77
|
|
|
$periodicalConfigDtoCollection = []; |
|
78
|
|
|
foreach ($periodicalConfigCollection as $periodicalConfig) { |
|
79
|
|
|
$periodicalConfigDtoCollection[] = new PeriodicalActivityConfigDto(...(array) $periodicalConfig); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
return $periodicalConfigDtoCollection; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @return PeriodicalActivityConfigDto[] array |
|
87
|
|
|
*/ |
|
88
|
|
|
private function filterPeriodicalConfigDto(array $periodicalConfigDtoCollection, string $periodicalDate): array |
|
89
|
|
|
{ |
|
90
|
|
|
$activityCandidates = []; |
|
91
|
|
|
foreach ($periodicalConfigDtoCollection as $periodicalActivityConfigDto) { |
|
92
|
|
|
if (str_contains($periodicalActivityConfigDto->frequency, date('N', strtotime($periodicalDate)))) { |
|
93
|
|
|
$activityCandidates[] = $periodicalActivityConfigDto; |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
return $activityCandidates; |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|