Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
5 / 5
CRAP
100.00% covered (success)
100.00%
23 / 23
OptionsRequestHandler
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
5 / 5
9
100.00% covered (success)
100.00%
23 / 23
 handleRequest
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
3 / 3
 getOptions
100.00% covered (success)
100.00%
1 / 1
3
100.00% covered (success)
100.00%
7 / 7
 getHandlerRoutes
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
6 / 6
 getMatchingRoutes
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
2 / 2
 getImplementedRequestMethods
100.00% covered (success)
100.00%
1 / 1
3
100.00% covered (success)
100.00%
5 / 5
<?php declare(strict_types=1);
/**
 * Copyright (c) 2017 Holger Woltersdorf & Contributors
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 */
namespace IceHawk\IceHawk\RequestHandlers;
use IceHawk\IceHawk\Constants\HandlerMethodInterfaceMap;
use IceHawk\IceHawk\Interfaces\HandlesRequest;
use IceHawk\IceHawk\Responses\Options;
use IceHawk\IceHawk\Routing\Interfaces\RoutesToHandler;
use IceHawk\IceHawk\Routing\OptionsRouter;
/**
 * Class OptionsRequestHandler
 * @package IceHawk\IceHawk\RequestHandlers
 */
final class OptionsRequestHandler extends AbstractRequestHandler
{
    public function handleRequest()
    {
        $allowedRequestMethods = $this->getOptions();
        (new Options( $allowedRequestMethods ))->respond();
    }
    private function getOptions() : array
    {
        $handlerRoutes = $this->getHandlerRoutes();
        $requestMethods = [];
        foreach ( $handlerRoutes as $handlerRoute )
        {
            $handler = $handlerRoute->getRequestHandler();
            foreach ( $this->getImplementedRequestMethods( $handler ) as $requestMethod )
            {
                $requestMethods[] = $requestMethod;
            }
        }
        return $requestMethods;
    }
    private function getHandlerRoutes() : array
    {
        $readRoutes  = $this->config->getReadRoutes();
        $writeRoutes = $this->config->getWriteRoutes();
        $uri         = $this->requestInfo->getUri();
        $matchingReadRoutes  = $this->getMatchingRoutes( $uri, $readRoutes );
        $matchingWriteRoutes = $this->getMatchingRoutes( $uri, $writeRoutes );
        return array_merge( $matchingReadRoutes, $matchingWriteRoutes );
    }
    /**
     * @param string             $uri
     * @param array|\Traversable $routes
     *
     * @return array|RoutesToHandler[]
     * @throws \IceHawk\IceHawk\Routing\Exceptions\RoutesAreNotTraversable
     */
    private function getMatchingRoutes( string $uri, $routes ) : array
    {
        $router = new OptionsRouter( $routes );
        return $router->findMatchingRoutes( $uri );
    }
    private function getImplementedRequestMethods( HandlesRequest $handler ) : array
    {
        $requestMethods = [];
        foreach ( HandlerMethodInterfaceMap::HTTP_METHODS as $requestMethod => $interface )
        {
            if ( $handler instanceof $interface )
            {
                $requestMethods[] = $requestMethod;
            }
        }
        return $requestMethods;
    }
}