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
|
|
|
/** |
10
|
|
|
* Class SyncCaldav |
11
|
|
|
* @package plunner\Console\Commands\SyncCaldav |
12
|
|
|
* @author Claudio Cardinale <[email protected]> |
13
|
|
|
* @copyright 2015 Claudio Cardinale |
14
|
|
|
* @version 1.0.0 |
15
|
|
|
*/ |
16
|
|
|
class SyncCaldav extends Command |
17
|
|
|
{ |
18
|
|
|
const BACKGROUND_MOD_MEX = 'background mode'; |
19
|
|
|
const BACKGROUND_COMPLETED_MEX = 'All background tasks started'; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* The name and signature of the console command. |
23
|
|
|
* |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
protected $signature = 'sync:caldav {calendarId?} {--background}'; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* The console command description. |
30
|
|
|
* |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
protected $description = 'Sync caldav accounts'; |
34
|
|
|
|
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var Schedule laravel schedule object needed to perform command in background |
38
|
|
|
*/ |
39
|
|
|
private $schedule; |
40
|
|
|
|
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Create a new command instance. |
44
|
|
|
* @param Schedule $schedule |
45
|
|
|
* |
46
|
|
|
*/ |
47
|
15 |
|
public function __construct(Schedule $schedule) |
48
|
|
|
{ |
49
|
15 |
|
parent::__construct(); |
50
|
15 |
|
$this->schedule = $schedule; |
51
|
15 |
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Execute the console command. |
55
|
|
|
* |
56
|
|
|
* @return mixed |
57
|
|
|
*/ |
58
|
9 |
|
public function handle() |
59
|
|
|
{ |
60
|
|
|
// |
61
|
9 |
|
$calendarId = $this->argument('calendarId'); |
62
|
9 |
|
if (is_numeric($calendarId)) |
63
|
6 |
|
$this->makeForeground(Caldav::findOrFail($calendarId)); |
64
|
|
|
else |
65
|
9 |
|
$this->syncAll(); |
66
|
9 |
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* sync calendars foreground |
70
|
|
|
* @param Caldav $calendar |
71
|
|
|
*/ |
72
|
6 |
|
private function makeForeground(Caldav $calendar) |
73
|
|
|
{ |
74
|
6 |
|
$this->info('Sync calendar ' . $calendar->calendar_id . ' started'); |
75
|
6 |
|
(new Sync($calendar))->sync(); |
76
|
|
|
//TODO show errors as warning |
77
|
6 |
|
$this->info('Sync calendar ' . $calendar->calendar_id . ' completed'); |
78
|
6 |
|
} |
79
|
|
|
|
80
|
9 |
|
private function syncAll() |
81
|
|
|
{ |
82
|
|
|
//TODO if are there no calendars? |
83
|
9 |
|
$calendars = Caldav::all(); |
84
|
9 |
|
if ($this->option('background')) { |
85
|
3 |
|
\Log::debug(self::BACKGROUND_MOD_MEX); |
86
|
3 |
|
$this->info(self::BACKGROUND_MOD_MEX); |
87
|
3 |
|
foreach ($calendars as $calendar) |
88
|
3 |
|
$this->makeBackground($calendar); |
89
|
3 |
|
\Log::debug(self::BACKGROUND_COMPLETED_MEX); |
90
|
3 |
|
$this->info(self::BACKGROUND_COMPLETED_MEX); |
91
|
2 |
|
} else |
92
|
6 |
|
foreach ($calendars as $calendar) |
93
|
6 |
|
$this->makeForeground($calendar); |
94
|
9 |
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* sync calendars via exec command |
98
|
|
|
* @param Caldav $calendar |
99
|
|
|
*/ |
100
|
3 |
|
private function makeBackground(Caldav $calendar) |
101
|
|
|
{ |
102
|
3 |
|
$event = $this->schedule->command('sync:caldav ' . $calendar->calendar_id)->withoutOverlapping(); |
103
|
3 |
|
if ($event->isDue($this->laravel)) |
104
|
3 |
|
$event->run($this->laravel); |
105
|
3 |
|
} |
106
|
|
|
} |
107
|
|
|
|