f46d230c3e52132404438a123dba79ce4ee3a059
[shpub.git] / src / shpub / Command / Rsvp.php
1 <?php
2 namespace shpub;
3
4 /**
5  * Repondez s'il vous plait - an answer to an invitation
6  */
7 class Command_Rsvp extends Command_AbstractProps
8 {
9     public static function opts(\Console_CommandLine $optParser)
10     {
11         $cmd = $optParser->addCommand('rsvp');
12         $cmd->description = 'Create a reservation';
13         static::addOptHtml($cmd);
14         static::optsGeneric($cmd);
15         $cmd->addArgument(
16             'url',
17             [
18                 'optional'    => false,
19                 'description' => 'URL that is replied to',
20             ]
21         );
22         $cmd->addArgument(
23             'rsvp',
24             [
25                 'optional'    => false,
26                 'multiple'    => false,
27                 'description' => 'Answer: yes, no, maybe',
28             ]
29         );
30         $cmd->addArgument(
31             'text',
32             [
33                 'optional'    => true,
34                 'multiple'    => false,
35                 'description' => 'Answer text',
36             ]
37         );
38     }
39
40     public function run(\Console_CommandLine_Result $cmdRes)
41     {
42         $url = Validator::url($cmdRes->args['url'], 'url');
43         if ($url === false) {
44             exit(10);
45         }
46         $rsvp = Validator::rsvp($cmdRes->args['rsvp']);
47         if ($rsvp === false) {
48             exit(10);
49         }
50
51         $req = new Request($this->cfg->host, $this->cfg);
52         $req->setType('h', 'entry');
53         $req->addProperty('in-reply-to', $url);
54         $req->addProperty('rsvp', $rsvp);
55         if ($cmdRes->args['text']) {
56             $req->addContent($cmdRes->args['text'], $cmdRes->options['html']);
57         }
58         $this->handleGenericOptions($cmdRes, $req);
59
60         $res = $req->send();
61         $postUrl = $res->getHeader('Location');
62         Log::info('RSVP created at server');
63         Log::msg($postUrl);
64     }
65 }
66 ?>