move option definition into command classes
[shpub.git] / src / shpub / Command / Like.php
1 <?php
2 namespace shpub;
3
4 class Command_Like
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('like');
19         $cmd->addArgument(
20             'url',
21             [
22                 'optional'    => false,
23                 'description' => 'URL that is liked',
24             ]
25         );
26     }
27
28     public function run($command)
29     {
30         $url = Validator::url($command->args['url'], 'url');
31         if ($url === false) {
32             exit(10);
33         }
34
35         $body = http_build_query(
36             [
37                 'h'       => 'entry',
38                 'like-of' => $url,
39             ]
40         );
41
42         $req = new Request($this->cfg->host, $this->cfg);
43         $res = $req->send($body);
44         $postUrl = $res->getHeader('Location');
45         if ($postUrl === null) {
46             Log::err('Error: Server sent no "Location" header and said:');
47             Log::err($res->getBody());
48             exit(20);
49         } else {
50             echo "Like created at server\n";
51             echo $postUrl . "\n";
52         }
53     }
54 }
55 ?>