Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
50.00% |
5 / 10 |
|
42.86% |
3 / 7 |
CRAP | |
0.00% |
0 / 1 |
| Controller | |
50.00% |
5 / 10 |
|
42.86% |
3 / 7 |
13.12 | |
0.00% |
0 / 1 |
| getPsrContainer | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| setRequest | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| setResponse | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| getPsrRequest | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getPsrResponse | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| setCookie | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| setContentHeader | |
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) 2019 (original work) Open Assessment Technologies SA |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | namespace oat\tao\model\http; |
| 23 | |
| 24 | use Psr\Container\ContainerInterface; |
| 25 | use Psr\Http\Message\ResponseInterface; |
| 26 | use Psr\Http\Message\ServerRequestInterface; |
| 27 | |
| 28 | /** |
| 29 | * Class Controller |
| 30 | * |
| 31 | * A controller to handle http request & response following Psr7 standard |
| 32 | * |
| 33 | * @package oat\tao\model\http |
| 34 | * @author Moyon Camille |
| 35 | * |
| 36 | * @deprecated Controllers shall be decoupled, using composition and Middlewares, not inheritance. |
| 37 | * They can be used with DI services (including PSR Request and Response) which will be autowired. |
| 38 | * More information here: |
| 39 | * - https://github.com/oat-sa/generis/tree/master/core/DependencyInjection |
| 40 | * - https://github.com/oat-sa/tao-core/tree/master/models/classes/routing |
| 41 | */ |
| 42 | abstract class Controller |
| 43 | { |
| 44 | use HttpRequestHelperTrait; |
| 45 | use HttpFlowTrait; |
| 46 | |
| 47 | /** @var ServerRequestInterface */ |
| 48 | protected $request; |
| 49 | |
| 50 | /** @var ResponseInterface */ |
| 51 | protected $response; |
| 52 | |
| 53 | public function getPsrContainer(): ContainerInterface |
| 54 | { |
| 55 | return $this->getServiceLocator()->getContainer(); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Set Psr7 http request |
| 60 | * |
| 61 | * @param ServerRequestInterface $request |
| 62 | * @return $this |
| 63 | */ |
| 64 | public function setRequest(ServerRequestInterface $request) |
| 65 | { |
| 66 | $this->request = $request; |
| 67 | return $this; |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Set Psr7 http response |
| 72 | * |
| 73 | * @param ResponseInterface $response |
| 74 | * @return $this |
| 75 | */ |
| 76 | public function setResponse(ResponseInterface $response) |
| 77 | { |
| 78 | $this->response = $response; |
| 79 | return $this; |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Get the Psr7 request |
| 84 | * |
| 85 | * @return ServerRequestInterface |
| 86 | */ |
| 87 | protected function getPsrRequest() |
| 88 | { |
| 89 | return $this->request; |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Get the Psr7 response |
| 94 | * |
| 95 | * @return ResponseInterface |
| 96 | */ |
| 97 | public function getPsrResponse() |
| 98 | { |
| 99 | return $this->response; |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Set cookie by setting the HTTP response header "set-cookie" |
| 104 | * |
| 105 | * @param $name |
| 106 | * @param null $value |
| 107 | * @param null $expire |
| 108 | * @param null $domainPath |
| 109 | * @param null $https |
| 110 | * @param null $httpOnly |
| 111 | * @return bool |
| 112 | */ |
| 113 | protected function setCookie( |
| 114 | $name, |
| 115 | $value = null, |
| 116 | $expire = null, |
| 117 | $domainPath = null, |
| 118 | $https = null, |
| 119 | $httpOnly = null |
| 120 | ) { |
| 121 | return setcookie($name, $value, $expire, $domainPath, $https, $httpOnly); |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Set content-type by setting the HTTP response header "content-type" |
| 126 | * |
| 127 | * @param $contentType |
| 128 | * @param string $charset |
| 129 | * @return $this |
| 130 | */ |
| 131 | protected function setContentHeader($contentType, $charset = 'UTF-8') |
| 132 | { |
| 133 | $this->response = $this->getPsrResponse()->withHeader('content-type', $contentType . ';' . $charset); |
| 134 | return $this; |
| 135 | } |
| 136 | } |