Code Coverage  | 
      ||||||||||
Lines  | 
       Functions and Methods  | 
       Classes and Traits  | 
      ||||||||
| Total |         | 
       60.00%  | 
       24 / 40  | 
               | 
       66.67%  | 
       4 / 6  | 
       CRAP |         | 
       0.00%  | 
       0 / 1  | 
      
| ExceptionInterpretor |         | 
       60.00%  | 
       24 / 40  | 
               | 
       66.67%  | 
       4 / 6  | 
       52.98 |         | 
       0.00%  | 
       0 / 1  | 
      
| setException |         | 
       100.00%  | 
       3 / 3  | 
               | 
       100.00%  | 
       1 / 1  | 
       1 | |||
| interpretError |         | 
       69.23%  | 
       18 / 26  | 
               | 
       0.00%  | 
       0 / 1  | 
       23.46 | |||
| getTrace |         | 
       100.00%  | 
       1 / 1  | 
               | 
       100.00%  | 
       1 / 1  | 
       2 | |||
| getHttpCode |         | 
       100.00%  | 
       1 / 1  | 
               | 
       100.00%  | 
       1 / 1  | 
       1 | |||
| getResponseClassName |         | 
       100.00%  | 
       1 / 1  | 
               | 
       100.00%  | 
       1 / 1  | 
       1 | |||
| getResponse |         | 
       0.00%  | 
       0 / 8  | 
               | 
       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_exception_MethodNotAllowed; | 
