move constructor to command base class
[shpub.git] / src / shpub / Command / Repost.php
1 <?php
2 namespace shpub;
3
4 class Command_Repost extends Command_AbstractProps
5 {
6     public static function opts(\Console_CommandLine $optParser)
7     {
8         $cmd = $optParser->addCommand('repost');
9         static::optsGeneric($cmd);
10         $cmd->addArgument(
11             'url',
12             [
13                 'optional'    => false,
14                 'description' => 'URL that shall be reposted',
15             ]
16         );
17     }
18
19     public function run(\Console_CommandLine_Result $cmdRes)
20     {
21         $url = Validator::url($cmdRes->args['url'], 'url');
22         if ($url === false) {
23             exit(10);
24         }
25
26         $req = new Request($this->cfg->host, $this->cfg);
27         $req->req->addPostParameter('h', 'entry');
28         $req->req->addPostParameter('repost-of', $url);
29
30         $this->handleGenericOptions($cmdRes, $req);
31         $res = $req->send();
32
33         $postUrl = $res->getHeader('Location');
34         if ($postUrl === null) {
35             Log::err('Error: Server sent no "Location" header and said:');
36             Log::err($res->getBody());
37             exit(20);
38         } else {
39             Log::info('Repost created at server');
40             Log::msg($postUrl);
41         }
42     }
43 }
44 ?>