Completed
Push — master ( 28d05b...16bc73 )
by Raphaël
02:44
created

DetectAppointmentsChangingCommand::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 6
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;
0 ignored issues
show
Documentation Bug introduced by
The property $fileLockAuthorizeRun was declared of type boolean, but $fileLockAuthorizeRun is of type string. Maybe add a type cast?

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.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
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){
0 ignored issues
show
Bug Best Practice introduced by
The expression $eventCode of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
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']))
0 ignored issues
show
Bug Best Practice introduced by
The expression $appointmentsOld of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

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.

Loading history...
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
}