622361a9d81f1aaa31b474e0088106a68ef5d2dd
[shpub.git] / src / shpub / Command / Reply.php
1 <?php
2 namespace shpub;
3
4 class Command_Reply
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         $cmd->addArgument(
20             'url',
21             [
22                 'optional'    => false,
23                 'description' => 'URL that is replied to',
24             ]
25         );
26         $cmd->addArgument(
27             'text',
28             [
29                 'optional'    => false,
30                 'multiple'    => true,
31                 'description' => 'Reply text',
32             ]
33         );
34     }
35     public function run($url, $text)
36     {
37         $url = Validator::url($url, 'url');
38         if ($url === false) {
39             exit(10);
40         }
41
42         $body = http_build_query(
43             [
44                 'h'           => 'entry',
45                 'content'     => $text,
46                 'in-reply-to' => $url,
47             ]
48         );
49
50         $req = new Request($this->cfg->host, $this->cfg);
51         $res = $req->send($body);
52         $postUrl = $res->getHeader('Location');
53         echo "Reply created at server\n";
54         echo $postUrl . "\n";
55     }
56 }
57 ?>