DetectorTrait   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 1
dl 0
loc 31
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A getDetector() 0 10 3
1
<?php
2
/**
3
 * @link https://github.com/vuongxuongminh/yii2-mobile-first
4
 * @copyright Copyright (c) 2019 Vuong Xuong Minh
5
 * @license [New BSD License](http://www.opensource.org/licenses/bsd-license.php)
6
 */
7
8
namespace vxm\mobileFirst;
9
10
use Yii;
11
12
use Detection\MobileDetect;
13
14
/**
15
 * Provide the device detector for classes needed
16
 *
17
 * @author Vuong Minh <[email protected]>
18
 * @since 1.0.0
19
 */
20
trait DetectorTrait
21
{
22
    /**
23
     * @var string class of mobile detect use to detect device type.
24
     * @see [[getDetector()]]
25
     */
26
    public $detectorClass = MobileDetect::class;
27
28
    /**
29
     * @var MobileDetect
30
     */
31
    private $_detector;
32
33
    /**
34
     * Mobile detector
35
     *
36
     * @param bool $force weather you need to force the detector
37
     * @return object|MobileDetect
38
     * @throws \yii\base\InvalidConfigException
39
     */
40 8
    protected function getDetector(bool $force = false): MobileDetect
41
    {
42 8
        if ($this->_detector === null || $force) {
43
44 8
            return $this->_detector = Yii::createObject($this->detectorClass);
45
        } else {
46
47 2
            return $this->_detector;
48
        }
49
    }
50
}
51