1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Wabel\CertainAPI\Commands; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use Symfony\Component\Console\Command\Command; |
7
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
8
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
9
|
|
|
use Wabel\CertainAPI\Helpers\FileChangesHelper; |
10
|
|
|
use Wabel\CertainAPI\Interfaces\CertainListener; |
11
|
|
|
use Wabel\CertainAPI\Services\DetectAppointmentsChangingsService; |
12
|
|
|
|
13
|
|
|
class DetectAppointmentsChangingCommand extends Command |
14
|
|
|
{ |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var bool |
18
|
|
|
*/ |
19
|
|
|
private $fileLockAuthorizeRun; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var DetectAppointmentsChangingsService |
23
|
|
|
*/ |
24
|
|
|
private $detectAppointmentsChangingsService; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var CertainListener[] |
28
|
|
|
*/ |
29
|
|
|
private $listeners; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
private $dirPathHistoryAppointments; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var string |
38
|
|
|
*/ |
39
|
|
|
private $filePathEventToCheck; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* DetectAppointmentsChangingCommand constructor. |
43
|
|
|
* @param DetectAppointmentsChangingsService $detectAppointmentsChangingsService |
44
|
|
|
* @param string $filePathEventToCheck |
45
|
|
|
* @param string $dirPathHistoryAppointments |
46
|
|
|
* @param string $fileLockAuthorizeRun |
47
|
|
|
* @param CertainListener[] $listeners |
48
|
|
|
* @param null $name |
49
|
|
|
*/ |
50
|
|
|
public function __construct(DetectAppointmentsChangingsService $detectAppointmentsChangingsService,$filePathEventToCheck,$dirPathHistoryAppointments,$fileLockAuthorizeRun, array $listeners=[],$name=null) |
51
|
|
|
{ |
52
|
|
|
parent::__construct($name); |
53
|
|
|
$this->detectAppointmentsChangingsService = $detectAppointmentsChangingsService; |
54
|
|
|
$this->listeners = $listeners; |
55
|
|
|
$this->dirPathHistoryAppointments = $dirPathHistoryAppointments; |
56
|
|
|
$this->fileLockAuthorizeRun = $fileLockAuthorizeRun; |
|
|
|
|
57
|
|
|
$this->filePathEventToCheck = $filePathEventToCheck; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
protected function configure() |
61
|
|
|
{ |
62
|
|
|
$this |
63
|
|
|
->setName('certain:detect-changings') |
64
|
|
|
->setDescription('Detect changings of appointments from Certain Event.') |
65
|
|
|
->setHelp(<<<EOT |
66
|
|
|
Request Certain to get appointments and detect changes between to request |
67
|
|
|
EOT |
68
|
|
|
); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function run(InputInterface $input, OutputInterface $output) |
72
|
|
|
{ |
73
|
|
|
$eventCode = null; |
74
|
|
|
//Get the EventCode we need to check. |
75
|
|
|
if($this->filePathEventToCheck && file_exists($this->filePathEventToCheck)){ |
76
|
|
|
$eventCode = FileChangesHelper::getContentFile($this->filePathEventToCheck); |
77
|
|
|
} |
78
|
|
|
//That permits to stop the followings instructions when we are makings changes on Certain. |
79
|
|
|
if(!file_exists($this->fileLockAuthorizeRun.'/detect_appointments_changes.lock') && $eventCode){ |
|
|
|
|
80
|
|
|
$output->writeln('Detect changes - Run.'); |
81
|
|
|
//Get the online appointments. |
82
|
|
|
$appointmentsNewCertain = $this->detectAppointmentsChangingsService->getCurrentAppoiments($eventCode); |
83
|
|
|
$appointmentsNew = DetectAppointmentsChangingsService::recursiveArrayObjectToFullArray($appointmentsNewCertain); |
84
|
|
|
//Get the last saved appointments to get old data. |
85
|
|
|
$appointmentsOldHistoryFilePath = FileChangesHelper::getTheLastAppointmentsSaved($eventCode,$this->dirPathHistoryAppointments); |
86
|
|
|
if(!$appointmentsOldHistoryFilePath){ |
87
|
|
|
//No files so it's the first time we attempt to synchronize. |
88
|
|
|
$appointmentsOld = []; |
89
|
|
|
}else{ |
90
|
|
|
//Get the last old appointments data. |
91
|
|
|
$appointmentsOldHistory = FileChangesHelper::getJsonContentFromFile($appointmentsOldHistoryFilePath); |
92
|
|
|
$appointmentsOld = DetectAppointmentsChangingsService::recursiveArrayObjectToFullArray($appointmentsOldHistory); |
93
|
|
|
} |
94
|
|
|
//Check if they are changes. |
95
|
|
|
$listChangings = $this->detectAppointmentsChangingsService->detectAppointmentsChangings($appointmentsOld,$appointmentsNew); |
96
|
|
|
if(!$appointmentsOld || ((isset($listChangings['updated']) && !empty($listChangings['updated'])) |
|
|
|
|
97
|
|
|
|| (isset($listChangings['deleted']) && !empty($listChangings['deleted'])))){ |
98
|
|
|
//Changes? So we save the new online appointments |
99
|
|
|
FileChangesHelper::saveAppointmentsFileByHistory($this->dirPathHistoryAppointments.'/appointments_'.$eventCode.'.json',json_encode($appointmentsNew)); |
100
|
|
|
$output->writeln('Detect changes - Save Changes'); |
101
|
|
|
}else{ |
102
|
|
|
$output->writeln('Detect changes - No Changes'); |
103
|
|
|
} |
104
|
|
|
foreach ($this->listeners as $listener){ |
105
|
|
|
//Run Listener. For instance,Here we can use ChangingsToFileListeners to save the changes in file. |
106
|
|
|
$listener->run($eventCode,$listChangings); |
107
|
|
|
} |
108
|
|
|
}else{ |
109
|
|
|
$output->writeln('Detect changes - Stop.'); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
} |
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.