Completed
Pull Request — master (#28)
by claudio
05:35
created

OptimiseCommand   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 12.12%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
c 1
b 0
f 0
lcom 1
cbo 5
dl 0
loc 92
ccs 4
cts 33
cp 0.1212
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A handle() 0 15 2
A syncAll() 0 14 4
A makeBackground() 0 6 2
A makeForeground() 0 6 1
1
<?php
2
3
namespace plunner\Console\Commands\Optimise;
4
5
use Illuminate\Console\Command;
6
use Illuminate\Console\Scheduling\Schedule;
7
use plunner\Company;
8
9
/**
10
 * Class OptimiseCommand
11
 * @package plunner\Console\Commands\Optimise
12
 * @author Claudio Cardinale <[email protected]>
13
 * @copyright 2015 Claudio Cardinale
14
 * @version 1.0.0
15
 */
16
class OptimiseCommand extends Command
17
{
18
    const BACKGROUND_MOD_MEX = 'background mode';
19
    const BACKGROUND_COMPLETED_MEX = 'All background tasks started';
20
    /**
21
     * The name and signature of the console command.
22
     *
23
     * @var string
24
     */
25
    protected $signature = 'optimise:meetings {companyId?}  {--background}';
26
27
    /**
28
     * The console command description.
29
     *
30
     * @var string
31
     */
32
    protected $description = 'Optimise meetings';
33
34
    /**
35
    * @var Schedule laravel schedule object needed to perform command in background
36
    */
37
    private $schedule;
38
39
    /**
40
     * Create a new command instance.
41
     * @param Schedule $schedule
42
     *
43
     */
44 6
    public function __construct(Schedule $schedule)
45
    {
46 6
        parent::__construct();
47 6
        $this->schedule = $schedule;
48 6
    }
49
50
    /**
51
     * Execute the console command.
52
     *
53
     * @return mixed
54
     */
55
    public function handle()
56
    {
57
        //
58
        //TODO check if glpk is installed
59
        //TODO multithreads
60
        //TODO log exceptions and fire
61
        //TODO insert a timeout
62
63
        //TODO try...catch with destruct
64
        $companyId = $this->argument('companyId');
65
        if(is_numeric($companyId))
66
            $this->makeForeground(Company::findOrFail($companyId));
67
        else
68
            $this->syncAll();
69
    }
70
71
    private function syncAll()
72
    {
73
        $calendars = Caldav::all();
74
        if($this->option('background')) {
75
            \Log::debug(self::BACKGROUND_MOD_MEX);
76
            $this->info(self::BACKGROUND_MOD_MEX);
77
            foreach ($calendars as $calendar)
78
                $this->makeBackground($calendar);
79
            \Log::debug(self::BACKGROUND_COMPLETED_MEX);
80
            $this->info(self::BACKGROUND_COMPLETED_MEX);
81
        }else
82
            foreach($calendars as $calendar)
83
                $this->makeForeground($calendar);
84
    }
85
86
    /**
87
     * optimise company via exec command
88
     * @param Company $company
89
     */
90
    private function makeBackground(Company $company)
91
    {
92
        $event = $this->schedule->command('optimise:meetings '.$company->id)->withoutOverlapping();
93
        if($event->isDue($this->laravel))
94
            $event->run($this->laravel);
95
    }
96
97
    /**
98
     * optimise company foreground
99
     * @param Company $company
100
     */
101
    private function makeForeground(Company $company)
102
    {
103
        $this->info('Optimisation company '. $company->id.' started');
104
        (new Optimise($company))->optimise()->save();
0 ignored issues
show
Bug introduced by
The call to Optimise::__construct() misses some required arguments starting with $schedule.
Loading history...
105
        $this->info('Optimisation '. $company->id.' completed');
106
    }
107
}
108