<commit_before><?php

namespace PeskyCMF\Db\Traits;

use Illuminate\Routing\Route;
use PeskyCMF\Config\CmfConfig;
use PeskyCMF\Db\CmfDbObject;
use PeskyCMF\HttpCode;

trait InjectsDbObjects {

    public function callAction($method, $parameters) {
        $this->readDbObjectForInjection($parameters);
        return parent::callAction($method, $parameters);
    }

    /**
     * @param $parameters
     */
    protected function readDbObjectForInjection($parameters) {
        /** @var Route $route */
        $route = \Request::route();
        $id = $route->parameter('id', false);
        if ($id === false && \Request::method() !== 'GET') {
             $id = \Request::get('id', false);
        }
        if ($id !== false) {
            if (empty($id)) {
                $this->sendRecordNotFoundResponse();
            }
            $object = null;
            foreach ($parameters as $key => $value) {
                if ($value instanceof CmfDbObject) {
                    // get only last object in params
                    $object = $value;
                }
            }
            if (!empty($object)) {
                $conditions = [
                    'id' => $id,
                ];
                $this->addConditionsForDbObjectInjection($route, $object, $conditions);
                $this->addParentIdsConditionsForDbObjectInjection($route, $object, $conditions);
                $object->find($conditions);
                if (!$object->exists()) {
                    $this->sendRecordNotFoundResponse();
                }
            }
        }
    }

    /**
     * Abort with HTTP code 404
     */
    protected function sendRecordNotFoundResponse() {
        abort(HttpCode::NOT_FOUND, CmfConfig::transBase('.error.db_record_not_exists'));
    }

    /**
     * @param Route $route
     * @param CmfDbObject $object
     * @param array $conditions
     */
    protected function addConditionsForDbObjectInjection(Route $route, CmfDbObject $object, array &$conditions) {

    }

    /**
     * @param Route $route
     * @param CmfDbObject $object
     * @param array $conditions
     */
    protected function addParentIdsConditionsForDbObjectInjection(Route $route, CmfDbObject $object, array &$conditions) {
        foreach ($route->parameterNames() as $name) {
            if ($object->_hasField($name)) {
                $conditions[$name] = $route->parameter($name);
            }
        }<commit_msg>InjectsDbObjects - not throws 404 error when there is an object injection required but id parameter is not passed (instead of ignoring injection if parameter is not passed)<commit_after><?php

namespace PeskyCMF\Db\Traits;

use Illuminate\Routing\Route;
use PeskyCMF\Config\CmfConfig;
use PeskyCMF\Db\CmfDbObject;
use PeskyCMF\HttpCode;

trait InjectsDbObjects {

    public function callAction($method, $parameters) {
        $this->readDbObjectForInjection($parameters);
        return parent::callAction($method, $parameters);
    }

    /**
     * @param $parameters
     */
    protected function readDbObjectForInjection($parameters) {
        /** @var Route $route */
        $route = \Request::route();
        $object = null;
        foreach ($parameters as $key => $value) {
            if ($value instanceof CmfDbObject) {
                // get only last object in params
                $object = $value;
            }
        }
        if (!empty($object)) {
            $id = $route->parameter('id', false);
            if ($id === false && \Request::method() !== 'GET') {
                 $id = \Request::get('id', false);
            }
            if (empty($id)) {
                $this->sendRecordNotFoundResponse();
            }
            $conditions = [
                'id' => $id,
            ];
            $this->addConditionsForDbObjectInjection($route, $object, $conditions);
            $this->addParentIdsConditionsForDbObjectInjection($route, $object, $conditions);
            $object->find($conditions);
            if (!$object->exists()) {
                $this->sendRecordNotFoundResponse();
            }
        }
    }

    /**
     * Abort with HTTP code 404
     */
    protected function sendRecordNotFoundResponse() {
        abort(HttpCode::NOT_FOUND, CmfConfig::transBase('.error.db_record_not_exists'));
    }

    /**
     * @param Route $route
     * @param CmfDbObject $object
     * @param array $conditions
     */
    protected function addConditionsForDbObjectInjection(Route $route, CmfDbObject $object, array &$conditions) {

    }

    /**
     * @param Route $route
     * @param CmfDbObject $object
     * @param array $conditions
     */
    protected function addParentIdsConditionsForDbObjectInjection(Route $route, CmfDbObject $object, array &$conditions) {
        foreach ($route->parameterNames() as $name) {
            if ($object->_hasField($name)) {
                $conditions[$name] = $route->parameter($name);
            }
        }
{'fraction_non_alphanumeric': 0.1294949494949495, 'fraction_numerical': 0.0018181818181818182, 'mean_word_length': 2.0885839051777917, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 29, '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': '5097333', 'n_tokens_mistral': 1370, 'n_tokens_neox': 1327, 'n_words': 291}