move generic options into separate class
[shpub.git] / src / shpub / Command / Like.php
index 8baeee7e70901d47673ab2fb85941a14309cf72a..fb396be6c7eaf93519d86373615ec8579da2c79a 100644 (file)
@@ -1,52 +1,54 @@
 <?php
 namespace shpub;
 
-class Command_Like
+class Command_Like extends Command_AbstractProps
 {
     /**
-     * @var Config_Host
+     * @var Config
      */
-    protected $host;
+    protected $cfg;
 
-    public function __construct($host)
+    public function __construct($cfg)
     {
-        $this->host = $host;
+        $this->cfg = $cfg;
     }
 
-    public function run($url)
+    public static function opts(\Console_CommandLine $optParser)
     {
-        $url = Validator::url($url, 'url');
-        if ($url === false) {
-            exit(10);
-        }
-
-        $body = http_build_query(
+        $cmd = $optParser->addCommand('like');
+        static::optsGeneric($cmd);
+        $cmd->addArgument(
+            'url',
             [
-                'h'       => 'entry',
-                'like-of' => $url,
+                'optional'    => false,
+                'description' => 'URL that is liked',
             ]
         );
+    }
 
-        $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);
+    public function run(\Console_CommandLine_Result $cmdRes)
+    {
+        $url = Validator::url($cmdRes->args['url'], 'url');
+        if ($url === false) {
+            exit(10);
         }
-        $req->setHeader('Content-type', 'application/x-www-form-urlencoded');
-        $req->setHeader('Authorization', 'Bearer ' . $this->host->token);
-        $req->setBody($body);
+
+        $req = new Request($this->cfg->host, $this->cfg);
+        $req->req->addPostParameter('h', 'entry');
+        $req->req->addPostParameter('like-of', $url);
+
+        $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 {
+            echo "Like created at server\n";
+            echo $postUrl . "\n";
         }
-        $postUrl = $res->getHeader('Location');
-        echo "Like created at server\n";
-        echo $postUrl . "\n";
     }
 }
 ?>