<commit_before><?php
/*
 * Fusio
 * A web-application to create dynamically RESTful APIs
 *
 * Copyright (C) 2015-2017 <NAME> <<EMAIL>>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

namespace Fusio\Impl\Table\Routes;

use PSX\Api\Resource;
use PSX\Sql\TableAbstract;

/**
 * Method
 *
 * @author  <NAME> <<EMAIL>>
 * @license http://www.gnu.org/licenses/agpl-3.0
 * @link    http://fusio-project.org
 */
class Method extends TableAbstract
{
    public function getName()
    {
        return 'fusio_routes_method';
    }

    public function getColumns()
    {
        return array(
            'id' => self::TYPE_INT | self::AUTO_INCREMENT | self::PRIMARY_KEY,
            'routeId' => self::TYPE_INT,
            'method' => self::TYPE_VARCHAR,
            'version' => self::TYPE_INT,
            'status' => self::TYPE_INT,
            'active' => self::TYPE_INT,
            'public' => self::TYPE_INT,
            'parameters' => self::TYPE_INT,
            'request' => self::TYPE_INT,
            'action' => self::TYPE_INT,
            'schemaCache' => self::TYPE_TEXT,
            'actionCache' => self::TYPE_TEXT,
        );
    }

    public function deleteAllFromRoute($routeId, $version = null, $status = null)
    {
        $sql = 'DELETE FROM fusio_routes_method
                      WHERE routeId = :id';

        $params = ['id' => $routeId];

        if ($version !== null) {
            $sql.= ' AND version = :version';
            $params['version'] = $version;
        }

        if ($status !== null) {
            $sql.= ' AND status = :status';
            $params['status'] = $status;
        }

        $this->connection->executeQuery($sql, $params);
    }

    public function hasSchema($schemaId)
    {
        $sql = '    SELECT COUNT(method.id) AS cnt
                      FROM fusio_routes_method method
                INNER JOIN fusio_routes routes
                        ON routes.id = method.routeId
                     WHERE routes.status = 1
                       AND (method.parameters = :parameters OR method.request = :request)';

        $count = $this->connection->fetchColumn($sql, [
            'parameters' => $schemaId,
            'request'    => $schemaId,
        ]);

        return $count > 0;
    }

    public function hasAction($actionId)
    {
        $sql = '    SELECT COUNT(method.id) AS cnt
                      FROM fusio_routes_method method
                INNER JOIN fusio_routes routes
                        ON routes.id = method.routeId
                     WHERE routes.status = 1
                       AND method.action = :actionId';

        $count = $this->connection->fetchColumn($sql, [<commit_msg>improve hasSchema and add phpdoc<commit_after><?php
/*
 * Fusio
 * A web-application to create dynamically RESTful APIs
 *
 * Copyright (C) 2015-2017 <NAME> <<EMAIL>>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

namespace Fusio\Impl\Table\Routes;

use PSX\Api\Resource;
use PSX\Sql\TableAbstract;

/**
 * Method
 *
 * @author  <NAME> <<EMAIL>>
 * @license http://www.gnu.org/licenses/agpl-3.0
 * @link    http://fusio-project.org
 */
class Method extends TableAbstract
{
    public function getName()
    {
        return 'fusio_routes_method';
    }

    public function getColumns()
    {
        return array(
            'id' => self::TYPE_INT | self::AUTO_INCREMENT | self::PRIMARY_KEY,
            'routeId' => self::TYPE_INT,
            'method' => self::TYPE_VARCHAR,
            'version' => self::TYPE_INT,
            'status' => self::TYPE_INT,
            'active' => self::TYPE_INT,
            'public' => self::TYPE_INT,
            'parameters' => self::TYPE_INT,
            'request' => self::TYPE_INT,
            'action' => self::TYPE_INT,
            'schemaCache' => self::TYPE_TEXT,
            'actionCache' => self::TYPE_TEXT,
        );
    }

    public function deleteAllFromRoute($routeId, $version = null, $status = null)
    {
        $sql = 'DELETE FROM fusio_routes_method
                      WHERE routeId = :id';

        $params = ['id' => $routeId];

        if ($version !== null) {
            $sql.= ' AND version = :version';
            $params['version'] = $version;
        }

        if ($status !== null) {
            $sql.= ' AND status = :status';
            $params['status'] = $status;
        }

        $this->connection->executeQuery($sql, $params);
    }

    /**
     * Returns whether a schema id is in use by a route. In the worst case this
     * gets reported by the database constraints but we use this method to 
     * report a proper error message
     * 
     * @param integer $schemaId
     * @return boolean
     */
    public function hasSchema($schemaId)
    {
        $sql = '    SELECT COUNT(resp.id) AS cnt
                      FROM fusio_routes_response resp
                INNER JOIN fusio_routes_method method
                        ON resp.methodId = method.id
                INNER JOIN fusio_routes routes
                        ON routes.id = method.routeId
                     WHERE routes.status = 1
                       AND (method.parameters = :schemaId OR method.request = :schemaId OR resp.response = :schemaId)';

        $count = $this->connection->fetchColumn($sql, [
            'schemaId' => $schemaId,
        ]);

        return $count > 0;
    }

    /**
     * Returns whether a action id is in use by a route. In the worst case this
     * gets reported by the database constraints but we use this method to
     * report a proper error message
     *
     * @param integer $actionId
     * @return boolean
     */
    public function hasAction($actionId)
    {
        $sql = '    SELECT COUNT(method.id) AS cnt
                      FROM fusio_routes_method method
                INNER JOIN fusio_routes routes
                        ON routes.id = method.routeId
                     WHERE routes.status = 1
                       AND method.action = :actionId';

        $count = $this->connection->fetchColumn($sql, [
{'fraction_non_alphanumeric': 0.10860853945004836, 'fraction_numerical': 0.0038690064944037583, 'mean_word_length': 2.0008291873963517, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 60, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, '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': '7630695', 'n_tokens_mistral': 2042, 'n_tokens_neox': 1968, 'n_words': 620}