Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 41 |
|
0.00% |
0 / 7 |
CRAP | |
0.00% |
0 / 1 |
| ResponseAbstract | |
0.00% |
0 / 41 |
|
0.00% |
0 / 7 |
342 | |
0.00% |
0 / 1 |
| chooseRenderer | |
0.00% |
0 / 17 |
|
0.00% |
0 / 1 |
72 | |||
| sendHeaders | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
12 | |||
| setHttpCode | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| setAllowedMethods | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| send | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
6 | |||
| trace | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| setException | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | /* |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; under version 2 |
| 7 | * of the License (non-upgradable). |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | * |
| 18 | * Copyright (c) 2016 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT); |
| 19 | */ |
| 20 | |
| 21 | namespace oat\tao\model\mvc\error; |
| 22 | |
| 23 | use common_Logger; |
| 24 | use Context; |
| 25 | use Exception; |
| 26 | use HTTPToolkit; |
| 27 | use tao_helpers_Request; |
| 28 | use Zend\ServiceManager\ServiceLocatorAwareInterface; |
| 29 | use Zend\ServiceManager\ServiceLocatorAwareTrait; |
| 30 | |
| 31 | /** |
| 32 | * Description of ResponseAbstract |
| 33 | * |
| 34 | * @author Christophe GARCIA <christopheg@taotesting.com> |
| 35 | */ |
| 36 | abstract class ResponseAbstract implements ResponseInterface, ServiceLocatorAwareInterface |
| 37 | { |
| 38 | use ServiceLocatorAwareTrait; |
| 39 | |
| 40 | /** |
| 41 | * http response code |
| 42 | * @var integer |
| 43 | */ |
| 44 | protected $httpCode; |
| 45 | /** |
| 46 | * content type to use into response header |
| 47 | * @var string |
| 48 | */ |
| 49 | protected $contentType = ''; |
| 50 | |
| 51 | /** |
| 52 | * Allowed methods for 405 response |
| 53 | * @var string[] |
| 54 | */ |
| 55 | protected $allowMethodsHeader; |
| 56 | |
| 57 | /** |
| 58 | * @var Exception |
| 59 | */ |
| 60 | protected $exception; |
| 61 | |
| 62 | |
| 63 | protected $rendererClassList = |
| 64 | [ |
| 65 | 'html' => 'HtmlResponse', |
| 66 | 'json' => 'JsonResponse', |
| 67 | 'none' => 'NonAcceptable', |
| 68 | 'ajax' => 'AjaxResponse', |
| 69 | ]; |
| 70 | |
| 71 | |
| 72 | /** |
| 73 | * search rendering method in function of request accept header |
| 74 | * @param array $accept |
| 75 | * @return ResponseAbstract |
| 76 | */ |
| 77 | protected function chooseRenderer(array $accept) |
| 78 | { |
| 79 | $renderClass = 'none'; |
| 80 | foreach ($accept as $mimeType) { |
| 81 | switch (trim(strtolower($mimeType))) { |
| 82 | case 'text/html': |
| 83 | case 'application/xhtml+xml': |
| 84 | case '*/*': |
| 85 | $renderClass = 'html'; |
| 86 | break 2; |
| 87 | case 'application/json': |
| 88 | case 'text/json': |
| 89 | $renderClass = 'json'; |
| 90 | break 2; |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | if (tao_helpers_Request::isAjax()) { |
| 95 | $renderClass = 'ajax'; |
| 96 | } |
| 97 | |
| 98 | $className = __NAMESPACE__ . '\\' . $this->rendererClassList[$renderClass]; |
| 99 | |
| 100 | $renderer = new $className(); |
| 101 | |
| 102 | return $renderer->setServiceLocator($this->getServiceLocator()); |
| 103 | } |
| 104 | /** |
| 105 | * send headers |
| 106 | * @return $this |
| 107 | */ |
| 108 | protected function sendHeaders() |
| 109 | { |
| 110 | $context = Context::getInstance(); |
| 111 | $context->getResponse()->setContentHeader($this->contentType); |
| 112 | |
| 113 | $statusCodeHeader = HTTPToolkit::statusCodeHeader($this->httpCode); |
| 114 | $statusCodeHeader ? header($statusCodeHeader) : http_response_code($this->httpCode); |
| 115 | |
| 116 | if (!empty($this->allowMethodsHeader)) { |
| 117 | header('Allow: ' . implode(', ', $this->allowMethodsHeader)); |
| 118 | } |
| 119 | return $this; |
| 120 | } |
| 121 | /** |
| 122 | * set response http status code |
| 123 | * @param int $code |
| 124 | * @return $this |
| 125 | */ |
| 126 | public function setHttpCode($code) |
| 127 | { |
| 128 | $this->httpCode = $code; |
| 129 | return $this; |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * @param string[]|null $allowedMethods |
| 134 | * @return $this |
| 135 | */ |
| 136 | public function setAllowedMethods($allowedMethods) |
| 137 | { |
| 138 | $this->allowMethodsHeader = $allowedMethods; |
| 139 | return $this; |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * @inherit |
| 144 | */ |
| 145 | public function send() |
| 146 | { |
| 147 | $accept = array_key_exists('HTTP_ACCEPT', $_SERVER) ? explode(',', $_SERVER['HTTP_ACCEPT']) : []; |
| 148 | $renderer = $this->chooseRenderer($accept); |
| 149 | |
| 150 | return $renderer |
| 151 | ->setException($this->exception) |
| 152 | ->setHttpCode($this->httpCode) |
| 153 | ->setAllowedMethods($this->allowMethodsHeader) |
| 154 | ->sendHeaders() |
| 155 | ->send(); |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * @inherit |
| 160 | */ |
| 161 | public function trace() |
| 162 | { |
| 163 | if ($this->exception) { |
| 164 | common_Logger::singleton()->handleException($this->exception); |
| 165 | } |
| 166 | |
| 167 | return $this; |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * set up exception |
| 172 | * @param Exception $exception |
| 173 | * @return $this |
| 174 | */ |
| 175 | public function setException(Exception $exception) |
| 176 | { |
| 177 | $this->exception = $exception; |
| 178 | return $this; |
| 179 | } |
| 180 | } |