<commit_before><?php

/**
 * @link http://www.digitaldeals.cz/
 * @copyright Copyright (c) 2016 Digital Deals s.r.o.
 * @license http://www.digitaldeals.cz/license/
 * @author <NAME> <<EMAIL>>
 */

namespace dlds\giixer\components\handlers;

use dlds\giixer\components\events\GxEvent;

/**
 * This is basic handler class used 
 * for invoking validation on appropriate model class and
 * based on validation result call appropriate callback
 * ---
 * This is useful for application controllers to keep its methods lean
 * and well-arranged.
 * ---
 * @see http://www.yiiframework.com/doc-2.0/guide-concept-events.html
 */
abstract class GxHandler extends \yii\base\Component
{

    // AFTER events
    const EVENT_AFTER_PROCESS = 'e_after_process';
    // BEFORE events
    const EVENT_BEFORE_PROCESS = 'e_before_process';
    const EVENT_BEFORE_LOAD = 'e_before_load';

    /**
     * @var string ActiveRecord classname
     */
    protected $model;

    /**
     * Initializes CRUD handler
     * @throws \yii\base\ErrorException
     */
    public function __construct($model = false)
    {
        if (!$model) {
            $model = $this->modelClass();
        }

        $this->model = $model;

        if (!is_subclass_of($this->model, \yii\base\Model::className())) {
            throw new \yii\base\ErrorException('Invalid model class. Method "getModelClass" has to retrieve Model descendant class.');
        }
    }

    /**
     * Process handler
     * ---
     * Processes through GxEvent which holds all data about action
     * @param array $attrs given attributes
     * @param string $scope given scope for massive assignment
     * @return instance|null
     */
    public function process(array $attrs, $scope = null)
    {
        $event = new GxEvent(['input' => $attrs]);

        $this->trigger(self::EVENT_BEFORE_PROCESS, $event);

        $this->processModel($event);

        $this->trigger(self::EVENT_AFTER_PROCESS, $event);

        return $event;
    }

    /**
     * Process model validation and run callback based on validation result
     * @param \yii\db\ActiveRecord $model given model to be changed
     * @param array $attrs given attributes
     * @return mixed
     */
    protected function processModel(GxEvent &$event, $scope = null)
    {
        $class = $this->model;

        // instantiate new class
        $event->model = new $class;

        $this->trigger(self::EVENT_BEFORE_LOAD, $event);

        // load data into model
        if ($event->model && ($event->model->load($event->input, $scope) || $event->isPushed())) {
            // if model is valid run validCallback otherwise notValidCallback
            if ($event->model->validate()) {
                $this->validCallback($event);
            } else {
                $this->notValidCallback($event);
            }
        }
    }

    /**
     * Retrieves model class, which must be \yii\base\Model class descendant
     * @return string model class
     */
    abstract protected function modelClass();

    /**
     * Processes when loaded model is valid
     */
    abstract protected function validCallback(GxEvent $event);

    /**
     * Processes when loaded model is not valid
     */
    abstract protected function notValidCallback(GxEvent $event);
}<commit_msg>+ GxHandler possibility to instantiate custom class<commit_after><?php

/**
 * @link http://www.digitaldeals.cz/
 * @copyright Copyright (c) 2016 Digital Deals s.r.o.
 * @license http://www.digitaldeals.cz/license/
 * @author <NAME> <<EMAIL>>
 */

namespace dlds\giixer\components\handlers;

use dlds\giixer\components\events\GxEvent;

/**
 * This is basic handler class used 
 * for invoking validation on appropriate model class and
 * based on validation result call appropriate callback
 * ---
 * This is useful for application controllers to keep its methods lean
 * and well-arranged.
 * ---
 * @see http://www.yiiframework.com/doc-2.0/guide-concept-events.html
 */
abstract class GxHandler extends \yii\base\Component
{

    // AFTER events
    const EVENT_AFTER_PROCESS = 'e_after_process';
    // BEFORE events
    const EVENT_BEFORE_PROCESS = 'e_before_process';
    const EVENT_BEFORE_LOAD = 'e_before_load';

    /**
     * @var string ActiveRecord classname
     */
    protected $model;

    /**
     * Initializes CRUD handler
     * @throws \yii\base\ErrorException
     */
    public function __construct($model = false)
    {
        if (!$model) {
            $model = $this->modelClass();
        }

        $this->model = $model;

        if (!is_subclass_of($this->model, \yii\base\Model::className())) {
            throw new \yii\base\ErrorException('Invalid model class. Method "getModelClass" has to retrieve Model descendant class.');
        }
    }

    /**
     * Process handler
     * ---
     * Processes through GxEvent which holds all data about action
     * @param array $attrs given attributes
     * @param string $scope given scope for massive assignment
     * @return instance|null
     */
    public function process(array $attrs, $scope = null)
    {
        $event = new GxEvent(['input' => $attrs]);

        $this->trigger(self::EVENT_BEFORE_PROCESS, $event);

        $this->processModel($event);

        $this->trigger(self::EVENT_AFTER_PROCESS, $event);

        return $event;
    }

    /**
     * Process model validation and run callback based on validation result
     * @param \yii\db\ActiveRecord $model given model to be changed
     * @param array $attrs given attributes
     * @return mixed
     */
    protected function processModel(GxEvent &$event, $scope = null)
    {
        $class = $this->model;

        $event->model = $this->instantiateModel($this->model);
        
        $this->trigger(self::EVENT_BEFORE_LOAD, $event);

        // load data into model
        if ($event->model && ($event->model->load($event->input, $scope) || $event->isPushed())) {
            // if model is valid run validCallback otherwise notValidCallback
            if ($event->model->validate()) {
                $this->validCallback($event);
            } else {
                $this->notValidCallback($event);
            }
        }
    }

    /**
     * Instantiate appropriate model
     * @param string $class
     * @return \yii\base\Model
     */
    protected function instantiateModel($class)
    {
        return new $class;
    }

    /**
     * Retrieves model class, which must be \yii\base\Model class descendant
     * @return string model class
     */
    abstract protected function modelClass();

    /**
     * Processes when loaded model is valid
     */
    abstract protected function validCallback(GxEvent $event);

    /**
     * Processes when loaded model is not valid
     */
    abstract protected function notValidCallback(GxEvent $event);
}
{'fraction_non_alphanumeric': 0.1204872321690637, 'fraction_numerical': 0.0017610801291458762, 'mean_word_length': 2.4646670055922724, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 49, 'https://': 0, 'lorem ipsum': 0, 'www.': 6, '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': '2476706', 'n_tokens_mistral': 1965, 'n_tokens_neox': 1900, 'n_words': 528}