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