Completed
Push — master ( 5abd28...83c676 )
by Klochok
22:57
created

Reminder   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 120
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 13
Bugs 2 Features 3
Metric Value
c 13
b 2
f 3
dl 0
loc 120
ccs 0
cts 75
cp 0
rs 10
wmc 15
lcom 1
cbo 4

9 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 5 1
A reminderNextTimeOptions() 0 10 1
A rules() 0 19 1
A attributeLabels() 0 10 1
A getObjectName() 0 13 3
A find() 0 6 1
A changeNextTime() 0 6 2
A insertWithClientOffset() 0 7 3
A calculateClientNextTime() 0 6 2
1
<?php
2
3
namespace hipanel\models;
4
5
use DateTime;
6
use DateTimeZone;
7
use hipanel\base\Model;
8
use hipanel\base\ModelTrait;
9
use Yii;
10
11
class Reminder extends Model
12
{
13
    use ModelTrait;
14
15
    public static $i18nDictionary = 'hipanel/reminder';
16
17
    const SCENARIO_CREATE = 'create';
18
    const SCENARIO_UPDATE = 'update';
19
    const SCENARIO_DELETE = 'delete';
20
21
    const TYPE_SITE = 'site';
22
    const TYPE_MAIL = 'mail';
23
24
    public $offset;
25
    public $reminderChange;
26
27
    public function init()
28
    {
29
        $this->on(self::EVENT_BEFORE_UPDATE, [$this, 'changeNextTime']);
30
        $this->on(self::EVENT_BEFORE_INSERT, [$this, 'insertWithClientOffset']);
31
    }
32
33
    public static function reminderNextTimeOptions()
34
    {
35
        return [
36
            '+15 minutes' => Yii::t('hipanel/reminder', '15m'),
37
            '+30 minutes' => Yii::t('hipanel/reminder', '30m'),
38
            '+1 hour' => Yii::t('hipanel/reminder', '1h'),
39
            '+12 hour' => Yii::t('hipanel/reminder', '12h'),
40
            '+1 day' => Yii::t('hipanel/reminder', '1d'),
41
        ];
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47
    public function rules()
48
    {
49
        return [
50
            [['id', 'object_id', 'client_id', 'state_id', 'type_id'], 'integer'],
51
            [['class_name', 'periodicity', 'from_time', 'till_time', 'next_time'], 'string'],
52
            [['to_site'], 'boolean'],
53
54
            // Create
55
            [['object_id', 'type', 'periodicity', 'from_time', 'message', 'offset'], 'required', 'on' => self::SCENARIO_CREATE],
56
57
            // Update
58
            [['id'], 'required', 'on' => 'update'],
59
            [['object_id', 'state_id', 'type_id'], 'integer', 'on' => self::SCENARIO_UPDATE],
60
            [['from_time', 'next_time', 'till_time', 'reminderChange', 'offset'], 'string', 'on' => self::SCENARIO_UPDATE],
61
62
            // Delete
63
            [['id'], 'required', 'on' => self::SCENARIO_DELETE]
64
        ];
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70
    public function attributeLabels()
71
    {
72
        return $this->mergeAttributeLabels([
73
            'periodicity' => Yii::t('hipanel/reminder', 'Periodicity'),
74
            'from_time' => Yii::t('hipanel/reminder', 'When the recall?'),
75
            'next_time' => Yii::t('hipanel/reminder', 'Next remind'),
76
            'till_time' => Yii::t('hipanel/reminder', 'Remind till'),
77
            'message' => Yii::t('hipanel/reminder', 'Message'),
78
        ]);
79
    }
80
81
    public function getObjectName()
82
    {
83
        $result = '';
84
        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...
85
            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...
86
                case 'thread':
87
                    $result = 'ticket';
88
                    break;
89
            }
90
        }
91
92
        return $result;
93
    }
94
95
    /**
96
     * {@inheritdoc}
97
     * @return ReminderQuery
98
     */
99
    public static function find($options = [])
100
    {
101
        return new ReminderQuery(get_called_class(), [
102
            'options' => $options,
103
        ]);
104
    }
105
106
    /**
107
     * @return bool
108
     */
109
    public function changeNextTime()
110
    {
111
        if ($this->scenario == self::SCENARIO_UPDATE) {
112
            $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...
113
        }
114
    }
115
116
    public function insertWithClientOffset()
117
    {
118
        if ($this->scenario == self::SCENARIO_CREATE) {
119
            $this->offset = strpos($this->offset, '-') !== false ? '+' . $this->offset : '-' . $this->offset;
120
            $this->from_time = (new DateTime($this->from_time))->modify($this->offset . ' minutes')->format('Y-m-d H:i:s');
0 ignored issues
show
Documentation introduced by
The property from_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 from_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...
121
        }
122
    }
123
124
    public function calculateClientNextTime($offset)
125
    {
126
        $offset = strpos($offset, '-') !== false ? $offset : '+' . $offset;
127
        $next_time = (new DateTime($this->next_time))->modify($offset . ' minutes');
0 ignored issues
show
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...
128
        return Yii::$app->formatter->asDatetime($next_time, 'short');
129
    }
130
}
131