move option definition into command classes
authorChristian Weiske <cweiske@cweiske.de>
Thu, 8 Sep 2016 15:04:16 +0000 (17:04 +0200)
committerChristian Weiske <cweiske@cweiske.de>
Thu, 8 Sep 2016 15:04:16 +0000 (17:04 +0200)
src/shpub/Cli.php
src/shpub/Command/Like.php
src/shpub/Command/Note.php [new file with mode: 0644]
src/shpub/Command/Reply.php

index f807d32..8ec4acb 100644 (file)
@@ -33,24 +33,12 @@ class Cli
                 $cmd->run($res->command->options['verbose']);
                 break;
 
-            case 'like':
-                $this->requireValidHost();
-                $cmd = new Command_Like($this->cfg);
-                $cmd->run($res->command->args['url']);
-                break;
-
-            case 'reply':
+            default:
+                $class = 'shpub\\Command_' . ucfirst($res->command_name);
                 $this->requireValidHost();
-                $cmd = new Command_Reply($this->cfg);
-                $cmd->run(
-                    $res->command->args['url'],
-                    implode(' ', $res->command->args['text'])
-                );
+                $cmd = new $class($this->cfg);
+                $cmd->run($res->command);
                 break;
-
-            default:
-                var_dump($this->cfg->host, $res);
-                Log::err('FIXME');
             }
         } catch (\Exception $e) {
             echo 'Error: ' . $e->getMessage() . "\n";
@@ -186,32 +174,9 @@ class Cli
             )
         );
 
-        //$cmd = $optParser->addCommand('post');
-        $cmd = $optParser->addCommand('reply');
-        $cmd->addArgument(
-            'url',
-            [
-                'optional'    => false,
-                'description' => 'URL that is replied to',
-            ]
-        );
-        $cmd->addArgument(
-            'text',
-            [
-                'optional'    => false,
-                'multiple'    => true,
-                'description' => 'Reply text',
-            ]
-        );
-
-        $cmd = $optParser->addCommand('like');
-        $cmd->addArgument(
-            'url',
-            [
-                'optional'    => false,
-                'description' => 'URL that is liked',
-            ]
-        );
+        Command_Note::opts($optParser);
+        Command_Reply::opts($optParser);
+        Command_Like::opts($optParser);
 
         return $optParser;
     }
index 26f615a..e7c6052 100644 (file)
@@ -13,9 +13,21 @@ class Command_Like
         $this->cfg = $cfg;
     }
 
-    public function run($url)
+    public static function opts(\Console_CommandLine $optParser)
     {
-        $url = Validator::url($url, 'url');
+        $cmd = $optParser->addCommand('like');
+        $cmd->addArgument(
+            'url',
+            [
+                'optional'    => false,
+                'description' => 'URL that is liked',
+            ]
+        );
+    }
+
+    public function run($command)
+    {
+        $url = Validator::url($command->args['url'], 'url');
         if ($url === false) {
             exit(10);
         }
diff --git a/src/shpub/Command/Note.php b/src/shpub/Command/Note.php
new file mode 100644 (file)
index 0000000..a60422a
--- /dev/null
@@ -0,0 +1,45 @@
+<?php
+namespace shpub;
+
+class Command_Note
+{
+    /**
+     * @var Config
+     */
+    protected $cfg;
+
+    public function __construct($cfg)
+    {
+        $this->cfg = $cfg;
+    }
+
+    public static function opts(\Console_CommandLine $optParser)
+    {
+        $cmd = $optParser->addCommand('note');
+        $cmd->addArgument(
+            'text',
+            [
+                'optional'    => false,
+                'multiple'    => false,
+                'description' => 'Post text',
+            ]
+        );
+    }
+
+    public function run($command)
+    {
+        $data = [
+            'h'       => 'entry',
+            'content' => $command->args['text'],
+        ];
+
+        $body = http_build_query($data);
+
+        $req = new Request($this->cfg->host, $this->cfg);
+        $res = $req->send($body);
+        $postUrl = $res->getHeader('Location');
+        echo "Post created at server\n";
+        echo $postUrl . "\n";
+    }
+}
+?>
index 327111d..622361a 100644 (file)
@@ -13,6 +13,25 @@ class Command_Reply
         $this->cfg = $cfg;
     }
 
+    public static function opts(\Console_CommandLine $optParser)
+    {
+        $cmd = $optParser->addCommand('reply');
+        $cmd->addArgument(
+            'url',
+            [
+                'optional'    => false,
+                'description' => 'URL that is replied to',
+            ]
+        );
+        $cmd->addArgument(
+            'text',
+            [
+                'optional'    => false,
+                'multiple'    => true,
+                'description' => 'Reply text',
+            ]
+        );
+    }
     public function run($url, $text)
     {
         $url = Validator::url($url, 'url');