Passed
Push — master ( c523c5...2aef85 )
by
unknown
02:17
created

BaseLanguageSetting::findOneByKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 6
rs 10
1
<?php
2
namespace andmemasin\language\models;
3
4
use andmemasin\language\interfaces\WithLanguageInterface;
5
use andmemasin\myabstract\MyActiveRecord;
6
use andmemasin\myabstract\src\interfaces\SettingInterface;
0 ignored issues
show
Bug introduced by
The type andmemasin\myabstract\sr...rfaces\SettingInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use andmemasin\myabstract\TypeInterface;
8
use yii\base\InvalidConfigException;
9
10
/**
11
 * Class BaseLanguageSetting
12
 * @property integer $language_id
13
 * @property integer $is_default
14
 * @property string $value
15
 *
16
 * @property Language $language
17
 * @property TypeInterface $type
18
 *
19
 * @package andmemasin\language\models
20
 */
21
class BaseLanguageSetting extends MyActiveRecord implements  SettingInterface {
22
23
    /** @var string $typeColumn the column containing setting type id */
24
    protected static $typeColumn;
25
26
    /** @var string $typeClass Class of setting */
27
    protected  static $typeClass;
28
29
    /** @var WithLanguageInterface */
30
    protected $parent;
31
32
33
    /** {@inheritdoc} */
34
    public function init()
35
    {
36
        parent::init();
37
        if($this->insert()){
38
            if(!$this->parent){
39
                throw new InvalidConfigException('Parent Model must be defined while initiating languages-settings');
40
            }
41
            if(!$this->parent instanceof WithLanguageInterface){
0 ignored issues
show
introduced by
$this->parent is always a sub-type of andmemasin\language\inte...s\WithLanguageInterface. If $this->parent can have other possible types, add them to models/BaseLanguageSetting.php:29.
Loading history...
42
                throw new InvalidConfigException('Parent Model must implement ModelWithLanguages');
43
            }
44
45
        }
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51
    public function rules()
52
    {
53
        return array_merge([
54
            [['is_default', 'language_id'], 'integer'],
55
            [['language_id', 'value'], 'required'],
56
            [['value'], 'string'],
57
            [['language_id'], 'exist', 'skipOnError' => true, 'targetClass' => Language::class, 'targetAttribute' => ['language_id' => 'language_id']],
58
        ],  parent::rules());
59
    }
60
61
    /**
62
     * @param MyActiveRecord $parent
63
     * @param Language $language
64
     * @param string $key
65
     * @return bool|static|array
66
     */
67
    public static function getSetting($parent, $language, $key){
68
69
        /** @var TypeInterface $typeClass */
70
        $typeClass = static::$typeClass;
71
        $type = $typeClass::getByKey($key);
72
73
        if(!empty($type)){
74
            /** @var BaseLanguageSetting $model */
75
            $query = static::find();
76
            if(!empty($parent)){
77
                $query->andWhere([$parent::primaryKeySingle() =>$parent->primaryKey]);
78
            }
79
            $model =$query->andWhere(['language_id'=>$language->primaryKey])
80
                ->andWhere([$typeClass::primaryKeySingle()=>$type->{$typeClass::primaryKeySingle()}])
81
                ->one();
82
83
            if(empty($model)){
84
                /** @var BaseLanguageSetting $model */
85
                $model = \Yii::createObject(['class'=>static::class]);
86
                $model->language_id = $language->primaryKey;
87
                if(!empty($parent)){
88
                    $model->{$parent::primaryKeySingle()} = $parent->primaryKey;
89
                }
90
                $model->{$typeClass::primaryKeySingle()}=$type->{$typeClass::primaryKeySingle()};
91
92
            }
93
            return $model;
94
        }
95
96
        throw new InvalidConfigException('Undefined type key: ' . $key);
97
98
    }
99
100
101
    /**
102
     * {@inheritdoc}
103
     */
104
    public function behaviors()
105
    {
106
        return array_merge([
107
            'actionlog' => [
108
                'class' => 'andmemasin\actionlog\behaviors\ActionLogBehavior',
109
            ],
110
        ], parent::behaviors());
111
    }
112
113
    /**
114
     * @return \yii\db\ActiveQuery
115
     */
116
    public function getLanguage()
117
    {
118
        return $this->hasOne(Language::class, ['language_id' => 'language_id']);
119
    }
120
121
122
    /**
123
     * @return TypeInterface
124
     */
125
    public function getType()
126
    {
127
        /** @var TypeInterface $typeClass */
128
        $typeClass = static::$typeClass;
129
        return $typeClass::getByKey(static::$typeColumn);
130
    }
131
132
    /**
133
     * @param $key string
134
     * @return static
135
     */
136
    public function findOneByKey($key){
137
        /** @var static $model */
138
        $model = static::find()
139
            ->andWhere([static::$typeColumn => $key])
140
            ->one();
141
        return $model;
142
    }
143
144
}