OSDN Git Service

extract FetcherComment class
authorscribu <mail@scribu.net>
Sat, 9 Nov 2013 22:58:23 +0000 (00:58 +0200)
committerscribu <mail@scribu.net>
Sat, 9 Nov 2013 22:58:23 +0000 (00:58 +0200)
php/WP_CLI/FetcherComment.php [new file with mode: 0644]
php/commands/comment.php

diff --git a/php/WP_CLI/FetcherComment.php b/php/WP_CLI/FetcherComment.php
new file mode 100644 (file)
index 0000000..806ece1
--- /dev/null
@@ -0,0 +1,18 @@
+<?php
+
+namespace WP_CLI;
+
+class FetcherComment extends Fetcher {
+
+       public function get( $arg ) {
+               $comment_id = (int) $arg;
+               $comment = get_comment( $comment_id );
+
+               if ( is_null( $comment ) ) {
+                       return false;
+               }
+
+               return $comment;
+       }
+}
+
index 8d97df2..cb7c4f0 100644 (file)
@@ -18,6 +18,10 @@ class Comment_Command extends \WP_CLI\CommandWithDBObject {
                'comment_author_email',
        );
 
+       public function __construct() {
+               $this->fetcher = new \WP_CLI\FetcherComment( "Comment with ID %s does not exist." );
+       }
+
        /**
         * Insert a comment.
         *
@@ -192,7 +196,7 @@ class Comment_Command extends \WP_CLI\CommandWithDBObject {
        }
 
        private function set_status( $args, $status, $success ) {
-               $comment = $this->_fetch_comment( $args );
+               $comment = $this->fetcher->get_check( $args[0] );
 
                $r = wp_set_comment_status( $comment->comment_ID, 'approve', true );
 
@@ -364,24 +368,10 @@ class Comment_Command extends \WP_CLI\CommandWithDBObject {
         *     wp comment exists 1337
         */
        public function exists( $args ) {
-               if ( $this->_fetch_comment( $args ) ) {
+               if ( $this->fetcher->get( $args[0] ) ) {
                        WP_CLI::success( "Comment with ID $args[0] exists." );
                }
        }
-
-       /**
-        * A helper function fetching a comment object from comment_id.
-        */
-       private function _fetch_comment( $args ) {
-               $comment_id = (int) $args[0];
-               $comment = get_comment( $comment_id );
-
-               if ( is_null( $comment ) ) {
-                       WP_CLI::error( "Comment with ID $args[0] does not exist." );
-               }
-
-               return $comment;
-       }
 }
 
 WP_CLI::add_command( 'comment', 'Comment_Command' );