| 24 | use common_exception_RestApi; | 
| 25 | use common_exception_ValidationFailed; | 
| 26 | use Exception; | 
| 27 | use common_exception_MissingParameter; | 
| 28 | use common_exception_BadRequest; | 
| 29 | use common_exception_ResourceNotFound; | 
| 30 | use Slim\Http\StatusCode; | 
| 31 | use tao_models_classes_MissingRequestParameterException; | 
| 32 | use Zend\ServiceManager\ServiceLocatorAwareInterface; | 
| 33 | use Zend\ServiceManager\ServiceLocatorAwareTrait; | 
| 34 | use oat\tao\model\exceptions\UserErrorException; | 
| 35 | |
| 36 | /** | 
| 37 | * Description of ExceptionInterpretor | 
| 38 | * | 
| 39 | * @author Christophe GARCIA <christopheg@taotesting.com> | 
| 40 | */ | 
| 41 | class ExceptionInterpretor implements ServiceLocatorAwareInterface | 
| 42 | { | 
| 43 | use ServiceLocatorAwareTrait; | 
| 44 | |
| 45 | /** | 
| 46 | * | 
| 47 | * @var Exception | 
| 48 | */ | 
| 49 | protected $exception; | 
| 50 | /** | 
| 51 | * | 
| 52 | * @var integer | 
| 53 | */ | 
| 54 | protected $returnHttpCode; | 
| 55 | |
| 56 | /** | 
| 57 | * @var string[]|null | 
| 58 | */ | 
| 59 | protected $allowedRequestMethods; | 
| 60 | |
| 61 | /** | 
| 62 | * | 
| 63 | * @var string | 
| 64 | */ | 
| 65 | protected $responseClassName; | 
| 66 | |
| 67 | /** | 
| 68 | * | 
| 69 | * @var string | 
| 70 | */ | 
| 71 | protected $trace = ''; | 
| 72 | |
| 73 | /** | 
| 74 | * set exception to interpet | 
| 75 | * @param Exception $exception | 
| 76 | * @return ExceptionInterpretor | 
| 77 | */ | 
| 78 | public function setException(Exception $exception) | 
| 79 | { | 
| 80 | $this->exception = $exception; | 
| 81 | $this->interpretError(); | 
| 82 | return $this; | 
| 83 | } | 
| 84 | /** | 
| 85 | * interpret exception type and set up render responseClassName | 
| 86 | * and http status to return | 
| 87 | */ | 
| 88 | protected function interpretError() | 
| 89 | { | 
| 90 | $this->responseClassName = 'MainResponse'; | 
| 91 | |
| 92 | if ($this->exception instanceof common_exception_RestApi) { | 
| 93 | $this->returnHttpCode = $this->exception->getCode() ?: StatusCode::HTTP_BAD_REQUEST; | 
| 94 | |
| 95 | return $this; | 
| 96 | } | 
| 97 | |
| 98 | switch (get_class($this->exception)) { | 
| 99 | case UserErrorException::class: | 
| 100 | case tao_models_classes_MissingRequestParameterException::class: | 
| 101 | case common_exception_MissingParameter::class: | 
| 102 | case common_exception_BadRequest::class: | 
| 103 | case common_exception_ValidationFailed::class: | 
| 104 | $this->returnHttpCode = StatusCode::HTTP_BAD_REQUEST; | 
| 105 | break; | 
| 106 | case 'tao_models_classes_AccessDeniedException': | 
| 107 | case 'ResolverException': | 
| 108 | $this->returnHttpCode = StatusCode::HTTP_FORBIDDEN; | 
| 109 | $this->responseClassName = 'RedirectResponse'; | 
| 110 | break; | 
| 111 | case 'tao_models_classes_UserException': | 
| 112 | $this->returnHttpCode = StatusCode::HTTP_FORBIDDEN; | 
| 113 | break; | 
| 114 | case 'ActionEnforcingException': | 
| 115 | case 'tao_models_classes_FileNotFoundException': | 
| 116 | case common_exception_ResourceNotFound::class: | 
| 117 | $this->returnHttpCode = StatusCode::HTTP_NOT_FOUND; | 
| 118 | break; | 
| 119 | case common_exception_MethodNotAllowed::class: | 
| 120 | $this->returnHttpCode = StatusCode::HTTP_METHOD_NOT_ALLOWED; | 
| 121 | /** @var common_exception_MethodNotAllowed $exception */ | 
| 122 | $exception = $this->exception; | 
| 123 | $this->allowedRequestMethods = $exception->getAllowedMethods(); | 
| 124 | break; | 
| 125 | default: | 
| 126 | $this->returnHttpCode = StatusCode::HTTP_INTERNAL_SERVER_ERROR; | 
| 127 | break; | 
| 128 | } | 
| 129 | return $this; | 
| 130 | } | 
| 131 | |
| 132 | public function getTrace() | 
| 133 | { | 
| 134 | return $this->exception ? $this->exception->getMessage() : ''; | 
| 135 | } | 
| 136 | |
| 137 | /** | 
| 138 | * @return integer | 
| 139 | */ | 
| 140 | public function getHttpCode() | 
| 141 | { | 
| 142 | return $this->returnHttpCode; | 
| 143 | } | 
| 144 | /** | 
| 145 | * return string | 
| 146 | */ | 
| 147 | public function getResponseClassName() | 
| 148 | { | 
| 149 | return __NAMESPACE__ . '\\' . $this->responseClassName; | 
| 150 | } | 
| 151 | |
| 152 | /** | 
| 153 | * return an instance of ResponseInterface | 
| 154 | * | 
| 155 | * @return ResponseAbstract | 
| 156 | */ | 
| 157 | public function getResponse() | 
| 158 | { | 
| 159 | $class = $this->getResponseClassName(); | 
| 160 | /** @var $response ResponseAbstract */ | 
| 161 | $response = new $class(); | 
| 162 | $response->setServiceLocator($this->getServiceLocator()); | 
| 163 | $response->setException($this->exception) | 
| 164 | ->setHttpCode($this->returnHttpCode) | 
| 165 | ->setAllowedMethods($this->allowedRequestMethods) | 
| 166 | ->trace(); | 
| 167 | |
| 168 | return $response; | 
| 169 | } | 
| 170 | } |