move generic options into separate class
authorChristian Weiske <cweiske@cweiske.de>
Tue, 13 Sep 2016 05:42:56 +0000 (07:42 +0200)
committerChristian Weiske <cweiske@cweiske.de>
Tue, 13 Sep 2016 05:42:56 +0000 (07:42 +0200)
src/shpub/Command/AbstractProps.php [new file with mode: 0644]
src/shpub/Command/Like.php
src/shpub/Command/Note.php
src/shpub/Command/Reply.php

diff --git a/src/shpub/Command/AbstractProps.php b/src/shpub/Command/AbstractProps.php
new file mode 100644 (file)
index 0000000..d1ef218
--- /dev/null
@@ -0,0 +1,165 @@
+<?php
+namespace shpub;
+
+/**
+ * Abstract command class that handles generic properties
+ */
+class Command_AbstractProps
+{
+    public static function optsGeneric(\Console_CommandLine_Command $cmd)
+    {
+        $cmd->addOption(
+            'categories',
+            array(
+                'short_name'  => '-c',
+                'long_name'   => '--category',
+                'description' => 'Category names',
+                'help_name'   => 'CAT',
+                'action'      => 'StoreArray',
+                'default'     => [],
+            )
+        );
+        $cmd->addOption(
+            'files',
+            array(
+                'short_name'  => '-f',
+                'long_name'   => '--files',
+                'description' => 'Files or URLs to upload',
+                'help_name'   => 'PATH',
+                'action'      => 'StoreArray',
+                'default'     => [],
+            )
+        );
+        $cmd->addOption(
+            'published',
+            array(
+                'long_name'   => '--published',
+                'description' => 'Publish date',
+                'help_name'   => 'DATE',
+                'action'      => 'StoreString',
+                'default'     => null,
+            )
+        );
+        $cmd->addOption(
+            'syndication',
+            array(
+                'short_name'  => '-s',
+                'long_name'   => '--syndication',
+                'description' => 'Syndication URL(s)',
+                'help_name'   => 'URL',
+                'action'      => 'StoreArray',
+                'default'     => [],
+            )
+        );
+        $cmd->addOption(
+            'x',
+            array(
+                'short_name'  => '-x',
+                'long_name'   => '--xprop',
+                'description' => 'Additional property',
+                'help_name'   => 'key=value',
+                'action'      => 'StoreArray',
+                'default'     => [],
+            )
+        );
+    }
+
+    protected function handleGenericOptions(
+        \Console_CommandLine_Result $cmdRes, Request $req
+    ) {
+        if ($cmdRes->options['published'] !== null) {
+            $req->req->addPostParameter(
+                'published', $cmdRes->options['published']
+            );
+        }
+        if (count($cmdRes->options['categories'])) {
+            $req->addPostParameter(
+                'category', $cmdRes->options['categories']
+            );
+        }
+        if (count($cmdRes->options['syndication'])) {
+            $req->addPostParameter(
+                'syndication', $cmdRes->options['syndication']
+            );
+        }
+
+        $this->handleFiles($cmdRes, $req);
+
+        if (count($cmdRes->options['x'])) {
+            $postParams = [];
+            foreach ($cmdRes->options['x'] as $xproperty) {
+                list($propkey, $propval) = explode('=', $xproperty, 2);
+                if (!isset($postParams[$propkey] )) {
+                    $postParams[$propkey] = [];
+                }
+                $postParams[$propkey][] = $propval;
+            }
+            foreach ($postParams as $propkey => $propvals) {
+                $req->addPostParameter($propkey, $propvals);
+            }
+        }
+    }
+
+    protected function handleFiles(
+        \Console_CommandLine_Result $cmdRes, Request $req
+    ) {
+        $files = $cmdRes->options['files'];
+        $fileList = $urlList = [
+            'audio' => [],
+            'image' => [],
+            'video' => [],
+        ];
+
+        foreach ($files as $filePath) {
+            if (strpos($filePath, '://') !== false) {
+                //url
+                $mte      = new \MIME_Type_Extension();
+                $mimetype = $mte->getMIMEType($filePath);
+                $media    = \MIME_Type::getMedia($mimetype);
+                if (!isset($urlList[$media])) {
+                    Log::err('File type not allowed: ' . $mimetype);
+                    exit(20);
+                }
+                $urlList[$media][] = $filePath;
+            } else if (file_exists($filePath)) {
+                //file
+                $mimetype = \MIME_Type::autoDetect($filePath);
+                $media    = \MIME_Type::getMedia($mimetype);
+                if (!isset($urlList[$media])) {
+                    Log::err('File type not allowed: ' . $mimetype);
+                    exit(20);
+                }
+                $fileList[$media][] = $filePath;
+            } else {
+                Log::err('File does not exist: ' . $filePath);
+                exit(20);
+            }
+        }
+        foreach ($urlList as $type => $urls) {
+            if ($type == 'image') {
+                $type = 'photo';
+            }
+            if (count($urls) == 1) {
+                $req->req->addPostParameter($type, reset($urls));
+            } else if (count($urls) > 1) {
+                $n = 0;
+                foreach ($urls as $url) {
+                    $req->req->addPostParameter(
+                        $type . '[' . $n++ . ']', $url
+                    );
+                }
+            }
+        }
+        foreach ($fileList as $type => $filePaths) {
+            if ($type == 'image') {
+                $type = 'photo';
+            }
+            if (count($filePaths) == 1) {
+                $req->addUpload($type, reset($filePaths));
+            } else if (count($filePaths) > 0) {
+                $req->addUpload($type, $filePaths);
+            }
+        }
+    }
+}
+?>
index e7c6052fc6866b458b01a820f7dd7a03bdc845ad..fb396be6c7eaf93519d86373615ec8579da2c79a 100644 (file)
@@ -1,7 +1,7 @@
 <?php
 namespace shpub;
 
