move constructor to command base class
[shpub.git] / src / shpub / Command / Article.php
1 <?php
2 namespace shpub;
3
4 class Command_Article extends Command_AbstractProps
5 {
6     public static function opts(\Console_CommandLine $optParser)
7     {
8         $cmd = $optParser->addCommand('article');
9         $cmd->addOption(
10             'html',
11             array(
12                 'short_name'  => '-h',
13                 'long_name'   => '--html',
14                 'description' => 'Text content is HTML',
15                 'action'      => 'StoreTrue',
16                 'default'     => false,
17             )
18         );
19         static::optsGeneric($cmd);
20         $cmd->addArgument(
21             'title',
22             [
23                 'optional'    => false,
24                 'multiple'    => false,
25                 'description' => 'Title/Name',
26             ]
27         );
28         $cmd->addArgument(
29             'text',
30             [
31                 'optional'    => false,
32                 'multiple'    => false,
33                 'description' => 'Text content',
34             ]
35         );
36     }
37
38     public function run(\Console_CommandLine_Result $cmdRes)
39     {
40         $req = new Request($this->cfg->host, $this->cfg);
41         $req->req->addPostParameter('h', 'entry');
42         $req->req->addPostParameter('name', $cmdRes->args['title']);
43         if ($cmdRes->options['html']) {
44             $req->req->addPostParameter(
45                 'content[html]', $cmdRes->args['text']
46             );
47         } else {
48             $req->req->addPostParameter('content', $cmdRes->args['text']);
49         }
50         $this->handleGenericOptions($cmdRes, $req);
51
52         $res = $req->send();
53         $postUrl = $res->getHeader('Location');
54         Log::info('Article created at server');
55         Log::msg($postUrl);
56     }
57 }
58 ?>