<commit_before><?php
namespace Graviton\RestBundle\Routing\Loader;

use Symfony\Component\Routing\Route;

/**
 * Generate routes for individual actions
 *
 * @category GravitonRestBundle
 * @package  Graviton
 * @author   <NAME> <<EMAIL>>
 * @author   <NAME> <<EMAIL>>
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
 * @link     http://swisscom.com
 */
class ActionFactory
{
    /**
     * Get route for GET requests
     *
     * @param String $service service id
     *
     * @return Route
     */
    public static function getRouteGet($service)
    {
        $pattern = '/'.static::getBaseFromService($service).'/{id}';
        $defaults = array(
            '_controller' => $service.':getAction',
            '_format' => '~',
        );

        $requirements = array(
            'id' => '\w+',
            '_method' => 'GET',
        );

        $route = new Route($pattern, $defaults, $requirements);

        return $route;
    }

    /**
     * Get route for getAll requests
     *
     * @param String $service service id
     *
     * @return Route
     */
    public static function getRouteAll($service)
    {
        $pattern = '/'.static::getBaseFromService($service);
        $defaults = array(
            '_controller' => $service.':allAction',
            '_format' => '~'
        );

        $requirements = array(
            '_method' => 'GET'
        );

        $route = new Route($pattern, $defaults, $requirements);

        return $route;
    }

    /**
     * Get route for POST requests
     *
     * @param String $service service id
     *
     * @return Route
     */
    public static function getRoutePost($service)
    {
        $pattern = '/'.static::getBaseFromService($service);
        $defaults = array(
            '_controller' => $service.':postAction',
            '_format' => '~'
        );

        $requirements = array(
            '_method' => 'POST'
        );

        $route = new Route($pattern, $defaults, $requirements);

        return $route;
    }

    /**
     * Get route for PUT requests
     *
     * @param String $service service id
     *
     * @return Route
     */
    public static function getRoutePut($service)
    {
        $pattern = '/'.static::getBaseFromService($service).'/{id}';
        $defaults = array(
            '_controller' => $service.':putAction',
            '_format' => '~'
        );

        $requirements = array(
            'id' => '\w+',
            '_method' => 'PUT'
        );

        $route = new Route($pattern, $defaults, $requirements);

        return $route;
    }

    /**
     * Get route for DELETE requests
     *
     * @param String $service service id
     *
     * @return Route
     */
    public static function getRouteDelete($service)
    {
        $pattern = '/'.static::getBaseFromService($service).'/{id}';
        $defaults = array(
            '_controller' => $service.':deleteAction',
            '_format' => '~'
        );

        $requirements = array(
            'id' => '\w+',
            '_method' => 'DELETE'
        );

        $route = new Route($pattern, $defaults, $requirements);

        return $route;
    }

    /**
     * Get entity name from service strings.
     *
     * By convention the last part of the service string so far
     * makes up the entities name.
     *
     * @param String $service (partial) service id
     *
     * @return String
     */
    private static function getBaseFromService($service)
    {
        $parts = explode('.', $service);

        $entity = array_pop($parts);
        $module = $parts[1];

        return '/'.$module.'/'.$entity;
    }
}<commit_msg>refactor ActionFactory<commit_after><?php
namespace Graviton\RestBundle\Routing\Loader;

use Symfony\Component\Routing\Route;

/**
 * Generate routes for individual actions
 *
 * @category GravitonRestBundle
 * @package  Graviton
 * @author   <NAME> <<EMAIL>>
 * @author   <NAME> <<EMAIL>>
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
 * @link     http://swisscom.com
 */
class ActionFactory
{
    /**
     * Get route for GET requests
     *
     * @param String $service service id
     *
     * @return Route
     */
    public static function getRouteGet($service)
    {
        return self::getRoute($service, 'GET', 'getAction', array('id' => '\w+'));
    }

    /**
     * Get route for getAll requests
     *
     * @param String $service service id
     *
     * @return Route
     */
    public static function getRouteAll($service)
    {
        return self::getRoute($service, 'GET', 'allAction');
    }

    /**
     * Get route for POST requests
     *
     * @param String $service service id
     *
     * @return Route
     */
    public static function getRoutePost($service)
    {
        return self::getRoute($service, 'POST', 'postAction');
    }

    /**
     * Get route for PUT requests
     *
     * @param String $service service id
     *
     * @return Route
     */
    public static function getRoutePut($service)
    {
        return self::getRoute($service, 'PUT', 'putAction', array('id' => '\w+'));
    }

    /**
     * Get route for DELETE requests
     *
     * @param String $service service id
     *
     * @return Route
     */
    public static function getRouteDelete($service)
    {
        return self::getRoute($service, 'DELETE', 'deleteAction', array('id' => '\w+'));
    }

    /**
     * Get entity name from service strings.
     *
     * By convention the last part of the service string so far
     * makes up the entities name.
     *
     * @param String $service (partial) service id
     *
     * @return String
     */
    private static function getBaseFromService($service)
    {
        $parts = explode('.', $service);

        $entity = array_pop($parts);
        $module = $parts[1];

        return '/'.$module.'/'.$entity;
    }

    /**
     * Get Route
     *
     * @return Route
     */
    static private function getRoute($service, $method, $action, $parameters = array())
    {
        $pattern = '/'.static::getBaseFromService($service);
        $defaults = array(
            '_controller' => $service.':'.$action,
            '_format' => '~',
        );

        $requirements = array(
            '_method' => $method,
        );

        foreach ($parameters as $paramName => $paramPattern) {
            $pattern .= '/{'.$paramName.'}';
            $requirements[$paramName] = $paramPattern;
        }

        $route = new Route($pattern, $defaults, $requirements);

        return $route;
    }
}
{'fraction_non_alphanumeric': 0.14454904342544792, 'fraction_numerical': 0.0003036744609778318, 'mean_word_length': 1.9577907498877414, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 40, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6611036', 'n_tokens_mistral': 1897, 'n_tokens_neox': 1859, 'n_words': 466}