<commit_before><?php
/**
 * Finance module for HiPanel
 *
 * @link      https://github.com/hiqdev/hipanel-module-finance
 * @package   hipanel-module-finance
 * @license   BSD-3-Clause
 * @copyright Copyright (c) 2015-2017, HiQDev (http://hiqdev.com/)
 */

namespace hipanel\modules\finance\logic;

use hipanel\modules\finance\forms\AbstractTariffForm;
use hipanel\modules\finance\models\Tariff;
use Yii;
use yii\base\Object;
use yii\helpers\ArrayHelper;
use yii\web\ForbiddenHttpException;
use yii\web\NotFoundHttpException;

abstract class AbstractTariffManager extends Object
{
    /**
     * @var Tariff[] they array of all available parent tariffs
     * @see findParentTariffs()
     */
    protected $parentTariffs;

    /**
     * @var AbstractTariffForm
     */
    public $form;

    /**
     * @var array options used to build [[form]]
     * @see buildForm()
     */
    public $formOptions = [];

    /**
     * @var string
     */
    public $scenario;

    /**
     * @var Tariff The actual tariff
     */
    protected $tariff;

    /**
     * @var string The type used to find parent tariffs
     */
    protected $type;

    public function init()
    {
        $this->fillParentTariffs();
        $this->buildForm();
    }

    /**
     * Fills [[form]] property with a proper [[AbstractTariffForm]] object.
     */
    protected function buildForm()
    {
        $this->form = Yii::createObject(array_merge([
            'scenario' => $this->scenario,
            'parentTariffs' => $this->parentTariffs,
            'tariff' => $this->tariff,
        ], $this->getFormOptions()));
    }

    protected function getFormOptions()
    {
        return $this->formOptions;
    }

    protected function fillParentTariffs()
    {
        $ids = $this->collectParentTariffIds();

        $this->parentTariffs = Tariff::find()
            ->where(['id' => $ids])
            ->details()
            ->all();
    }

    /**
     * Collects parent tariff ids. Used in [[fillParentTariffs]]
     *
     * @return array
     * @throws NotFoundHttpException
     */
    protected function collectParentTariffIds()
    {
        if (!isset($this->tariff)) {
            $availableTariffs = Tariff::find()
                ->action('get-available-info')
                ->andFilterWhere(['type' => $this->type])
                ->all();

            return ArrayHelper::getColumn($availableTariffs, 'id');
        }

        if (isset($this->tariff->parent_id)) {
            return [$this->tariff->parent_id];
        }

        throw new NotFoundHttpException('No available tariffs found');
    }

    public function getType()
    {
        return $this->type;
    }

    /**
     * @param Tariff $tariff
     */
    public function setTariff($tariff)
    {
        $this->tariff = $tariff;
    }
}
<commit_msg>Fixed AbstractTariffManager::collectParentTariffIds() to return current tariff id as the last hope. Used for case when tariff is a root.<commit_after><?php
/**
 * Finance module for HiPanel
 *
 * @link      https://github.com/hiqdev/hipanel-module-finance
 * @package   hipanel-module-finance
 * @license   BSD-3-Clause
 * @copyright Copyright (c) 2015-2017, HiQDev (http://hiqdev.com/)
 */

namespace hipanel\modules\finance\logic;

use hipanel\modules\finance\forms\AbstractTariffForm;
use hipanel\modules\finance\models\Tariff;
use Yii;
use yii\base\Object;
use yii\helpers\ArrayHelper;
use yii\web\ForbiddenHttpException;
use yii\web\NotFoundHttpException;

abstract class AbstractTariffManager extends Object
{
    /**
     * @var Tariff[] they array of all available parent tariffs
     * @see findParentTariffs()
     */
    protected $parentTariffs;

    /**
     * @var AbstractTariffForm
     */
    public $form;

    /**
     * @var array options used to build [[form]]
     * @see buildForm()
     */
    public $formOptions = [];

    /**
     * @var string
     */
    public $scenario;

    /**
     * @var Tariff The actual tariff
     */
    protected $tariff;

    /**
     * @var string The type used to find parent tariffs
     */
    protected $type;

    public function init()
    {
        $this->fillParentTariffs();
        $this->buildForm();
    }

    /**
     * Fills [[form]] property with a proper [[AbstractTariffForm]] object.
     */
    protected function buildForm()
    {
        $this->form = Yii::createObject(array_merge([
            'scenario' => $this->scenario,
            'parentTariffs' => $this->parentTariffs,
            'tariff' => $this->tariff,
        ], $this->getFormOptions()));
    }

    protected function getFormOptions()
    {
        return $this->formOptions;
    }

    protected function fillParentTariffs()
    {
        $ids = $this->collectParentTariffIds();

        $this->parentTariffs = Tariff::find()
            ->where(['id' => $ids])
            ->details()
            ->all();
    }

    /**
     * Collects parent tariff ids. Used in [[fillParentTariffs]]
     *
     * @return array
     * @throws NotFoundHttpException
     */
    protected function collectParentTariffIds()
    {
        if (!isset($this->tariff)) {
            $availableTariffs = Tariff::find()
                ->action('get-available-info')
                ->andFilterWhere(['type' => $this->type])
                ->all();

            return ArrayHelper::getColumn($availableTariffs, 'id');
        }

        if (isset($this->tariff->parent_id)) {
            return [$this->tariff->parent_id];
        }

        return [$this->tariff->id];
    }

    public function getType()
    {
        return $this->type;
    }

    /**
     * @param Tariff $tariff
     */
    public function setTariff($tariff)
    {
        $this->tariff = $tariff;
    }
}

{'fraction_non_alphanumeric': 0.13804537521815008, 'fraction_numerical': 0.0031413612565445027, 'mean_word_length': 2.168048645660586, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 63, 'https://': 2, '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': '1063295', 'n_tokens_mistral': 1787, 'n_tokens_neox': 1736, 'n_words': 368}