Completed
Push — master ( 3248b4...c5658a )
by Dmitry
03:44
created

Reminder::fixNextTimeTZ()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 2
eloc 3
c 1
b 1
f 0
nc 2
nop 0
dl 0
loc 6
ccs 0
cts 6
cp 0
crap 6
rs 9.4285
1
<?php
2
3
namespace hipanel\models;
4
5
use DateTime;
6
use hipanel\base\Model;
7
use hipanel\base\ModelTrait;
8
use Yii;
9
10
class Reminder extends Model
11
{
12
    use ModelTrait;
13
14
    public static $i18nDictionary = 'hipanel/reminder';
15
16
    const SCENARIO_CREATE = 'create';
17
    const SCENARIO_UPDATE = 'update';
18
    const SCENARIO_DELETE = 'delete';
19
20
    const TYPE_SITE = 'site';
21
    const TYPE_MAIL = 'mail';
22
23
    public $clientTimeZone;
24
    public $reminderChange;
25
26
    public function init()
27
    {
28
        $this->on(self::EVENT_BEFORE_UPDATE, [$this, 'fixNextTimeTZ']);
29
    }
30
31
    public static function reminderNextTimeOptions()
32
    {
33
        return [
34
            '+ 15 minutes' => Yii::t('hipanel/reminder', '15m'),
35
            '+ 30 minutes' => Yii::t('hipanel/reminder', '30m'),
36
            '+ 1 hour' => Yii::t('hipanel/reminder', '1h'),
37
            '+ 12 hour' => Yii::t('hipanel/reminder', '12h'),
38
            '+ 1 day' => Yii::t('hipanel/reminder', '1d'),
39
        ];
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45
    public function rules()
46
    {
47
        return [
48
            [['id', 'object_id', 'client_id', 'state_id', 'type_id'], 'integer'],
49
            [['class_name', 'periodicity', 'from_time', 'till_time', 'next_time'], 'string'],
50
            [['to_site'], 'boolean'],
51
52
            // Create
53
            [['object_id', 'type', 'periodicity', 'from_time', 'message'], 'required', 'on' => self::SCENARIO_CREATE],
54
55
            // Update
56
            [['id'], 'required', 'on' => 'update'],
57
            [['object_id', 'state_id', 'type_id'], 'integer', 'on' => self::SCENARIO_UPDATE],
58
            [['from_time', 'next_time', 'till_time', 'reminderChange', 'clientTimeZone'], 'string', 'on' => self::SCENARIO_UPDATE],
59
60
            // Delete
61
            [['id'], 'required', 'on' => self::SCENARIO_DELETE]
62
        ];
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68
    public function attributeLabels()
69
    {
70
        return $this->mergeAttributeLabels([
71
            'periodicity' => Yii::t('hipanel/reminder', 'Periodicity'),
72
            'from_time' => Yii::t('hipanel/reminder', 'When the recall?'),
73
            'next_time' => Yii::t('hipanel/reminder', 'Next remind'),
74
            'till_time' => Yii::t('hipanel/reminder', 'Remind till'),
75
            'message' => Yii::t('hipanel/reminder', 'Message'),
76
        ]);
77
    }
78
79
    public function getObjectName()
80
    {
81
        $result = '';
82
        if ($this->class_name) {
0 ignored issues
show
Documentation introduced by
The property class_name does not exist on object<hipanel\models\Reminder>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
83
            switch ($this->class_name) {
0 ignored issues
show
Documentation introduced by
The property class_name does not exist on object<hipanel\models\Reminder>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
84
                case 'thread':
85
                    $result = 'ticket';
86
                    break;
87
            }
88
        }
89
90
        return $result;
91
    }
92
93
    /**
94
     * {@inheritdoc}
95
     * @return ReminderQuery
96
     */
97
    public static function find($options = [])
98
    {
99
        return new ReminderQuery(get_called_class(), [
100
            'options' => $options,
101
        ]);
102
    }
103
104
    /**
105
     * @return bool
106
     */
107
    public function fixNextTimeTZ()
108
    {
109
        if ($this->scenario == self::SCENARIO_UPDATE) {
110
            $this->next_time = (new DateTime($this->next_time))->modify($this->reminderChange)->format('Y-m-d H:i:s');
0 ignored issues
show
Documentation introduced by
The property next_time does not exist on object<hipanel\models\Reminder>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
Documentation introduced by
The property next_time does not exist on object<hipanel\models\Reminder>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
111
        }
112
    }
113
}
114