move generic options into separate class
[shpub.git] / src / shpub / Command / Reply.php
1 <?php
2 namespace shpub;
3
4 class Command_Reply extends Command_AbstractProps
5 {
6     /**
7      * @var Config
8      */
9     protected $cfg;
10
11     public function __construct($cfg)
12     {
13         $this->cfg = $cfg;
14     }
15
16     public static function opts(\Console_CommandLine $optParser)
17     {
18         $cmd = $optParser->addCommand('reply');
19         static::optsGeneric($cmd);
20         $cmd->addArgument(
21             'url',
22             [
23                 'optional'    => false,
24                 'description' => 'URL that is replied to',
25             ]
26         );
27         $cmd->addArgument(
28             'text',
29             [
30                 'optional'    => false,
31                 'multiple'    => true,
32                 'description' => 'Reply text',
33             ]
34         );
35     }
36
37     public function run(\Console_CommandLine_Result $cmdRes)
38     {
39         $url = Validator::url($cmdRes->args['url'], 'url');
40         if ($url === false) {
41             exit(10);
42         }
43
44         $req = new Request($this->cfg->host, $this->cfg);
45         $req->req->addPostParameter('h', 'entry');
46         $req->req->addPostParameter('content', implode(' ', $cmdRes->args['text']));
47         $req->req->addPostParameter('in-reply-to', $url);
48
49         $this->handleGenericOptions($cmdRes, $req);
50         $res = $req->send();
51
52         $postUrl = $res->getHeader('Location');
53         echo "Reply created at server\n";
54         echo $postUrl . "\n";
55     }
56 }
57 ?>