Completed
Pull Request — master (#1)
by Raphaël
04:40
created
tests/Wabel/Services/DetectAppointmentsChangingsServiceTest.php 1 patch
Spacing   +28 added lines, -28 removed lines patch added patch discarded remove patch
@@ -29,7 +29,7 @@  discard block
 block discarded – undo
29 29
      * Personal Object to test
30 30
      * @return array
31 31
      */
32
-    private function personalAppointment(){
32
+    private function personalAppointment() {
33 33
         return [
34 34
                     "appointmentId"=> 52,
35 35
                     "startDate"=> "2015-16-07 11:00:00",
@@ -68,23 +68,23 @@  discard block
 block discarded – undo
68 68
 
69 69
     protected function setUp()
70 70
     {
71
-        $certainApiClient =  new CertainApiClient(null,getenv('username'),getenv('password'),getenv('accountCode'));
72
-        $this->certainApiService  = new CertainApiService($certainApiClient);
71
+        $certainApiClient = new CertainApiClient(null, getenv('username'), getenv('password'), getenv('accountCode'));
72
+        $this->certainApiService = new CertainApiService($certainApiClient);
73 73
         $this->appointmentsCertain = new AppointmentsCertain($this->certainApiService);
74
-        $this->certainAppointmentsList = $this->appointmentsCertain->get(getenv('eventCode'),['start_index'=>0,'max_results'=>10])->getResults()->appointments;
74
+        $this->certainAppointmentsList = $this->appointmentsCertain->get(getenv('eventCode'), ['start_index'=>0, 'max_results'=>10])->getResults()->appointments;
75 75
     }
76 76
 
77
-    public function testDetectChangings(){
77
+    public function testDetectChangings() {
78 78
         $detectService = new DetectAppointmentsChangingsService($this->appointmentsCertain);
79 79
         $oldAppointments = $this->certainAppointmentsList;
80 80
         $currentAppoiments = $oldAppointments;
81 81
         unset($currentAppoiments[0]);
82 82
         unset($currentAppoiments[1]);
83
-        $hasChanged = $detectService->hasChanged($oldAppointments,$currentAppoiments);
83
+        $hasChanged = $detectService->hasChanged($oldAppointments, $currentAppoiments);
84 84
         $this->assertTrue($hasChanged);
85 85
     }
86 86
 
87
-    public function testGetListChangings(){
87
+    public function testGetListChangings() {
88 88
         $detectService = new DetectAppointmentsChangingsService($this->appointmentsCertain);
89 89
         $oldAppointments = $this->certainAppointmentsList;
90 90
         $currentAppoiments = $oldAppointments;
@@ -94,14 +94,14 @@  discard block
 block discarded – undo
94 94
         unset($currentAppoiments[0]);
95 95
         unset($currentAppoiments[1]);
96 96
         unset($currentAppoiments[4]);
97
-        $changings = $detectService->getListChangings($oldAppointments,$currentAppoiments);
98
-        $this->assertEquals(3,count($changings));
99
-        $this->assertContains($delete1,$changings);
100
-        $this->assertContains($delete2,$changings);
101
-        $this->assertContains($delete3,$changings);
97
+        $changings = $detectService->getListChangings($oldAppointments, $currentAppoiments);
98
+        $this->assertEquals(3, count($changings));
99
+        $this->assertContains($delete1, $changings);
100
+        $this->assertContains($delete2, $changings);
101
+        $this->assertContains($delete3, $changings);
102 102
     }
103 103
 
104
-    public function testDetectDeleteOrUpdated(){
104
+    public function testDetectDeleteOrUpdated() {
105 105
         $detectService = new DetectAppointmentsChangingsService($this->appointmentsCertain);
106 106
         $oldAppointments = $this->certainAppointmentsList;
107 107
         $oldAppointments[10] = $this->personalAppointment();
@@ -113,19 +113,19 @@  discard block
 block discarded – undo
113 113
         $update1 = $currentAppoiments[10];
114 114
         $currentAppoiments[10]["startDate"] = "2015-17-07 12:00:00";
115 115
         $currentAppoiments[10]["endDate"] = "2015-17-07 12:30:00";
116
-        $changings = $detectService->getListChangings($oldAppointments,$currentAppoiments);
117
-        $listDetected = $detectService->detectDeleteOrUpdatedOrInserted($currentAppoiments,$changings);
118
-        $this->assertEquals(3,count($changings));
119
-        $this->assertArrayHasKey('deleted',$listDetected);
120
-        $this->assertArrayHasKey('updated',$listDetected);
121
-        $this->assertEquals(2,count($listDetected['deleted']));
122
-        $this->assertEquals(1,count($listDetected['updated']));
123
-        $this->assertContains($delete1,$listDetected['deleted']);
124
-        $this->assertContains($delete2,$listDetected['deleted']);
125
-        $this->assertContains($update1,$listDetected['updated']);
116
+        $changings = $detectService->getListChangings($oldAppointments, $currentAppoiments);
117
+        $listDetected = $detectService->detectDeleteOrUpdatedOrInserted($currentAppoiments, $changings);
118
+        $this->assertEquals(3, count($changings));
119
+        $this->assertArrayHasKey('deleted', $listDetected);
120
+        $this->assertArrayHasKey('updated', $listDetected);
121
+        $this->assertEquals(2, count($listDetected['deleted']));
122
+        $this->assertEquals(1, count($listDetected['updated']));
123
+        $this->assertContains($delete1, $listDetected['deleted']);
124
+        $this->assertContains($delete2, $listDetected['deleted']);
125
+        $this->assertContains($update1, $listDetected['updated']);
126 126
     }
127 127
 
128
-    public function testDetectAppointmentsChangings(){
128
+    public function testDetectAppointmentsChangings() {
129 129
         $detectService = new DetectAppointmentsChangingsService($this->appointmentsCertain);
130 130
         $oldAppointments = $this->certainAppointmentsList;
131 131
         $oldAppointments[10] = $this->personalAppointment();
@@ -141,10 +141,10 @@  discard block
 block discarded – undo
141 141
         $update1['dateDetectChanges'] = $time;
142 142
         $currentAppoiments[10]["startDate"] = "2015-17-07 12:00:00";
143 143
         $currentAppoiments[10]["endDate"] = "2015-17-07 12:30:00";
144
-        $listDetected = $detectService->detectAppointmentsChangings($oldAppointments,$currentAppoiments,$time);
145
-        $this->assertContains($delete1,$listDetected['deleted']);
146
-        $this->assertContains($delete2,$listDetected['deleted']);
147
-        $this->assertContains($update1,$listDetected['updated']);
144
+        $listDetected = $detectService->detectAppointmentsChangings($oldAppointments, $currentAppoiments, $time);
145
+        $this->assertContains($delete1, $listDetected['deleted']);
146
+        $this->assertContains($delete2, $listDetected['deleted']);
147
+        $this->assertContains($update1, $listDetected['updated']);
148 148
     }
149 149
 
150 150
 }
151 151
\ No newline at end of file
Please login to merge, or discard this patch.
src/Commands/DetectAppointmentsChangingCommand.php 1 patch
Spacing   +14 added lines, -14 removed lines patch added patch discarded remove patch
@@ -36,7 +36,7 @@  discard block
 block discarded – undo
36 36
      * @param CertainListener[] $listeners
37 37
      * @param null $name
38 38
      */
39
-    public function __construct(DetectAppointmentsChangingsService $detectAppointmentsChangingsService,$dirPathHistoryAppointments, array $listeners=[],$name=null)
39
+    public function __construct(DetectAppointmentsChangingsService $detectAppointmentsChangingsService, $dirPathHistoryAppointments, array $listeners = [], $name = null)
40 40
     {
41 41
         parent::__construct($name);
42 42
         $this->detectAppointmentsChangingsService = $detectAppointmentsChangingsService;
@@ -53,44 +53,44 @@  discard block
 block discarded – undo
53 53
 Request Certain to get appointments and detect changes between to request
54 54
 EOT
55 55
             );
56
-        $this->addArgument('eventCode',InputArgument::REQUIRED,'Specify the eventCode from Certain');
56
+        $this->addArgument('eventCode', InputArgument::REQUIRED, 'Specify the eventCode from Certain');
57 57
     }
58 58
 
59 59
     public function execute(InputInterface $input, OutputInterface $output)
60 60
     {
61 61
         $eventCode = $input->getArgument('eventCode');
62 62
         //That permits to stop the followings instructions when we are makings changes on Certain.
63
-        if($eventCode){
63
+        if ($eventCode) {
64 64
             $output->writeln('Detect changes - Run.');
65 65
             //Get the online appointments.
66 66
             $appointmentsNewCertain = $this->detectAppointmentsChangingsService->getCurrentAppoiments($eventCode);
67 67
             $appointmentsNew = DetectAppointmentsChangingsService::recursiveArrayObjectToFullArray($appointmentsNewCertain);
68 68
             //Get the last saved appointments to get old data.
69
-            $appointmentsOldHistoryFilePath = FileChangesHelper::getTheLastAppointmentsSaved($eventCode,$this->dirPathHistoryAppointments);
70
-            if(!$appointmentsOldHistoryFilePath){
69
+            $appointmentsOldHistoryFilePath = FileChangesHelper::getTheLastAppointmentsSaved($eventCode, $this->dirPathHistoryAppointments);
70
+            if (!$appointmentsOldHistoryFilePath) {
71 71
                 //No files so it's the first time we attempt to synchronize.
72 72
                 $appointmentsOld = [];
73
-            }else{
73
+            }else {
74 74
                 //Get the last old appointments data.
75 75
                 $appointmentsOldHistory = FileChangesHelper::getJsonContentFromFile($appointmentsOldHistoryFilePath);
76 76
                 $appointmentsOld = DetectAppointmentsChangingsService::recursiveArrayObjectToFullArray($appointmentsOldHistory);
77 77
             }
78 78
             //Check if they are changes.
79 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'])))){
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 83
                 //Changes? So we save the new online appointments
84
-                FileChangesHelper::saveAppointmentsFileByHistory($this->dirPathHistoryAppointments.'/appointments_'.$eventCode.'.json',json_encode($appointmentsNew));
84
+                FileChangesHelper::saveAppointmentsFileByHistory($this->dirPathHistoryAppointments.'/appointments_'.$eventCode.'.json', json_encode($appointmentsNew));
85 85
                 $output->writeln('Detect changes - Save Changes');
86
-            }else{
86
+            }else {
87 87
                 $output->writeln('Detect changes - No Changes');
88 88
             }
89
-            foreach ($this->listeners as $listener){
89
+            foreach ($this->listeners as $listener) {
90 90
                 //Run Listener. For instance,Here we can use ChangingsToFileListeners to save the changes in file.
91
-                $listener->run($eventCode,$listChangings);
91
+                $listener->run($eventCode, $listChangings);
92 92
             }
93
-        }else{
93
+        }else {
94 94
             $output->writeln('Detect changes - Stop.');
95 95
         }
96 96
     }
Please login to merge, or discard this patch.
src/Services/DetectAppointmentsChangingsService.php 1 patch
Spacing   +35 added lines, -35 removed lines patch added patch discarded remove patch
@@ -23,14 +23,14 @@  discard block
 block discarded – undo
23 23
      * @param null|int $maxResult
24 24
      * @return mixed
25 25
      */
26
-    public function getCurrentAppoiments($eventCode,$start=null,$maxResult=null){
27
-        if(!$start){
26
+    public function getCurrentAppoiments($eventCode, $start = null, $maxResult = null) {
27
+        if (!$start) {
28 28
             $start = 0;
29 29
         }
30
-        if(!$maxResult){
30
+        if (!$maxResult) {
31 31
             $maxResult = 999999;
32 32
         }
33
-        return $this->certainAppointmentsList = $this->appointmentsCertain->get($eventCode,['start_index'=>$start,'max_results'=>$maxResult])->getResults()->appointments;
33
+        return $this->certainAppointmentsList = $this->appointmentsCertain->get($eventCode, ['start_index'=>$start, 'max_results'=>$maxResult])->getResults()->appointments;
34 34
     }
35 35
 
36 36
     /**
@@ -38,21 +38,21 @@  discard block
 block discarded – undo
38 38
      * @param array $appointmentsNew
39 39
      * @return bool
40 40
      */
41
-    public function hasChanged(array $appointmentsOld,array $appointmentsNew){
41
+    public function hasChanged(array $appointmentsOld, array $appointmentsNew) {
42 42
         $hasChanged = false;
43 43
         $appointmentsOld = self::recursiveArrayObjectToFullArray($appointmentsOld);
44 44
         $appointmentsNew = self::recursiveArrayObjectToFullArray($appointmentsNew);
45 45
         //Has change by update or delete
46
-        foreach ($appointmentsOld as $appointmentOld){
47
-            if(!in_array($appointmentOld,$appointmentsNew)){
46
+        foreach ($appointmentsOld as $appointmentOld) {
47
+            if (!in_array($appointmentOld, $appointmentsNew)) {
48 48
                 $hasChanged = true;
49 49
                 break;
50 50
             }
51 51
         }
52 52
         //Has changes by insertion or update
53
-        if(!$hasChanged){
54
-            foreach ($appointmentsNew as $appointmentNew){
55
-                if(!in_array($appointmentNew,$appointmentsOld)){
53
+        if (!$hasChanged) {
54
+            foreach ($appointmentsNew as $appointmentNew) {
55
+                if (!in_array($appointmentNew, $appointmentsOld)) {
56 56
                     $hasChanged = true;
57 57
                     break;
58 58
                 }
@@ -66,7 +66,7 @@  discard block
 block discarded – undo
66 66
      * @return array
67 67
      */
68 68
     public static  function objectToArray($object) {
69
-        if(is_object($object)){
69
+        if (is_object($object)) {
70 70
 
71 71
             return (array) $object;
72 72
         }
@@ -77,7 +77,7 @@  discard block
 block discarded – undo
77 77
      * @param $appointments
78 78
      * @return array
79 79
      */
80
-    public static function recursiveArrayObjectToFullArray($appointments){
80
+    public static function recursiveArrayObjectToFullArray($appointments) {
81 81
         return json_decode(json_encode($appointments), true);
82 82
     }
83 83
 
@@ -88,8 +88,8 @@  discard block
 block discarded – undo
88 88
      */
89 89
     private function arrayRecursiveDiff(array $arrayOlds, array $arrayNews) {
90 90
         $difference = [];
91
-        foreach($arrayOlds as $key => $arrayOld){
92
-            if(!in_array($arrayOld,$arrayNews)){
91
+        foreach ($arrayOlds as $key => $arrayOld) {
92
+            if (!in_array($arrayOld, $arrayNews)) {
93 93
                 $difference[$key] = $arrayOld;
94 94
             }
95 95
         }
@@ -104,9 +104,9 @@  discard block
 block discarded – undo
104 104
      */
105 105
     private function arrayRecursiveDiffNew(array $arrayOlds, array $arrayNews, array $existedUpdateOrDelete) {
106 106
         $difference = [];
107
-        foreach($arrayNews as $key => $arrayNew){
108
-            if(!in_array($arrayNew,$arrayOlds)
109
-                && !in_array(self::recursiveArrayObjectToFullArray($arrayNew), $existedUpdateOrDelete)){
107
+        foreach ($arrayNews as $key => $arrayNew) {
108
+            if (!in_array($arrayNew, $arrayOlds)
109
+                && !in_array(self::recursiveArrayObjectToFullArray($arrayNew), $existedUpdateOrDelete)) {
110 110
                 $difference[$key] = $arrayNew;
111 111
             }
112 112
         }
@@ -118,13 +118,13 @@  discard block
 block discarded – undo
118 118
      * @param array $appointmentsNew
119 119
      * @return array
120 120
      */
121
-    public function getListChangings(array $appointmentsOld,array $appointmentsNew){
121
+    public function getListChangings(array $appointmentsOld, array $appointmentsNew) {
122 122
         $appointmentsOld = self::recursiveArrayObjectToFullArray($appointmentsOld);
123 123
         $appointmentsNew = self::recursiveArrayObjectToFullArray($appointmentsNew);
124 124
         $changesListUpdateOrDelete = [];
125 125
         $changesListInsert = [];
126
-        if($this->hasChanged($appointmentsOld,$appointmentsNew)){
127
-            $changesListUpdateOrDelete = self::recursiveArrayObjectToFullArray($this->arrayRecursiveDiff($appointmentsOld,$appointmentsNew));
126
+        if ($this->hasChanged($appointmentsOld, $appointmentsNew)) {
127
+            $changesListUpdateOrDelete = self::recursiveArrayObjectToFullArray($this->arrayRecursiveDiff($appointmentsOld, $appointmentsNew));
128 128
             $changesListInsert = self::recursiveArrayObjectToFullArray($this->arrayRecursiveDiffNew($appointmentsOld, $appointmentsNew, $changesListUpdateOrDelete));
129 129
         }
130 130
         $changesList = array_merge($changesListUpdateOrDelete, $changesListInsert);
@@ -137,27 +137,27 @@  discard block
 block discarded – undo
137 137
      * @param array $changingsDetected
138 138
      * @return array ['deleted'=>[],'updated'=>[]]
139 139
      */
140
-    public function detectDeleteOrUpdatedOrInserted(array $currentAppointments, array $changingsDetected){
140
+    public function detectDeleteOrUpdatedOrInserted(array $currentAppointments, array $changingsDetected) {
141 141
         $delete = [];
142 142
         $update = [];
143 143
         $insert = [];
144 144
         //@Todo: Detect Fields has changed
145 145
         $appointmentsNew = self::recursiveArrayObjectToFullArray($currentAppointments);
146 146
         $changings = self::recursiveArrayObjectToFullArray($changingsDetected);
147
-        foreach ($changings as $changing){
147
+        foreach ($changings as $changing) {
148 148
             $registration = $changing['registration']['regCode'];
149 149
             $registrationTarget = $changing['targetRegistration']['regCode'];
150
-            foreach ($appointmentsNew as $currentAppointment){
150
+            foreach ($appointmentsNew as $currentAppointment) {
151 151
                 $registrationCurrent = $currentAppointment['registration']['regCode'];
152 152
                 $registrationTargetCurrent = $currentAppointment['targetRegistration']['regCode'];
153
-                if(in_array($registration,[$registrationCurrent,$registrationTargetCurrent])
154
-                    && in_array($registrationTarget,[$registrationCurrent,$registrationTargetCurrent])
155
-                    && !in_array($changing,$update) && !in_array($changing,$delete)) {
153
+                if (in_array($registration, [$registrationCurrent, $registrationTargetCurrent])
154
+                    && in_array($registrationTarget, [$registrationCurrent, $registrationTargetCurrent])
155
+                    && !in_array($changing, $update) && !in_array($changing, $delete)) {
156 156
                     $update[] = $changing;
157 157
                     break;
158 158
                 }
159 159
             }
160
-            if(!in_array($changing,$update) && !in_array($changing,$delete)){
160
+            if (!in_array($changing, $update) && !in_array($changing, $delete)) {
161 161
                 $delete[] = $changing;
162 162
             }
163 163
             $insert[] = $changing;
@@ -175,8 +175,8 @@  discard block
 block discarded – undo
175 175
      * @param $timestamp
176 176
      * @return array
177 177
      */
178
-    public static function insertDateTimeChanges(array $appointments,$timestamp){
179
-        foreach ($appointments as $key => $appointment){
178
+    public static function insertDateTimeChanges(array $appointments, $timestamp) {
179
+        foreach ($appointments as $key => $appointment) {
180 180
             $appointments[$key]['dateDetectChanges'] = $timestamp;
181 181
         }
182 182
         return $appointments;
@@ -188,12 +188,12 @@  discard block
 block discarded – undo
188 188
      * @param string $timestamp
189 189
      * @return array ['deleted'=>[],'updated'=>[]]
190 190
      */
191
-    public function detectAppointmentsChangings(array $appointmentsOld,array $appointmentsNew,$timestamp){
192
-        $changings = $this->getListChangings($appointmentsOld,$appointmentsNew);
193
-        $changesList = $this->detectDeleteOrUpdatedOrInserted($appointmentsNew,$changings);
194
-        $changesList['inserted'] = self::insertDateTimeChanges($changesList['inserted'],$timestamp);
195
-        $changesList['updated'] = self::insertDateTimeChanges($changesList['updated'],$timestamp);
196
-        $changesList['deleted'] = self::insertDateTimeChanges($changesList['deleted'],$timestamp);
191
+    public function detectAppointmentsChangings(array $appointmentsOld, array $appointmentsNew, $timestamp) {
192
+        $changings = $this->getListChangings($appointmentsOld, $appointmentsNew);
193
+        $changesList = $this->detectDeleteOrUpdatedOrInserted($appointmentsNew, $changings);
194
+        $changesList['inserted'] = self::insertDateTimeChanges($changesList['inserted'], $timestamp);
195
+        $changesList['updated'] = self::insertDateTimeChanges($changesList['updated'], $timestamp);
196
+        $changesList['deleted'] = self::insertDateTimeChanges($changesList['deleted'], $timestamp);
197 197
         return $changesList;
198 198
     }
199 199
 
Please login to merge, or discard this patch.