|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Wabel\CertainAPI\Commands; |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
use Symfony\Component\Console\Command\Command; |
|
7
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
|
8
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
9
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
10
|
|
|
use Wabel\CertainAPI\Helpers\FileChangesHelper; |
|
11
|
|
|
use Wabel\CertainAPI\Interfaces\CertainListener; |
|
12
|
|
|
use Wabel\CertainAPI\Services\DetectAppointmentsChangingsService; |
|
13
|
|
|
|
|
14
|
|
|
class DetectAppointmentsChangingCommand extends Command |
|
15
|
|
|
{ |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @var DetectAppointmentsChangingsService |
|
19
|
|
|
*/ |
|
20
|
|
|
private $detectAppointmentsChangingsService; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var CertainListener[] |
|
24
|
|
|
*/ |
|
25
|
|
|
private $listeners; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var string |
|
29
|
|
|
*/ |
|
30
|
|
|
private $dirPathHistoryAppointments; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* DetectAppointmentsChangingCommand constructor. |
|
34
|
|
|
* @param DetectAppointmentsChangingsService $detectAppointmentsChangingsService |
|
35
|
|
|
* @param string $dirPathHistoryAppointments |
|
36
|
|
|
* @param CertainListener[] $listeners |
|
37
|
|
|
* @param null $name |
|
38
|
|
|
*/ |
|
39
|
|
|
public function __construct(DetectAppointmentsChangingsService $detectAppointmentsChangingsService,$dirPathHistoryAppointments, array $listeners=[],$name=null) |
|
40
|
|
|
{ |
|
41
|
|
|
parent::__construct($name); |
|
42
|
|
|
$this->detectAppointmentsChangingsService = $detectAppointmentsChangingsService; |
|
43
|
|
|
$this->listeners = $listeners; |
|
44
|
|
|
$this->dirPathHistoryAppointments = $dirPathHistoryAppointments; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
protected function configure() |
|
48
|
|
|
{ |
|
49
|
|
|
$this |
|
50
|
|
|
->setName('certain:detect-changings') |
|
51
|
|
|
->setDescription('Detect changings of appointments from Certain Event.') |
|
52
|
|
|
->setHelp(<<<EOT |
|
53
|
|
|
Request Certain to get appointments and detect changes between to request |
|
54
|
|
|
EOT |
|
55
|
|
|
); |
|
56
|
|
|
$this->addArgument('eventCode',InputArgument::REQUIRED,'Specify the eventCode from Certain'); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
public function execute(InputInterface $input, OutputInterface $output) |
|
60
|
|
|
{ |
|
61
|
|
|
$eventCode = $input->getArgument('eventCode'); |
|
62
|
|
|
//That permits to stop the followings instructions when we are makings changes on Certain. |
|
63
|
|
|
if($eventCode){ |
|
64
|
|
|
$output->writeln('Detect changes - Run.'); |
|
65
|
|
|
//Get the online appointments. |
|
66
|
|
|
$appointmentsNewCertain = $this->detectAppointmentsChangingsService->getCurrentAppoiments($eventCode); |
|
67
|
|
|
$appointmentsNew = DetectAppointmentsChangingsService::recursiveArrayObjectToFullArray($appointmentsNewCertain); |
|
68
|
|
|
//Get the last saved appointments to get old data. |
|
69
|
|
|
$appointmentsOldHistoryFilePath = FileChangesHelper::getTheLastAppointmentsSaved($eventCode,$this->dirPathHistoryAppointments); |
|
70
|
|
|
if(!$appointmentsOldHistoryFilePath){ |
|
71
|
|
|
//No files so it's the first time we attempt to synchronize. |
|
72
|
|
|
$appointmentsOld = []; |
|
73
|
|
|
}else{ |
|
74
|
|
|
//Get the last old appointments data. |
|
75
|
|
|
$appointmentsOldHistory = FileChangesHelper::getJsonContentFromFile($appointmentsOldHistoryFilePath); |
|
76
|
|
|
$appointmentsOld = DetectAppointmentsChangingsService::recursiveArrayObjectToFullArray($appointmentsOldHistory); |
|
77
|
|
|
} |
|
78
|
|
|
//Check if they are changes. |
|
79
|
|
|
$timestamp = time(); |
|
80
|
|
|
$listChangings = $this->detectAppointmentsChangingsService->detectAppointmentsChangings($appointmentsOld,$appointmentsNew,$timestamp); |
|
81
|
|
|
if(!$appointmentsOld || ((isset($listChangings['updated']) && !empty($listChangings['updated'])) |
|
|
|
|
|
|
82
|
|
|
|| (isset($listChangings['deleted']) && !empty($listChangings['deleted'])))){ |
|
83
|
|
|
//Changes? So we save the new online appointments |
|
84
|
|
|
FileChangesHelper::saveAppointmentsFileByHistory($this->dirPathHistoryAppointments.'/appointments_'.$eventCode.'.json',json_encode($appointmentsNew)); |
|
85
|
|
|
$output->writeln('Detect changes - Save Changes'); |
|
86
|
|
|
}else{ |
|
87
|
|
|
$output->writeln('Detect changes - No Changes'); |
|
88
|
|
|
} |
|
89
|
|
|
foreach ($this->listeners as $listener){ |
|
90
|
|
|
//Run Listener. For instance,Here we can use ChangingsToFileListeners to save the changes in file. |
|
91
|
|
|
$listener->run($eventCode,$listChangings); |
|
92
|
|
|
} |
|
93
|
|
|
}else{ |
|
94
|
|
|
$output->writeln('Detect changes - Stop.'); |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
} |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.