From b581aef8f2d616b6ce6ec3cd5ef2a59e1b91340b Mon Sep 17 00:00:00 2001
From: Trismegiste <florent.g@allopneus.com>
Date: Sun, 12 May 2013 22:08:35 +0200
Subject: [PATCH] adding concrete handler

---
 .../Responsible/FastStorage.php               | 37 ++++++++++++++++
 .../Responsible/SlowStorage.php               | 44 +++++++++++++++++++
 2 files changed, 81 insertions(+)
 create mode 100644 ChainOfResponsibilities/Responsible/FastStorage.php
 create mode 100644 ChainOfResponsibilities/Responsible/SlowStorage.php

diff --git a/ChainOfResponsibilities/Responsible/FastStorage.php b/ChainOfResponsibilities/Responsible/FastStorage.php
new file mode 100644
index 0000000..0011ea0
--- /dev/null
+++ b/ChainOfResponsibilities/Responsible/FastStorage.php
@@ -0,0 +1,37 @@
+<?php
+
+/*
+ * DesignPatternPHP
+ */
+
+namespace DesignPatterns\ChainOfResponsibilities\Responsible;
+
+use DesignPatterns\ChainOfResponsibilities\Handler;
+use DesignPatterns\ChainOfResponsibilities\Request;
+
+class FastStorage extends Handler
+{
+
+    protected $_data = array();
+
+    public function __construct($data = array())
+    {
+        $this->_data = $data;
+    }
+
+    protected function processing(Request $req)
+    {
+        if ('get' === $req->verb) {
+            if (array_key_exists($req->key, $this->_data)) {
+                // the handler IS responsible and then processes the request
+                $req->response = $this->_data[$req->key];
+                // instead of returning true, I could return the value but it proves
+                // to be a bad idea. What if the value IS "false" ?
+                return true;
+            }
+        }
+
+        return false;
+    }
+
+}
diff --git a/ChainOfResponsibilities/Responsible/SlowStorage.php b/ChainOfResponsibilities/Responsible/SlowStorage.php
new file mode 100644
index 0000000..79635bf
--- /dev/null
+++ b/ChainOfResponsibilities/Responsible/SlowStorage.php
@@ -0,0 +1,44 @@
+<?php
+
+/*
+ * DesignPatternPHP
+ */
+
+namespace DesignPatterns\ChainOfResponsibilities\Responsible;
+
+use DesignPatterns\ChainOfResponsibilities\Handler;
+use DesignPatterns\ChainOfResponsibilities\Request;
+
+/**
+ * This is mostly the same code as FastStorage but in fact, it may greatly differs
+ * 
+ * One important fact about CoR : each item in the chain MUST NOT assume its position
+ * in the chain. A CoR is not responsible if the request is not handled UNLESS
+ * you make an "ExceptionHandler" which throws execption if the request goes there.
+ * 
+ * To be really extendable, each handler doesn't know if there is something after him.
+ * 
+ */
+class SlowStorage extends Handler
+{
+
+    protected $_data = array();
+
+    public function __construct($data = array())
+    {
+        $this->_data = $data;
+    }
+
+    protected function processing(Request $req)
+    {
+        if ('get' === $req->verb) {
+            if (array_key_exists($req->key, $this->_data)) {
+                $req->response = $this->_data[$req->key];
+                return true;
+            }
+        }
+
+        return false;
+    }
+
+}