Completed
Push — master ( bc4e81...14a953 )
by claudio
04:04
created

SyncCaldav::handle()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 9
ccs 6
cts 6
cp 1
rs 9.6667
cc 2
eloc 6
nc 2
nop 0
crap 2
1
<?php
2
3
namespace plunner\Console\Commands\SyncCaldav;
4
5
use Illuminate\Console\Command;
6
use Illuminate\Console\Scheduling\Schedule;
7
use plunner\Caldav;
8
9
class SyncCaldav extends Command
10
{
11
    const BACKGROUND_MOD_MEX = 'background mode';
12
    const BACKGROUND_COMPLETED_MEX = 'All background tasks started';
13
14
    /**
15
     * The name and signature of the console command.
16
     *
17
     * @var string
18
     */
19
    protected $signature = 'sync:caldav {calendarId?} {--background}';
20
21
    /**
22
     * The console command description.
23
     *
24
     * @var string
25
     */
26
    protected $description = 'Sync caldav accounts';
27
28
29
    /**
30
     * @var Schedule laravel schedule object needed to perform command in background
31
     */
32
    private $schedule;
33
34
35
    /**
36
     * Create a new command instance.
37
     * @param Schedule $schedule
38
     *
39
     */
40 6
    public function __construct(Schedule $schedule)
41
    {
42 6
        parent::__construct();
43 6
        $this->schedule = $schedule;
44 6
    }
45
46
    /**
47
     * Execute the console command.
48
     *
49
     * @return mixed
50
     */
51 6
    public function handle()
52
    {
53
        //
54 6
        $calendarId = $this->argument('calendarId');
55 6
        if(is_numeric($calendarId))
56 6
            $this->makeForeground(Caldav::findOrFail($calendarId));
57
        else
58 6
            $this->syncAll();
59 6
    }
60
61 6
    private function syncAll()
62
    {
63 6
        $calendars = Caldav::all();
64 6
        if($this->option('background')) {
65 2
            \Log::debug(self::BACKGROUND_MOD_MEX);
66 2
            $this->info(self::BACKGROUND_MOD_MEX);
67 2
            foreach ($calendars as $calendar)
68 2
                $this->makeBackground($calendar);
69 2
            \Log::debug(self::BACKGROUND_COMPLETED_MEX);
70 2
            $this->info(self::BACKGROUND_COMPLETED_MEX);
71 2
        }else
72 4
            foreach($calendars as $calendar)
73 4
                $this->makeForeground($calendar);
74 6
    }
75
76
    /**
77
     * sync calendars via exec command
78
     * @param Caldav $calendar
79
     */
80 2
    private function makeBackground(Caldav $calendar)
81
    {
82 2
        $event = $this->schedule->command('sync:caldav '.$calendar->calendar_id)->withoutOverlapping();
83 2
        if($event->isDue($this->laravel))
84 2
             $event->run($this->laravel);
85 2
    }
86
87
    /**
88
     * sync calendars foreground
89
     * @param Caldav $calendar
90
     */
91 4
    private function makeForeground(Caldav $calendar)
92
    {
93 4
        $this->info('Sync calendar '. $calendar->calendar_id.' started');
94 4
        (new Sync($calendar))->sync();
95 4
        $this->info('Sync calendar '. $calendar->calendar_id.' completed');
96 4
    }
97
}
98