|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Spatie\CalendarLinks\Console; |
|
4
|
|
|
|
|
5
|
|
|
use Symfony\Component\Console\Command\Command; |
|
6
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
7
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
8
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
9
|
|
|
use Spatie\CalendarLinks\Link; |
|
10
|
|
|
use DateTime; |
|
11
|
|
|
use Symfony\Component\Console\Style\SymfonyStyle; |
|
12
|
|
|
|
|
13
|
|
|
class CreateLinkCommand extends Command |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @var string |
|
17
|
|
|
*/ |
|
18
|
|
|
private $commandName = 'create'; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var string |
|
22
|
|
|
*/ |
|
23
|
|
|
private $commandDescription = 'Generate a link to create an event on specific service'; |
|
24
|
|
|
|
|
25
|
|
|
protected function configure(): void |
|
26
|
|
|
{ |
|
27
|
|
|
$helpContent = ' |
|
28
|
|
|
Example of a default request: |
|
29
|
|
|
|
|
30
|
|
|
<fg=black;bg=yellow>calendar_links create --to="2018-02-01 18:00"</> |
|
31
|
|
|
|
|
32
|
|
|
Example of a request with start and end date: |
|
33
|
|
|
|
|
34
|
|
|
<fg=black;bg=yellow>calendar_links create --from="2019-02-01 18:00" --to="2019-02-01 22:00"</></> |
|
35
|
|
|
|
|
36
|
|
|
Example of a request for multiple links: |
|
37
|
|
|
|
|
38
|
|
|
<fg=black;bg=yellow>calendar_links create --from="2019-02-01 18:00" --to="2019-02-01 22:00" -s google -s yahoo</> |
|
39
|
|
|
|
|
40
|
|
|
Example of a raw request: |
|
41
|
|
|
|
|
42
|
|
|
<fg=black;bg=yellow>calendar_links create --from="2019-02-01 18:00" --to="2019-02-01 22:00" -r true</> |
|
43
|
|
|
'; |
|
44
|
|
|
|
|
45
|
|
|
$this |
|
46
|
|
|
->setName($this->commandName) |
|
47
|
|
|
->setDescription($this->commandDescription) |
|
48
|
|
|
->setHelp($helpContent) |
|
49
|
|
|
->addOptions(); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): void |
|
53
|
|
|
{ |
|
54
|
|
|
$io = new SymfonyStyle($input, $output); |
|
55
|
|
|
$raw = $input->getOption('raw'); |
|
56
|
|
|
$services = $input->getOption('services'); |
|
57
|
|
|
$from = DateTime::createFromFormat('Y-m-d H:i', $input->getOption('from')); |
|
58
|
|
|
$to = DateTime::createFromFormat('Y-m-d H:i', $input->getOption('to')); |
|
59
|
|
|
|
|
60
|
|
|
$link = Link::create('Sebastian\'s birthday', $from, $to) |
|
|
|
|
|
|
61
|
|
|
->description('Cookies & cocktails!') |
|
62
|
|
|
->address('Samberstraat 69D, 2060 Antwerpen'); |
|
63
|
|
|
|
|
64
|
|
|
$contentOutput = function ($service) use ($raw, $link) { |
|
65
|
|
|
$link = $link->$service(); |
|
|
|
|
|
|
66
|
|
|
|
|
67
|
|
|
if ($raw) { |
|
68
|
|
|
return [$link, '']; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
return [ |
|
72
|
|
|
"<fg=black;bg=cyan>$service link</>", |
|
73
|
|
|
$link, |
|
74
|
|
|
'' |
|
75
|
|
|
]; |
|
76
|
|
|
}; |
|
77
|
|
|
|
|
78
|
|
|
if (!empty($services)) { |
|
79
|
|
|
foreach ($services as $service) { |
|
|
|
|
|
|
80
|
|
|
$output->writeln($contentOutput($service)); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
if (!$raw) { |
|
84
|
|
|
$io->success(''); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
return; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
$service = $io->choice('Select the service to which the link will be generated', ['google', 'yahoo', 'webOutlook', 'ics'], 'google'); |
|
91
|
|
|
|
|
92
|
|
|
$output->writeln($contentOutput($service)); |
|
93
|
|
|
|
|
94
|
|
|
$io->success(''); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
private function addOptions(): self |
|
98
|
|
|
{ |
|
99
|
|
|
$this |
|
100
|
|
|
->addOption('from', 'f', InputOption::VALUE_OPTIONAL, 'The event start date', date('Y-m-d H:i')) |
|
101
|
|
|
->addOption('to', 't', InputOption::VALUE_REQUIRED, 'The event end date') |
|
102
|
|
|
->addOption( |
|
103
|
|
|
'services', |
|
104
|
|
|
's', |
|
105
|
|
|
InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, |
|
106
|
|
|
'The service to which the link will be generated' |
|
107
|
|
|
) |
|
108
|
|
|
->addOption( |
|
109
|
|
|
'raw', |
|
110
|
|
|
'r', |
|
111
|
|
|
InputOption::VALUE_OPTIONAL, |
|
112
|
|
|
'Remove any formattation from output content (true|false)', |
|
113
|
|
|
false |
|
114
|
|
|
); |
|
115
|
|
|
|
|
116
|
|
|
return $this; |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|
This check looks for type mismatches where the missing type is
false. This is usually indicative of an error condtion.Consider the follow example
This function either returns a new
DateTimeobject or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returnedfalsebefore passing on the value to another function or method that may not be able to handle afalse.