Add command to list syndication targets
authorChristian Weiske <cweiske@cweiske.de>
Fri, 31 Mar 2017 19:42:40 +0000 (21:42 +0200)
committerChristian Weiske <cweiske@cweiske.de>
Fri, 31 Mar 2017 19:42:40 +0000 (21:42 +0200)
Part of https://github.com/cweiske/shpub/issues/2

src/shpub/Cli.php
src/shpub/Command/Targets.php [new file with mode: 0644]

index 8ea550e..733f4df 100644 (file)
@@ -119,6 +119,7 @@ class Cli
 
         Command_Connect::opts($optParser);
         Command_Server::opts($optParser);
 
         Command_Connect::opts($optParser);
         Command_Server::opts($optParser);
+        Command_Targets::opts($optParser);
 
         Command_Article::opts($optParser);
         Command_Note::opts($optParser);
 
         Command_Article::opts($optParser);
         Command_Note::opts($optParser);
diff --git a/src/shpub/Command/Targets.php b/src/shpub/Command/Targets.php
new file mode 100644 (file)
index 0000000..eb66692
--- /dev/null
@@ -0,0 +1,55 @@
+<?php
+namespace shpub;
+
+/**
+ * List syndication targets
+ *
+ * @author  Christian Weiske <cweiske@cweiske.de>
+ * @license http://www.gnu.org/licenses/agpl.html GNU AGPL v3
+ * @link    http://cweiske.de/shpub.htm
+ */
+class Command_Targets
+{
+    public function __construct(Config $cfg)
+    {
+        $this->cfg = $cfg;
+    }
+
+    public static function opts(\Console_CommandLine $optParser)
+    {
+        $cmd = $optParser->addCommand('targets');
+        $cmd->description = 'List a server\'s syndication targets';
+    }
+
+    public function run(\Console_CommandLine_Result $cmdRes)
+    {
+        $req = new Request($this->cfg->host, $this->cfg);
+        $req->req->setMethod('GET');
+        $req->req->setHeader('Content-type');
+        $req->req->getUrl()->setQueryVariable('q', 'syndicate-to');
+        $res = $req->send();
+
+        if ($res->getHeader('content-type') != 'application/json') {
+            Log::err('response data are not of type application/json');
+            exit(2);
+        }
+
+        $data = json_decode($res->getBody(), true);
+        if (!isset($data['syndicate-to'])) {
+            Log::err('"syndicate-to" property missing');
+            exit(2);
+        }
+
+        foreach ($data['syndicate-to'] as $target) {
+            Log::msg($target['name']);
+            Log::msg(' ' . $target['uid']);
+            if (isset($target['user'])) {
+                Log::msg(' User: ' . $target['user']['name']);
+            }
+            if (isset($target['service'])) {
+                Log::msg(' Service: ' . $target['service']['name']);
+            }
+        }
+    }
+}
+?>