From e23857380fed4b7b4d02bc2d1519dece5207bbc2 Mon Sep 17 00:00:00 2001
From: Gabriel Dias <diasgab@gmail.com>
Date: Sat, 19 Dec 2009 00:17:14 -0300
Subject: [PATCH] Created a blog_filter_factory

---
 blog/locallib.php |   76 ++++++++++++++++++++++++++++++++---------------------
 1 files changed, 46 insertions(+), 30 deletions(-)

diff --git a/blog/locallib.php b/blog/locallib.php
index 565ea9c..86dbc5b 100644
--- a/blog/locallib.php
+++ b/blog/locallib.php
@@ -694,7 +694,7 @@ class blog_listing {
         // Unset filters overridden by more specific filters
         foreach ($filters as $type => $id) {
             if (!empty($type) && !empty($id)) {
-                $this->filters[$type] = blog_filter::get_instance($id, $type);
+                $this->filters[$type] = blog_filter_factory::factory($id, $type);
             }
         }
 
@@ -904,6 +904,51 @@ class blog_listing {
 }
 
 /**
+ * blog_filter_factory is used to manufacture an instance of required filter blog.
+ *
+ */
+class blog_filter_factory {
+
+    /**
+     * Create a new blog_filter object
+     *
+     * @param int    $id
+     * @param string $type
+     * @return instance of filter blog
+     */
+    public static function factory($id, $type) {
+
+       switch ($type) {
+            case 'site':
+            case 'course':
+            case 'module':
+                $classname = 'blog_filter_context';
+                break;
+
+            case 'group':
+            case 'user':
+                $classname = 'blog_filter_user';
+                break;
+
+            case 'tag':
+                $classname = 'blog_filter_tag';
+                break;
+
+            default:
+                $classname = 'blog_filter_' . $type;
+
+        }
+        if (class_exists($classname)) {
+            return new $classname($id, $type);
+        }else {
+           debugging('Error when calling '. $classname .' class.');
+           return false;
+        }
+    }
+}
+
+
+/**
  * Abstract class for blog_filter objects.
  * A set of core filters are implemented here. To write new filters, you need to subclass
  * blog_filter and give it the name of the type you want (for example, blog_filter_entry).
@@ -959,35 +1004,6 @@ abstract class blog_filter {
         $this->type = $type;
     }
 
-    /**
-     * TODO This is poor design. A parent class should not know anything about its children.
-     * The default case helps to resolve this design issue
-     */
-    public static function get_instance($id, $type) {
-
-        switch ($type) {
-            case 'site':
-            case 'course':
-            case 'module':
-                return new blog_filter_context($id, $type);
-                break;
-
-            case 'group':
-            case 'user':
-                return new blog_filter_user($id, $type);
-                break;
-
-            case 'tag':
-                return new blog_filter_tag($id);
-                break;
-
-            default:
-                $classname = "blog_filter_$type";
-                if (class_exists($classname)) {
-                    return new $classname($id, $type);
-                }
-        }
-    }
 }
 
 /**
-- 
1.6.0.4

