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 f807d329712a0ee00ea27dd94360a1835c7daf30..8ec4acbfea6a0f1431fa5be6fa95283ca0994985 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 26f615a163e8d5e2f7643a71e866b58d70d1c441..e7c6052fc6866b458b01a820f7dd7a03bdc845ad 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 327111dbcc6a345eb14b7f875af654d58f14fbb8..622361a9d81f1aaa31b474e0088106a68ef5d2dd 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');