add description for all commands
[shpub.git] / src / shpub / Command / Update.php
1 <?php
2 namespace shpub;
3
4 class Command_Update extends Command_AbstractProps
5 {
6     public static function opts(\Console_CommandLine $optParser)
7     {
8         $cmd = $optParser->addCommand('update');
9         $cmd->description = 'Modify an existing post';
10         $cmd->addOption(
11             'add',
12             array(
13                 'short_name'  => '-a',
14                 'long_name'   => '--add',
15                 'description' => 'Property to add',
16                 'help_name'   => 'PROP=VAL',
17                 'action'      => 'StoreArray',
18                 'default'     => [],
19             )
20         );
21         $cmd->addOption(
22             'replace',
23             array(
24                 'short_name'  => '-r',
25                 'long_name'   => '--replace',
26                 'description' => 'Property to remove',
27                 'help_name'   => 'PROP=VAL',
28                 'action'      => 'StoreArray',
29                 'default'     => [],
30             )
31         );
32         $cmd->addOption(
33             'delete',
34             array(
35                 'short_name'  => '-d',
36                 'long_name'   => '--delete',
37                 'description' => 'Property to delete',
38                 'help_name'   => 'PROP=VAL',
39                 'action'      => 'StoreArray',
40                 'default'     => [],
41             )
42         );
43         $cmd->addArgument(
44             'url',
45             [
46                 'optional'    => false,
47                 'multiple'    => false,
48                 'description' => 'Post URL',
49             ]
50         );
51     }
52
53     public function run(\Console_CommandLine_Result $cmdRes)
54     {
55         $url = Validator::url($cmdRes->args['url'], 'url');
56         if ($url === false) {
57             exit(10);
58         }
59
60         $req = new Request($this->cfg->host, $this->cfg);
61
62         $json = [
63             'action' => 'update',
64             'url'    => $url,
65         ];
66
67         $parts = [
68             'add'     => [],
69             'delete'  => [],
70             'replace' => [],
71         ];
72         foreach (['add', 'delete', 'replace'] as $part) {
73             if (!count($cmdRes->options[$part])) {
74                 continue;
75             }
76             foreach ($cmdRes->options[$part] as $kvpair) {
77                 list($prop, $val) = explode('=', $kvpair, 2);
78                 if (!isset($parts[$part][$prop])) {
79                     $parts[$part][$prop] = [];
80                 }
81                 $parts[$part][$prop][] = $val;
82             }
83         }
84         foreach ($parts as $part => $changes) {
85             if (count($changes)) {
86                 $json[$part] = $changes;
87             }
88         }
89
90         $req->req->setHeader('Content-Type: application/json');
91         $res = $req->send(json_encode($json));
92         $newPostUrl = $res->getHeader('Location');
93         Log::info('Post updated at server');
94         if ($newPostUrl) {
95             Log::info('Post has a new URL:');
96             Log::msg($newPostUrl);
97         }
98     }
99 }
100 ?>