Add command to list syndication targets
[shpub.git] / src / shpub / Command / Targets.php
1 <?php
2 namespace shpub;
3
4 /**
5  * List syndication targets
6  *
7  * @author  Christian Weiske <cweiske@cweiske.de>
8  * @license http://www.gnu.org/licenses/agpl.html GNU AGPL v3
9  * @link    http://cweiske.de/shpub.htm
10  */
11 class Command_Targets
12 {
13     public function __construct(Config $cfg)
14     {
15         $this->cfg = $cfg;
16     }
17
18     public static function opts(\Console_CommandLine $optParser)
19     {
20         $cmd = $optParser->addCommand('targets');
21         $cmd->description = 'List a server\'s syndication targets';
22     }
23
24     public function run(\Console_CommandLine_Result $cmdRes)
25     {
26         $req = new Request($this->cfg->host, $this->cfg);
27         $req->req->setMethod('GET');
28         $req->req->setHeader('Content-type');
29         $req->req->getUrl()->setQueryVariable('q', 'syndicate-to');
30         $res = $req->send();
31
32         if ($res->getHeader('content-type') != 'application/json') {
33             Log::err('response data are not of type application/json');
34             exit(2);
35         }
36
37         $data = json_decode($res->getBody(), true);
38         if (!isset($data['syndicate-to'])) {
39             Log::err('"syndicate-to" property missing');
40             exit(2);
41         }
42
43         foreach ($data['syndicate-to'] as $target) {
44             Log::msg($target['name']);
45             Log::msg(' ' . $target['uid']);
46             if (isset($target['user'])) {
47                 Log::msg(' User: ' . $target['user']['name']);
48             }
49             if (isset($target['service'])) {
50                 Log::msg(' Service: ' . $target['service']['name']);
51             }
52         }
53     }
54 }
55 ?>