|
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\PeriodicalActivityDataImport\Business; |
|
13
|
|
|
|
|
14
|
|
|
use ForecastAutomation\Activity\Shared\Dto\ActivityDto; |
|
15
|
|
|
use ForecastAutomation\Activity\Shared\Dto\ActivityDtoCollection; |
|
16
|
|
|
use ForecastAutomation\ForecastClient\Shared\Config\ForecastClientQueueConstants; |
|
17
|
|
|
use ForecastAutomation\PeriodicalActivityDataImport\Shared\Dto\PeriodicalActivityConfigDto; |
|
18
|
|
|
use ForecastAutomation\QueueClient\QueueClientFacade; |
|
19
|
|
|
use ForecastAutomation\QueueClient\Shared\Dto\MessageCollectionDto; |
|
20
|
|
|
use ForecastAutomation\QueueClient\Shared\Dto\MessageDto; |
|
21
|
|
|
|
|
22
|
|
|
class PeriodicalActivityDataImportProcess |
|
23
|
|
|
{ |
|
24
|
|
|
public const PERIODICAL_SUFFIX = 'Geplanter Termin'; |
|
25
|
|
|
|
|
26
|
|
|
public function __construct( |
|
27
|
|
|
private PeriodicalActivityConfigReader $periodicalActivityConfigReader, |
|
28
|
|
|
private QueueClientFacade $queueClientFacade |
|
29
|
|
|
) { |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
public function start(string $periodicalDate): int |
|
33
|
|
|
{ |
|
34
|
|
|
$activityDtoCollection = $this->generateActivity( |
|
35
|
|
|
$this->periodicalActivityConfigReader->readPeriodicalConfig($periodicalDate), |
|
36
|
|
|
$periodicalDate |
|
37
|
|
|
); |
|
38
|
|
|
|
|
39
|
|
|
$this->queueClientFacade->sendMessages( |
|
40
|
|
|
ForecastClientQueueConstants::QUEUE_NAME, |
|
41
|
|
|
$this->createMessageCollectionDto($activityDtoCollection) |
|
42
|
|
|
); |
|
43
|
|
|
|
|
44
|
|
|
return \count($activityDtoCollection->activityDtos); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
private function createMessageCollectionDto(ActivityDtoCollection $activityDtoCollection): MessageCollectionDto |
|
48
|
|
|
{ |
|
49
|
|
|
$messages = []; |
|
50
|
|
|
foreach ($activityDtoCollection as $activityDto) { |
|
51
|
|
|
$messages[] = |
|
52
|
|
|
new MessageDto( |
|
53
|
|
|
['created' => $activityDto->created->format('c')] + (array) $activityDto, |
|
54
|
|
|
ForecastClientQueueConstants::QUEUE_NAME, |
|
55
|
|
|
ForecastClientQueueConstants::IMPORT_EVENT |
|
56
|
|
|
); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
return new MessageCollectionDto(...$messages); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
private function generateActivity( |
|
63
|
|
|
array $periodicalConfigDtoCollection, |
|
64
|
|
|
string $periodicalDate |
|
65
|
|
|
): ActivityDtoCollection { |
|
66
|
|
|
$activityDtoArray = []; |
|
67
|
|
|
/** @var PeriodicalActivityConfigDto $periodicalConfigDto */ |
|
68
|
|
|
foreach ($periodicalConfigDtoCollection as $periodicalConfigDto) { |
|
69
|
|
|
$activityDtoArray[] = new ActivityDto( |
|
70
|
|
|
$periodicalConfigDto->project_id, |
|
71
|
|
|
sprintf('%s: %s', self::PERIODICAL_SUFFIX, $periodicalConfigDto->note), |
|
72
|
|
|
new \DateTime(date('d-m-Y', strtotime($periodicalDate))), |
|
73
|
|
|
$periodicalConfigDto->duration |
|
74
|
|
|
); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
return new ActivityDtoCollection(...$activityDtoArray); |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|