move generic options into separate class
[shpub.git] / src / shpub / Command / Like.php
index 7daafc22d7ee4afb51ebe1729c72ec2d941fe9e5..fb396be6c7eaf93519d86373615ec8579da2c79a 100644 (file)
@@ -1,37 +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');
+        $cmd = $optParser->addCommand('like');
+        static::optsGeneric($cmd);
+        $cmd->addArgument(
+            'url',
+            [
+                'optional'    => false,
+                'description' => 'URL that is liked',
+            ]
+        );
+    }
+
+    public function run(\Console_CommandLine_Result $cmdRes)
+    {
+        $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->req->addPostParameter('h', 'entry');
+        $req->req->addPostParameter('like-of', $url);
+
+        $this->handleGenericOptions($cmdRes, $req);
+        $res = $req->send();
 
-        $req = new Request($this->host);
-        $res = $req->send($body);
         $postUrl = $res->getHeader('Location');
-        echo "Like created at server\n";
-        echo $postUrl . "\n";
+        if ($postUrl === null) {
+            Log::err('Error: Server sent no "Location" header and said:');
+            Log::err($res->getBody());
+            exit(20);
+        } else {
+            echo "Like created at server\n";
+            echo $postUrl . "\n";
+        }
     }
 }
 ?>