Completed
Pull Request — master (#10)
by claudio
03:57
created

SyncCaldav::syncAll()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 14
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 4

Importance

Changes 8
Bugs 0 Features 0
Metric Value
c 8
b 0
f 0
dl 0
loc 14
ccs 13
cts 13
cp 1
rs 9.2
cc 4
eloc 12
nc 4
nop 0
crap 4
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
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
40
     */
41 6
    public function __construct(Schedule $schedule)
42
    {
43 6
        parent::__construct();
44 6
        $this->schedule = $schedule;
45 6
    }
46
47
    /**
48
     * Execute the console command.
49
     *
50
     * @return mixed
51
     */
52 6
    public function handle()
53
    {
54
        //
55 6
        $calendarId = $this->argument('calendarId');
56 6
        if(is_numeric($calendarId))
57 6
            $this->makeForeground(Caldav::findOrFail($calendarId));
58
        else
59 6
            $this->syncAll();
60 6
    }
61
62 6
    private function syncAll()
63
    {
64 6
        $calendars = Caldav::all();
65 6
        if($this->option('background')) {
66 2
            \Log::debug(self::BACKGROUND_MOD_MEX);
67 2
            $this->info(self::BACKGROUND_MOD_MEX);
68 2
            foreach ($calendars as $calendar)
69 2
                $this->makeBackground($calendar);
70 2
            \Log::debug(self::BACKGROUND_COMPLETED_MEX);
71 2
            $this->info(self::BACKGROUND_COMPLETED_MEX);
72 2
        }else
73 4
            foreach($calendars as $calendar)
74 4
                $this->makeForeground($calendar);
75 6
    }
76
77
    /**
78
     * sync calendars via exec command
79
     * @param Caldav $calendar
80
     */
81 2
    private function makeBackground(Caldav $calendar)
82
    {
83 2
        $event = $this->schedule->command('sync:caldav '.$calendar->calendar_id)->withoutOverlapping();
84 2
        if($event->isDue($this->laravel))
85 2
             $event->run($this->laravel);
86 2
    }
87
88
    /**
89
     * sync calendars foreground
90
     * @param Caldav $calendar
91
     */
92 4
    private function makeForeground(Caldav $calendar)
93
    {
94 4
        $this->info('Sync calendar '. $calendar->calendar_id.' started');
95 4
        (new Sync($calendar))->sync();
96 4
        $this->info('Sync calendar '. $calendar->calendar_id.' completed');
97 4
    }
98
}
99