Completed
Push — master ( 7202af...a59a4f )
by claudio
06:39
created
app/Console/Commands/Optimise/Optimise.php 1 patch
Braces   +29 added lines, -19 removed lines patch added patch discarded remove patch
@@ -158,11 +158,12 @@  discard block
 block discarded – undo
158 158
             $solver = $this->setData($solver);
159 159
             $solver = $solver->solve();
160 160
             $this->solver = $solver;
161
-        }catch(OptimiseException $e) {
162
-            if(!$e->isEmpty())
163
-                \Event::fire(new ErrorEvent($this->company, $e->getMessage()));
161
+        } catch(OptimiseException $e) {
162
+            if(!$e->isEmpty()) {
163
+                            \Event::fire(new ErrorEvent($this->company, $e->getMessage()));
164
+            }
164 165
             throw $e;
165
-        }catch (\Exception $e) {
166
+        } catch (\Exception $e) {
166 167
             //TODO use the correct exceptions to avoid to share private data
167 168
             \Event::fire(new ErrorEvent($this->company, $e->getMessage()));
168 169
             throw new OptimiseException('Optimising error', 0, $e);
@@ -207,8 +208,9 @@  discard block
 block discarded – undo
207 208
     {
208 209
         //since we consider busy timeslots, we need to get all users
209 210
         $users = $this->company->employees->pluck('id')->toArray();
210
-        if(count($users) == 0)
211
-            throw ((new OptimiseException("No users for this company"))->withEmpty(true));
211
+        if(count($users) == 0) {
212
+                    throw ((new OptimiseException("No users for this company"))->withEmpty(true));
213
+        }
212 214
         return $solver->setUsers($users);
213 215
     }
214 216
 
@@ -223,8 +225,9 @@  discard block
 block discarded – undo
223 225
          * @var $meetings \Illuminate\Support\Collection
224 226
          */
225 227
         $meetings = collect($this->company->getMeetingsTimeSlots($this->startTime, $this->endTime));
226
-        if($meetings->count() == 0)
227
-            throw ((new OptimiseException("No meetings for this week"))->withEmpty(true));
228
+        if($meetings->count() == 0) {
229
+                    throw ((new OptimiseException("No meetings for this week"))->withEmpty(true));
230
+        }
228 231
         $timeslots = $meetings->groupBy('id')->map(function ($item) { //convert timeslots
229 232
             return $this->durationConverter($this->timeSlotsConverter($item));
230 233
         });
@@ -298,8 +301,9 @@  discard block
 block discarded – undo
298 301
     {
299 302
         $ret = [];
300 303
         foreach ($ids as $id) {
301
-            if(isset($timeSlots[$id]))
302
-                $ret = self::fillTimeSlots($ret, $id, $timeSlots[$id], $free ? '1' : '0');
304
+            if(isset($timeSlots[$id])) {
305
+                            $ret = self::fillTimeSlots($ret, $id, $timeSlots[$id], $free ? '1' : '0');
306
+            }
303 307
             $ret = self::fillRow($ret, $id, $timeslotsN, $free ? '0' : '1');
304 308
         }
305 309
 
@@ -316,8 +320,9 @@  discard block
 block discarded – undo
316 320
     static private function fillTimeSlots(array $array, $id, \Illuminate\Support\Collection $timeSlots, $fill = '0')
317 321
     {
318 322
         foreach ($timeSlots as $timeSlot) {
319
-            if (!isset($array[$id]))
320
-                $array[$id] = [];
323
+            if (!isset($array[$id])) {
324
+                            $array[$id] = [];
325
+            }
321 326
             $array[$id] = self::arrayPadInterval($array[$id], $timeSlot->time_start, $timeSlot->time_end, $fill);
322 327
         }
323 328
         return $array;
@@ -332,8 +337,9 @@  discard block
 block discarded – undo
332 337
      */
333 338
     static private function arrayPadInterval(array $array, $from, $to, $pad = '0')
334 339
     {
335
-        for ($i = $from; $i < $to; $i++)
336
-            $array[$i] = $pad;
340
+        for ($i = $from; $i < $to; $i++) {
341
+                    $array[$i] = $pad;
342
+        }
337 343
         return $array;
338 344
     }
339 345
 
@@ -346,8 +352,9 @@  discard block
 block discarded – undo
346 352
     static private function fillRow(array $array, $id, $until, $fill = '0')
347 353
     {
348 354
         for ($i = 1; $i <= $until; $i++) {
349
-            if (!isset($array[$id][$i]))
350
-                $array[$id][$i] = $fill;
355
+            if (!isset($array[$id][$i])) {
356
+                            $array[$id][$i] = $fill;
357
+            }
351 358
         }
352 359
 
353 360
         return $array;
@@ -386,8 +393,9 @@  discard block
 block discarded – undo
386 393
          * @var $usersMeetings \Illuminate\Support\Collection
387 394
          */
388 395
         $usersMeetings = collect($this->company->getUsersMeetings($users, $meetings))->groupBy('employee_id');
389
-        if($usersMeetings->count() == 0)
390
-            throw ((new OptimiseException("No users for any meeting"))->withEmpty(true));
396
+        if($usersMeetings->count() == 0) {
397
+                    throw ((new OptimiseException("No users for any meeting"))->withEmpty(true));
398
+        }
391 399
 
392 400
         return $solver->setUsersMeetings(self::getUsersMeetingsArray($users, $meetings, $usersMeetings));
393 401
     }
@@ -464,8 +472,10 @@  discard block
 block discarded – undo
464 472
     {
465 473
         $ret = clone $this->startTime;
466 474
         //TODO check, because the meetings cannot have this date available -> this to avoid errors if we don't have a date for a meeting
467
-        if ($timeslot <= 1) //false == 0
475
+        if ($timeslot <= 1) {
476
+            //false == 0
468 477
             return $ret;
478
+        }
469 479
         return $ret->add(new \DateInterval('PT' . (($timeslot - 1) * config('app.timeslots.duration')) . 'S'));
470 480
     }
471 481
 
Please login to merge, or discard this patch.
app/Console/Commands/Optimise/Solver.php 1 patch
Braces   +60 added lines, -40 removed lines patch added patch discarded remove patch
@@ -104,8 +104,9 @@  discard block
 block discarded – undo
104 104
      */
105 105
     static private function checkGlpsol()
106 106
     {
107
-        if (!(`which glpsol`))
108
-            throw new OptimiseException('glpsol is not installed');
107
+        if (!(`which glpsol`)) {
108
+                    throw new OptimiseException('glpsol is not installed');
109
+        }
109 110
     }
110 111
 
111 112
     /**
@@ -193,8 +194,9 @@  discard block
 block discarded – undo
193 194
      */
194 195
     public function setTimeSlots($timeSlots)
195 196
     {
196
-        if (!is_int($timeSlots) || $timeSlots <= 0)
197
-            throw new OptimiseException($timeSlots . ' is not integer or it is not >0');
197
+        if (!is_int($timeSlots) || $timeSlots <= 0) {
198
+                    throw new OptimiseException($timeSlots . ' is not integer or it is not >0');
199
+        }
198 200
 
199 201
         $this->timeSlots = $timeSlots;
200 202
         return $this;
@@ -215,8 +217,9 @@  discard block
 block discarded – undo
215 217
      */
216 218
     public function setMaxTimeSlots($maxTimeSlots)
217 219
     {
218
-        if (!is_int($maxTimeSlots) || $maxTimeSlots <= 0)
219
-            throw new OptimiseException($maxTimeSlots . ' is not integer or it is not >0');
220
+        if (!is_int($maxTimeSlots) || $maxTimeSlots <= 0) {
221
+                    throw new OptimiseException($maxTimeSlots . ' is not integer or it is not >0');
222
+        }
220 223
 
221 224
         $this->maxTimeSlots = $maxTimeSlots;
222 225
         return $this;
@@ -238,13 +241,15 @@  discard block
 block discarded – undo
238 241
     public function setMeetingsAvailability($meetingsAvailability)
239 242
     {
240 243
         $meetings = array_keys($meetingsAvailability);
241
-        if (array_diff($meetings, $this->meetings))
242
-            throw new OptimiseException('meetings different from meetings set');
244
+        if (array_diff($meetings, $this->meetings)) {
245
+                    throw new OptimiseException('meetings different from meetings set');
246
+        }
243 247
         foreach ($meetingsAvailability as $key => $meetingsAvailabilityS) {
244 248
             $timeSlots = array_keys($meetingsAvailabilityS);//TODO this is useless, we can use directly $usersAvailabilityS
245 249
             //TODO check if keys are sequential and the last is the same with $this->timeSlots
246
-            if (count($timeSlots) != $this->timeSlots)
247
-                throw new OptimiseException('timeSlots different from timeSlots set');
250
+            if (count($timeSlots) != $this->timeSlots) {
251
+                            throw new OptimiseException('timeSlots different from timeSlots set');
252
+            }
248 253
             $meetingsAvailability[$key] = self::arrayPad($meetingsAvailabilityS, $this->timeSlots + $this->maxTimeSlots, 0);
249 254
         }
250 255
 
@@ -261,8 +266,9 @@  discard block
 block discarded – undo
261 266
     static private function arrayPad(array $array, $len, $pad)
262 267
     {
263 268
         $len = $len - count($array);
264
-        for ($i = 0; $i < $len; $i++)
265
-            $array[] = $pad;
269
+        for ($i = 0; $i < $len; $i++) {
270
+                    $array[] = $pad;
271
+        }
266 272
         return $array;
267 273
     }
268 274
 
@@ -287,8 +293,9 @@  discard block
 block discarded – undo
287 293
         }
288 294
         foreach ($meetingsDuration as $duration) {
289 295
             $duration = (int)$duration; //TODO fix this (fix for optimise)
290
-            if (!is_int($duration) || $duration <= 0)
291
-                throw new OptimiseException('duration is not integer or it is not >0');
296
+            if (!is_int($duration) || $duration <= 0) {
297
+                            throw new OptimiseException('duration is not integer or it is not >0');
298
+            }
292 299
         }
293 300
 
294 301
         $this->meetingsDuration = $meetingsDuration;
@@ -311,12 +318,14 @@  discard block
 block discarded – undo
311 318
     public function setUsersAvailability($usersAvailability)
312 319
     {
313 320
         $users = array_keys($usersAvailability);
314
-        if (array_diff($users, $this->users))
315
-            throw new OptimiseException('users different from users set');
321
+        if (array_diff($users, $this->users)) {
322
+                    throw new OptimiseException('users different from users set');
323
+        }
316 324
         foreach ($usersAvailability as $key => $usersAvailabilityS) {
317 325
             $timeSlots = array_keys($usersAvailabilityS);//TODO this is useless, we can use directly $usersAvailabilityS
318
-            if (count($timeSlots) != $this->timeSlots)
319
-                throw new OptimiseException('timeSlots different from timeSlots set');
326
+            if (count($timeSlots) != $this->timeSlots) {
327
+                            throw new OptimiseException('timeSlots different from timeSlots set');
328
+            }
320 329
 
321 330
             $usersAvailability[$key] = self::arrayPad($usersAvailabilityS, $this->timeSlots + $this->maxTimeSlots, 0);
322 331
         }
@@ -341,12 +350,14 @@  discard block
 block discarded – undo
341 350
     public function setUsersMeetings($usersMeetings)
342 351
     {
343 352
         $users = array_keys($usersMeetings);
344
-        if (array_diff($users, $this->users))
345
-            throw new OptimiseException('users different from users set');
353
+        if (array_diff($users, $this->users)) {
354
+                    throw new OptimiseException('users different from users set');
355
+        }
346 356
         foreach ($usersMeetings as $usersMeetingsS) {
347 357
             $meetings = array_keys($usersMeetingsS);
348
-            if (array_diff($meetings, $this->meetings))
349
-                throw new OptimiseException('meetings different from meetings set');
358
+            if (array_diff($meetings, $this->meetings)) {
359
+                            throw new OptimiseException('meetings different from meetings set');
360
+            }
350 361
         }
351 362
 
352 363
         $this->usersMeetings = $usersMeetings;
@@ -363,8 +374,9 @@  discard block
 block discarded – undo
363 374
         $this->writeModelFile();
364 375
         $event = $this->schedule->exec('glpsol --math ' . $this->path->getModelPath())->sendOutputTo($this->path->getOutputPath())->after(function () {
365 376
         }); //this just to execute in foreground
366
-        if ($event->isDue($this->laravel))
367
-            $event->run($this->laravel);
377
+        if ($event->isDue($this->laravel)) {
378
+                    $event->run($this->laravel);
379
+        }
368 380
         //TODO catch glpsol errors
369 381
         return $this;
370 382
     }
@@ -398,9 +410,10 @@  discard block
 block discarded – undo
398 410
      */
399 411
     private function checkArrayProprieties($proprieties)
400 412
     {
401
-        foreach ($proprieties as $propriety)
402
-            if (count($this->$propriety) == 0)
413
+        foreach ($proprieties as $propriety) {
414
+                    if (count($this->$propriety) == 0)
403 415
                 throw new OptimiseException($propriety . ' property is not set correctly');
416
+        }
404 417
     }
405 418
 
406 419
     /**
@@ -409,9 +422,10 @@  discard block
 block discarded – undo
409 422
      */
410 423
     private function checkIntProprieties($proprieties)
411 424
     {
412
-        foreach ($proprieties as $propriety)
413
-            if (!is_int($this->$propriety) || $this->$propriety <= 0)
425
+        foreach ($proprieties as $propriety) {
426
+                    if (!is_int($this->$propriety) || $this->$propriety <= 0)
414 427
                 throw new OptimiseException($propriety . ' property is not set correctly');
428
+        }
415 429
     }
416 430
 
417 431
     /**
@@ -448,8 +462,9 @@  discard block
 block discarded – undo
448 462
     static private function writeCSV($file, $data, $heading, \Closure $writer)
449 463
     {
450 464
         $fp = @fopen($file, 'w');
451
-        if (!$fp)
452
-            throw new OptimiseException('problem during creation of a file');
465
+        if (!$fp) {
466
+                    throw new OptimiseException('problem during creation of a file');
467
+        }
453 468
 
454 469
         fputcsv($fp, $heading);
455 470
 
@@ -511,8 +526,9 @@  discard block
 block discarded – undo
511 526
     {
512 527
         $f = function ($fp, $data) {
513 528
             foreach ($data as $key => $field) {
514
-                foreach ($field as $key2 => $field2)
515
-                    fputcsv($fp, [$key, $key2, $field2]);
529
+                foreach ($field as $key2 => $field2) {
530
+                                    fputcsv($fp, [$key, $key2, $field2]);
531
+                }
516 532
             }
517 533
         };
518 534
 
@@ -543,8 +559,9 @@  discard block
 block discarded – undo
543 559
         $strReplaceS = array('{USERS_PATH}', '{MEETINGS_PATH}', '{USER_AVAILABILITY_PATH}', '{MEETINGS_AVAILABILITY_PATH}', '{USER_MEETINGS_PATH}', '{MEETINGS_DURATION_PATH}', '{TIME_SLOTS}', '{MAX_TIME_SLOTS}', '{X_OUT_PATH}', '{Y_OUT_PATH}');
544 560
         $strReplaceR = array($this->path->getUsersPath(), $this->path->getMeetingsPath(), $this->path->getUsersAvailabilityPath(), $this->path->getMeetingsAvailabilityPath(), $this->path->getUsersMeetingsPath(), $this->path->getMeetingsDurationPath(), $this->timeSlots, $this->maxTimeSlots, $this->path->getXPath(), $this->path->getYPath());
545 561
         $f = @fopen($this->path->getModelPath(), "w");
546
-        if (!$f)
547
-            throw new OptimiseException('problem during creation of a file');
562
+        if (!$f) {
563
+                    throw new OptimiseException('problem during creation of a file');
564
+        }
548 565
         fwrite($f, str_replace($strReplaceS, $strReplaceR, file_get_contents(__DIR__ . "/model.stub")));
549 566
         fclose($f);
550 567
     }
@@ -565,12 +582,14 @@  discard block
 block discarded – undo
565 582
      */
566 583
     static private function readCSVFile($file)
567 584
     {
568
-        if (!file_exists($file) || !filesize($file))
569
-            throw new OptimiseException('no results file');
585
+        if (!file_exists($file) || !filesize($file)) {
586
+                    throw new OptimiseException('no results file');
587
+        }
570 588
 
571 589
         $handle = @fopen($file, "r");
572
-        if (!$handle)
573
-            throw new OptimiseException('problems during reading the file');
590
+        if (!$handle) {
591
+                    throw new OptimiseException('problems during reading the file');
592
+        }
574 593
 
575 594
         $ret = [];
576 595
         fgetcsv($handle); //skip head
@@ -603,8 +622,9 @@  discard block
 block discarded – undo
603 622
      */
604 623
     public function getOutput()
605 624
     {
606
-        if (!($data = file_get_contents($this->path->getOutputPath())))
607
-            throw new OptimiseException('problems during reading the file');
625
+        if (!($data = file_get_contents($this->path->getOutputPath()))) {
626
+                    throw new OptimiseException('problems during reading the file');
627
+        }
608 628
         return $data;
609 629
     }
610 630
 }
611 631
\ No newline at end of file
Please login to merge, or discard this patch.