-class Command_Like
+class Command_Like extends Command_AbstractProps
 {
     /**
      * @var Config
@@ -16,6 +16,7 @@ class Command_Like
     public static function opts(\Console_CommandLine $optParser)
     {
         $cmd = $optParser->addCommand('like');
+        static::optsGeneric($cmd);
         $cmd->addArgument(
             'url',
             [
@@ -25,22 +26,20 @@ class Command_Like
         );
     }
 
-    public function run($command)
+    public function run(\Console_CommandLine_Result $cmdRes)
     {
-        $url = Validator::url($command->args['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);
-        $res = $req->send($body);
+        $req->req->addPostParameter('h', 'entry');
+        $req->req->addPostParameter('like-of', $url);
+
+        $this->handleGenericOptions($cmdRes, $req);
+        $res = $req->send();
+
         $postUrl = $res->getHeader('Location');
         if ($postUrl === null) {
             Log::err('Error: Server sent no "Location" header and said:');
index cc3a5c178aba0e3081d3331321d3f167238baaf4..de6794890711f4d73fd9354c7583ff06370a683e 100644 (file)
@@ -1,7 +1,7 @@
 <?php
 namespace shpub;
 
-class Command_Note
+class Command_Note extends Command_AbstractProps
 {
     /**
      * @var Config
@@ -16,60 +16,7 @@ class Command_Note
     public static function opts(\Console_CommandLine $optParser)
     {
         $cmd = $optParser->addCommand('note');
-        $cmd->addOption(
-            'categories',
-            array(
-                'short_name'  => '-c',
-                'long_name'   => '--category',
-                'description' => 'Categories',
-                'help_name'   => 'CAT',
-                'action'      => 'StoreArray',
-                'default'     => [],
-            )
-        );
-        $cmd->addOption(
-            'files',
-            array(
-                'short_name'  => '-f',
-                'long_name'   => '--files',
-                'description' => 'Files to upload',
-                'help_name'   => 'PATH',
-                'action'      => 'StoreArray',
-                'default'     => [],
-            )
-        );
-        $cmd->addOption(
-            'published',
-            array(
-                'long_name'   => '--published',
-                'description' => 'Publish date',
-                'help_name'   => 'DATE',
-                'action'      => 'StoreString',
-                'default'     => null,
-            )
-        );
-        $cmd->addOption(
-            'syndication',
-            array(
-                'short_name'  => '-s',
-                'long_name'   => '--syndication',
-                'description' => 'Syndication URL(s)',
-                'help_name'   => 'URL',
-                'action'      => 'StoreArray',
-                'default'     => [],
-            )
-        );
-        $cmd->addOption(
-            'x',
-            array(
-                'short_name'  => '-x',
-                'long_name'   => '--xprop',
-                'description' => 'Additional property',
-                'help_name'   => 'key=value',
-                'action'      => 'StoreArray',
-                'default'     => [],
-            )
-        );
+        static::optsGeneric($cmd);
         $cmd->addArgument(
             'text',
             [
@@ -80,98 +27,12 @@ class Command_Note
         );
     }
 
-    public function run($command)
+    public function run(\Console_CommandLine_Result $cmdRes)
     {
         $req = new Request($this->cfg->host, $this->cfg);
         $req->req->addPostParameter('h', 'entry');
-        $req->req->addPostParameter('content', $command->args['text']);
-        if ($command->options['published'] !== null) {
-            $req->req->addPostParameter(
-                'published', $command->options['published']
-            );
-        }
-        if (count($command->options['categories'])) {
-            $req->addPostParameter(
-                'category', $command->options['categories']
-            );
-        }
-        if (count($command->options['syndication'])) {
-            $req->addPostParameter(
-                'syndication', $command->options['syndication']
-            );
-        }
-
-        $files = $command->options['files'];
-        $fileList = $urlList = [
-            'audio' => [],
-            'image' => [],
-            'video' => [],
-        ];
-
-        foreach ($files as $filePath) {
-            if (strpos($filePath, '://') !== false) {
-                //url
-                $mte      = new \MIME_Type_Extension();
-                $mimetype = $mte->getMIMEType($filePath);
-                $media    = \MIME_Type::getMedia($mimetype);
-                if (!isset($urlList[$media])) {
-                    Log::err('File type not allowed: ' . $mimetype);
-                    exit(20);
-                }
-                $urlList[$media][] = $filePath;
-            } else if (file_exists($filePath)) {
-                //file
-                $mimetype = \MIME_Type::autoDetect($filePath);
-                $media    = \MIME_Type::getMedia($mimetype);
-                if (!isset($urlList[$media])) {
-                    Log::err('File type not allowed: ' . $mimetype);
-                    exit(20);
-                }
-                $fileList[$media][] = $filePath;
-            } else {
-                Log::err('File does not exist: ' . $filePath);
-                exit(20);
-            }
-        }
-        foreach ($urlList as $type => $urls) {
-            if ($type == 'image') {
-                $type = 'photo';
-            }
-            if (count($urls) == 1) {
-                $req->req->addPostParameter($type, reset($urls));
-            } else if (count($urls) > 1) {
-                $n = 0;
-                foreach ($urls as $url) {
-                    $req->req->addPostParameter(
-                        $type . '[' . $n++ . ']', $url
-                    );
-                }
-            }
-        }
-        foreach ($fileList as $type => $filePaths) {
-            if ($type == 'image') {
-                $type = 'photo';
-            }
-            if (count($filePaths) == 1) {
-                $req->addUpload($type, reset($filePaths));
-            } else if (count($filePaths) > 0) {
-                $req->addUpload($type, $filePaths);
-            }
-        }
-
-        if (count($command->options['x'])) {
-            $postParams = [];
-            foreach ($command->options['x'] as $xproperty) {
-                list($propkey, $propval) = explode('=', $xproperty, 2);
-                if (!isset($postParams[$propkey] )) {
-                    $postParams[$propkey] = [];
-                }
-                $postParams[$propkey][] = $propval;
-            }
-            foreach ($postParams as $propkey => $propvals) {
-                $req->addPostParameter($propkey, $propvals);
-            }
-        }
+        $req->req->addPostParameter('content', $cmdRes->args['text']);
+        $this->handleGenericOptions($cmdRes, $req);
 
         $res = $req->send();
         $postUrl = $res->getHeader('Location');
index 622361a9d81f1aaa31b474e0088106a68ef5d2dd..e84a055f39d91d7fae1183b780500cf546ac7cce 100644 (file)
@@ -1,7 +1,7 @@
 <?php
 namespace shpub;
 
-class Command_Reply
+class Command_Reply extends Command_AbstractProps
 {
     /**
      * @var Config
@@ -16,6 +16,7 @@ class Command_Reply
     public static function opts(\Console_CommandLine $optParser)
     {
         $cmd = $optParser->addCommand('reply');
+        static::optsGeneric($cmd);
         $cmd->addArgument(
             'url',
             [
@@ -32,23 +33,22 @@ class Command_Reply
             ]
         );
     }
-    public function run($url, $text)
+
+    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',
-                'content'     => $text,
-                'in-reply-to' => $url,
-            ]
-        );
-
         $req = new Request($this->cfg->host, $this->cfg);
-        $res = $req->send($body);
+        $req->req->addPostParameter('h', 'entry');
+        $req->req->addPostParameter('content', implode(' ', $cmdRes->args['text']));
+        $req->req->addPostParameter('in-reply-to', $url);
+
+        $this->handleGenericOptions($cmdRes, $req);
+        $res = $req->send();
+
         $postUrl = $res->getHeader('Location');
         echo "Reply created at server\n";
         echo $postUrl . "\n";