26f615a163e8d5e2f7643a71e866b58d70d1c441
[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 function run($url)
17     {
18         $url = Validator::url($url, 'url');
19         if ($url === false) {
20             exit(10);
21         }
22
23         $body = http_build_query(
24             [
25                 'h'       => 'entry',
26                 'like-of' => $url,
27             ]
28         );
29
30         $req = new Request($this->cfg->host, $this->cfg);
31         $res = $req->send($body);
32         $postUrl = $res->getHeader('Location');
33         if ($postUrl === null) {
34             Log::err('Error: Server sent no "Location" header and said:');
35             Log::err($res->getBody());
36             exit(20);
37         } else {
38             echo "Like created at server\n";
39             echo $postUrl . "\n";
40         }
41     }
42 }
43 ?>