add description for all commands
[shpub.git] / src / shpub / Command / Like.php
index 8baeee7e70901d47673ab2fb85941a14309cf72a..a619f7ae08bd6e4acf1677df3f225b2a19bd06b5 100644 (file)
@@ -1,52 +1,45 @@
 <?php
 namespace shpub;
 
-class Command_Like
+class Command_Like extends Command_AbstractProps
 {
-    /**
-     * @var Config_Host
-     */
-    protected $host;
-
-    public function __construct($host)
+    public static function opts(\Console_CommandLine $optParser)
     {
-        $this->host = $host;
+        $cmd = $optParser->addCommand('like');
+        $cmd->description = 'Create a like';
+        static::optsGeneric($cmd);
+        $cmd->addArgument(
+            'url',
+            [
+                'optional'    => false,
+                'description' => 'URL that is liked',
+            ]
+        );
     }
 
-    public function run($url)
+    public function run(\Console_CommandLine_Result $cmdRes)
     {
-        $url = Validator::url($url, 'url');
+        $url = Validator::url($cmdRes->args['url'], 'url');
         if ($url === false) {
             exit(10);
         }
 
-        $body = http_build_query(
-            [
-                'h'       => 'entry',
-                'like-of' => $url,
-            ]
-        );
+        $req = new Request($this->cfg->host, $this->cfg);
+        $req->setType('entry');
+        $req->addProperty('like-of', $url);
 
-        $req = new \HTTP_Request2($this->host->endpoints->micropub, 'POST');
-        if (version_compare(PHP_VERSION, '5.6.0', '<')) {
-            //correct ssl validation on php 5.5 is a pain, so disable
-            $req->setConfig('ssl_verify_host', false);
-            $req->setConfig('ssl_verify_peer', false);
-        }
-        $req->setHeader('Content-type', 'application/x-www-form-urlencoded');
-        $req->setHeader('Authorization', 'Bearer ' . $this->host->token);
-        $req->setBody($body);
+        $this->handleGenericOptions($cmdRes, $req);
         $res = $req->send();
-        if (intval($res->getStatus() / 100) != 2) {
-            Log::err(
-                'Server returned an error status code ' . $res->getStatus()
-            );
+
+        $postUrl = $res->getHeader('Location');
+        if ($postUrl === null) {
+            Log::err('Error: Server sent no "Location" header and said:');
             Log::err($res->getBody());
-            exit(11);
+            exit(20);
+        } else {
+            Log::info('Like created at server');
+            Log::msg($postUrl);
         }
-        $postUrl = $res->getHeader('Location');
-        echo "Like created at server\n";
-        echo $postUrl . "\n";
     }
 }
 ?>