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