Completed
Push — master ( 5f03d9...53d264 )
by vistart
06:52
created

NotificationRangeTrait   A

Complexity

Total Complexity 27

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 27
c 4
b 0
f 0
lcom 1
cbo 1
dl 0
loc 88
ccs 0
cts 55
cp 0
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
C getRange() 0 24 7
B setRange() 0 20 9
B inRange() 0 12 7
A getNotificationRangeRules() 0 7 2
A validateRange() 0 11 2
1
<?php
2
3
/**
4
 *  _   __ __ _____ _____ ___  ____  _____
5
 * | | / // // ___//_  _//   ||  __||_   _|
6
 * | |/ // /(__  )  / / / /| || |     | |
7
 * |___//_//____/  /_/ /_/ |_||_|     |_|
8
 * @link https://vistart.name/
9
 * @copyright Copyright (c) 2016 vistart
10
 * @license https://vistart.name/license/
11
 */
12
13
namespace vistart\Models\traits;
14
15
use yii\helpers\Json;
16
17
/**
18
 * Description of NotificationRangeTrait
19
 *
20
 * @property array range
21
 * [
22
 *     'user' => [
23
 *     ],
24
 *     'status' => [
25
 *     ],
26
 *     'exclude' => false, (default)
27
 * ]
28
 * @author vistart <[email protected]>
29
 */
30
trait NotificationRangeTrait
31
{
32
33
    /**
34
     * @var string range attribute name.
35
     * We do not recommend you access this attribute directly.
36
     */
37
    public $rangeAttribute = 'range';
38
39
    public function getRange()
40
    {
41
        $rangeAttribute = $this->rangeAttribute;
42
        if (!is_string($rangeAttribute)) {
43
            return null;
44
        }
45
        try {
46
            $range = Json::decode($this->$rangeAttribute);
47
        } catch (\Exception $ex) {
48
            $range = [];
49
        }
50
        if (!isset($range['exclude']) || !$range['exclude']) {
51
            $range['exclude'] = false;
52
        } else {
53
            $range['exclude'] = true;
54
        }
55
        if (!isset($range['user'])) {
56
            $range['user'] = [];
57
        }
58
        if (!isset($range['status'])) {
59
            $range['status'] = [];
60
        }
61
        return $range;
62
    }
63
64
    public function setRange($range)
65
    {
66
        $rangeAttribute = $this->rangeAttribute;
67
        if (!is_string($rangeAttribute)) {
68
            return null;
69
        }
70
        if (!is_array($range)) {
71
            $range = [];
72
        }
73
        if (isset($range['exclude']) && $range['exclude'] == false) {
74
            unset($range['exclude']);
75
        }
76
        if (isset($range['user']) && empty($range['user'])) {
77
            unset($range['user']);
78
        }
79
        if (isset($range['status']) && empty($range['status'])) {
80
            unset($range['status']);
81
        }
82
        return $this->$rangeAttribute = Json::encode($range);
83
    }
84
85
    public function inRange($user, $status = null)
86
    {
87
        $rangeAttribute = $this->rangeAttribute;
88
        if (!is_string($rangeAttribute)) {
89
            return false;
90
        }
91
        $range = $this->getRange();
92
        if ($status === null) {
93
            return $range['exclude'] ? !in_array($user, $range['user']) : in_array($user, $range['user']);
94
        }
95
        return $range['exclude'] ? (!in_array($user, $range['user']) || !in_array($status, $range['status'])) : (in_array($user, $range['user']) || in_array($status, $range['status']));
96
    }
97
98
    public function getNotificationRangeRules()
99
    {
100
        return is_string($this->rangeAttribute) ? [
101
            [$this->rangeAttribute, 'string'],
102
            [$this->rangeAttribute, 'validateRange'],
103
            ] : [];
104
    }
105
106
    public function validateRange()
107
    {
108
        try {
109
            $rangeAttribute = $this->rangeAttribute;
110
            Json::decode($this->$rangeAttribute);
111
        } catch (\Exception $ex) {
112
            $this->addError($ex->getMessage());
0 ignored issues
show
Bug introduced by
It seems like addError() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
113
            return false;
114
        }
115
        return true;
116
    }
117
}
118