| Total Complexity | 6 |
| Total Lines | 66 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 1 | <?php |
||
| 18 | class CORS implements Policy |
||
| 19 | { |
||
| 20 | /** |
||
| 21 | * @var string |
||
| 22 | */ |
||
| 23 | private $prefix = null; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @var Rule |
||
| 27 | */ |
||
| 28 | private $rule = null; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * CORS constructor. |
||
| 32 | * @param string $prefix |
||
| 33 | * @param Rule $rule |
||
| 34 | */ |
||
| 35 | public function __construct(string $prefix, Rule $rule) |
||
| 36 | { |
||
| 37 | $this->prefix = $prefix; |
||
| 38 | $this->rule = $rule; |
||
| 39 | } |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @param string $uri |
||
| 43 | * @return bool |
||
| 44 | */ |
||
| 45 | public function allowed(string $uri) : bool |
||
| 46 | { |
||
| 47 | return substr($uri, 0, strlen($this->prefix)) === $this->prefix; |
||
| 48 | } |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @param Context $ctx |
||
| 52 | * @param ServerRequest $sr |
||
| 53 | * @param int $route |
||
| 54 | * @param callable $handler |
||
| 55 | * @param array $params |
||
| 56 | * @return Response|null |
||
| 57 | */ |
||
| 58 | public function request( |
||
| 59 | Context $ctx, |
||
| 60 | ServerRequest $sr, |
||
| 61 | int $route, |
||
| 62 | callable $handler = null, |
||
| 63 | array $params = [] |
||
| 64 | ) : ?Response { |
||
| 65 | if ($route === Dispatcher::FOUND) { |
||
| 66 | return null; |
||
| 67 | } |
||
| 68 | |||
| 69 | if ($sr->getMethod() !== 'OPTIONS') { |
||
| 70 | return null; |
||
| 71 | } |
||
| 72 | |||
| 73 | return $this->response($sr, new Response); |
||
| 74 | } |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @param ServerRequest $sr |
||
| 78 | * @param Response $respond |
||
| 79 | * @return Response |
||
| 80 | */ |
||
| 81 | public function response(ServerRequest $sr, Response $respond) : Response |
||
| 84 | } |
||
| 85 | } |
||
| 86 |