Completed
Push — master ( b4f9a0...781efd )
by vistart
06:17
created

NotificationRangeTrait::getRange()   B

Complexity

Conditions 5
Paths 8

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 17
ccs 0
cts 14
cp 0
rs 8.8571
cc 5
eloc 12
nc 8
nop 0
crap 30
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
    public $rangeAttribute = 'range';
34
35
    public function getRange()
36
    {
37
        $rangeAttribute = $this->rangeAttribute;
38
        $range = Json::decode($this->$rangeAttribute);
39
        if (!isset($range['exclude']) || !$range['exclude']) {
40
            $range['exclude'] = false;
41
        } else {
42
            $range['exclude'] = true;
43
        }
44
        if (!isset($range['user'])) {
45
            $range['user'] = [];
46
        }
47
        if (!isset($range['status'])) {
48
            $range['status'] = [];
49
        }
50
        return $range;
51
    }
52
53
    public function setRange($range)
54
    {
55
        if (!is_array($range)) {
56
            $range = [];
57
        }
58
        if (isset($range['exclude']) && $range['exclude'] == false) {
59
            unset($range['exclude']);
60
        }
61
        if (isset($range['user']) && empty($range['user'])) {
62
            unset($range['user']);
63
        }
64
        if (isset($range['status']) && empty($range['status'])) {
65
            unset($range['status']);
66
        }
67
        $rangeAttribute = $this->rangeAttribute;
68
        return $this->$rangeAttribute = Json::encode($range);
69
    }
70
71
    public function inRange($user, $status = null)
72
    {
73
        $range = $this->getRange();
74
        if ($status === null) {
75
            return $range['exclude'] ? !in_array($user, $range['user']) : in_array($user, $range['user']);
76
        }
77
        return $range['exclude'] ? (!in_array($user, $range['user']) || !in_array($status, $range['status'])) : (in_array($user, $range['user']) || in_array($status, $range['status']));
78
    }
79
80
    public function getNotificationRangeRules()
81
    {
82
        return [
83
            [$this->rangeAttribute, 'string'],
84
            [$this->rangeAttribute, 'validateRange'],
85
        ];
86
    }
87
88
    public function validateRange()
89
    {
90
        try {
91
            $rangeAttribute = $this->rangeAttribute;
92
            Json::decode($this->$rangeAttribute);
93
        } catch (\Exception $ex) {
94
            $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...
95
            return false;
96
        }
97
        return true;
98
    }
99
}
100