ContextBehavior::context()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 6
nc 2
nop 1
dl 0
loc 9
ccs 7
cts 7
cp 1
crap 2
rs 10
c 1
b 0
f 0
1
<?php
2
/**
3
 * @link https://github.com/lav45/yii2-settings
4
 * @copyright Copyright (c) 2016 LAV45
5
 * @author Aleksey Loban <[email protected]>
6
 * @license http://opensource.org/licenses/BSD-3-Clause
7
 */
8
9
namespace lav45\settings\behaviors;
10
11
use yii\base\Behavior;
12
13
/**
14
 * Class ContextBehavior
15
 * @package lav45\settings\behaviors
16
 *
17
 * @property \lav45\settings\Settings $owner
18
 */
19
class ContextBehavior extends Behavior
20
{
21
    /**
22
     * @var array
23
     */
24
    private $_models = [];
25
26 1
    public function context($data)
27
    {
28 1
        $key = md5(serialize($data));
29 1
        if (!isset($this->_models[$key])) {
30 1
            $settings = clone $this->owner;
31 1
            $settings->keyPrefix = $key;
32 1
            $this->_models[$key] = $settings;
33
        }
34 1
        return $this->_models[$key];
35
    }
36
}