Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
94.74% |
36 / 38 |
|
92.31% |
12 / 13 |
CRAP | |
0.00% |
0 / 1 |
| MiddlewareMap | |
94.74% |
36 / 38 |
|
92.31% |
12 / 13 |
22.07 | |
0.00% |
0 / 1 |
| byRoute | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| byRoutes | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| byMiddlewareId | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| byMiddlewareIds | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| andMiddlewareId | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| andHttpMethod | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| andRoute | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| getRoutes | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getMiddlewaresIds | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getHttpMethods | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| jsonSerialize | |
77.78% |
7 / 9 |
|
0.00% |
0 / 1 |
3.10 | |||
| fromJson | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
7 | |||
| __construct | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| 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) 2022 (original work) Open Assessment Technologies SA; |
| 19 | * |
| 20 | * @author Gabriel Felipe Soares <gabriel.felipe.soares@taotesting.com> |
| 21 | */ |
| 22 | |
| 23 | declare(strict_types=1); |
| 24 | |
| 25 | namespace oat\generis\model\Middleware; |
| 26 | |
| 27 | use LogicException; |
| 28 | |
| 29 | final class MiddlewareMap implements MiddlewareMapInterface |
| 30 | { |
| 31 | private const OPTION_MIDDLEWARES = 'middlewares'; |
| 32 | private const OPTION_ROUTES = 'routes'; |
| 33 | private const OPTION_HTTP_METHODS = 'httpMethods'; |
| 34 | |
| 35 | /** @var string[] */ |
| 36 | private $routes = []; |
| 37 | |
| 38 | /** @var string[] */ |
| 39 | private $middlewaresIds = []; |
| 40 | |
| 41 | /** @var string[]|null */ |
| 42 | private $httpMethods = []; |
| 43 | |
| 44 | public static function byRoute(string $route): self |
| 45 | { |
| 46 | return new self([$route]); |
| 47 | } |
| 48 | |
| 49 | public static function byRoutes(array $route): self |
| 50 | { |
| 51 | return new self($route); |
| 52 | } |
| 53 | |
| 54 | public static function byMiddlewareId(string $middlewareId): self |
| 55 | { |
| 56 | return new self([], [$middlewareId]); |
| 57 | } |
| 58 | |
| 59 | public static function byMiddlewareIds(array $middlewareIds): self |
| 60 | { |
| 61 | return new self([], $middlewareIds); |
| 62 | } |
| 63 | |
| 64 | public function andMiddlewareId(string $middlewareId): self |
| 65 | { |
| 66 | $this->middlewaresIds[] = $middlewareId; |
| 67 | |
| 68 | return $this; |
| 69 | } |
| 70 | |
| 71 | public function andHttpMethod(string $httpMethod): self |
| 72 | { |
| 73 | if (!in_array($httpMethod, ['POST', 'PUT', 'GET', 'DELETE', 'PATCH'], true)) { |
| 74 | throw new LogicException(sprintf('Invalid HTTP method "%s" for middleware map', $httpMethod)); |
| 75 | } |
| 76 | |
| 77 | $this->httpMethods[] = $httpMethod; |
| 78 | |
| 79 | return $this; |
| 80 | } |
| 81 | |
| 82 | public function andRoute(string $route): self |
| 83 | { |
| 84 | $this->routes[] = $route; |
| 85 | |
| 86 | return $this; |
| 87 | } |
| 88 | |
| 89 | public function getRoutes(): array |
| 90 | { |
| 91 | return $this->routes; |
| 92 | } |
| 93 | |
| 94 | public function getMiddlewaresIds(): array |
| 95 | { |
| 96 | return $this->middlewaresIds; |
| 97 | } |
| 98 | |
| 99 | public function getHttpMethods(): ?array |
| 100 | { |
| 101 | return $this->httpMethods; |
| 102 | } |
| 103 | |
| 104 | public function jsonSerialize(): array |
| 105 | { |
| 106 | if (empty($this->routes)) { |
| 107 | throw new LogicException('A middleware map should have at least one route'); |
| 108 | } |
| 109 | |
| 110 | if (empty($this->middlewaresIds)) { |
| 111 | throw new LogicException('A middleware map should have at least one middlewareId'); |
| 112 | } |
| 113 | |
| 114 | return [ |
| 115 | self::OPTION_ROUTES => $this->routes, |
| 116 | self::OPTION_HTTP_METHODS => $this->httpMethods, |
| 117 | self::OPTION_MIDDLEWARES => $this->middlewaresIds, |
| 118 | ]; |
| 119 | } |
| 120 | |
| 121 | public static function fromJson(array $json): MiddlewareMapInterface |
| 122 | { |
| 123 | if ( |
| 124 | empty($json[self::OPTION_ROUTES]) || |
| 125 | empty($json[self::OPTION_MIDDLEWARES]) || |
| 126 | !is_array($json[self::OPTION_ROUTES]) || |
| 127 | !is_array($json[self::OPTION_MIDDLEWARES]) || |
| 128 | (!empty($json[self::OPTION_HTTP_METHODS]) && !is_array($json[self::OPTION_HTTP_METHODS])) |
| 129 | ) { |
| 130 | throw new LogicException(sprintf('Wrong middleware json: %s', json_encode($json))); |
| 131 | } |
| 132 | |
| 133 | return new self( |
| 134 | $json[self::OPTION_ROUTES], |
| 135 | $json[self::OPTION_MIDDLEWARES], |
| 136 | $json[self::OPTION_HTTP_METHODS] ?? [] |
| 137 | ); |
| 138 | } |
| 139 | |
| 140 | private function __construct(array $routes = [], array $middlewaresIds = [], array $httpMethods = []) |
| 141 | { |
| 142 | $this->routes = $routes; |
| 143 | $this->middlewaresIds = $middlewaresIds; |
| 144 | $this->httpMethods = $httpMethods; |
| 145 | } |
| 146 | } |