summaryrefslogtreecommitdiffstats
path: root/src/Controller/CheckResponse.php
diff options
context:
space:
mode:
authorArnold Daniels <arnold@jasny.net>2016-11-17 21:40:25 +0100
committerArnold Daniels <arnold@jasny.net>2016-11-17 21:40:25 +0100
commit1d84d01f35547bb53a78f33d41a8002f12cbf18c (patch)
treefd9892a7998db822b05229abb7ad06ead8dd58ed /src/Controller/CheckResponse.php
parentc04808754395a7c0454921dcd2e14596bea20745 (diff)
downloadcontroller-1d84d01f35547bb53a78f33d41a8002f12cbf18c.zip
controller-1d84d01f35547bb53a78f33d41a8002f12cbf18c.tar.gz
controller-1d84d01f35547bb53a78f33d41a8002f12cbf18c.tar.bz2
Refactor controller. Move stuff to traits.
Diffstat (limited to 'src/Controller/CheckResponse.php')
-rw-r--r--src/Controller/CheckResponse.php75
1 files changed, 75 insertions, 0 deletions
diff --git a/src/Controller/CheckResponse.php b/src/Controller/CheckResponse.php
new file mode 100644
index 0000000..7c02d46
--- /dev/null
+++ b/src/Controller/CheckResponse.php
@@ -0,0 +1,75 @@
+<?php
+
+namespace Jasny\Controller;
+
+use Psr\Http\Message\ResponseInterface;
+
+/**
+ * Methods to check the response
+ */
+trait CheckResponse
+{
+ /**
+ * Get response. set for controller
+ *
+ * @return ResponseInterface
+ */
+ abstract protected function getResponse();
+
+
+ /**
+ * Check if response is 2xx succesful, or empty
+ *
+ * @return boolean
+ */
+ public function isSuccessful()
+ {
+ $code = $this->getResponse()->getStatusCode() ?: 200;
+
+ return !$code || ($code >= 200 && $code < 300);
+ }
+
+ /**
+ * Check if response is a 3xx redirect
+ *
+ * @return boolean
+ */
+ public function isRedirection()
+ {
+ $code = $this->getResponse()->getStatusCode() ?: 200;
+
+ return $code >= 300 && $code < 400;
+ }
+
+ /**
+ * Check if response is a 4xx client error
+ *
+ * @return boolean
+ */
+ public function isClientError()
+ {
+ $code = $this->getResponse()->getStatusCode() ?: 200;
+
+ return $code >= 400 && $code < 500;
+ }
+
+ /**
+ * Check if response is a 5xx redirect
+ *
+ * @return boolean
+ */
+ public function isServerError()
+ {
+ return $this->getResponse()->getStatusCode() ?: 200 >= 500;
+ }
+
+ /**
+ * Check if response is 4xx or 5xx error
+ *
+ * @return boolean
+ */
+ public function isError()
+ {
+ return $this->isClientError() || $this->isServerError();
+ }
+}