Completed
Push — master ( 39a64b...6c0fb3 )
by Andrii
16:00
created

Answer::scenarioActions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 6
ccs 0
cts 5
cp 0
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * HiPanel tickets module
5
 *
6
 * @link      https://github.com/hiqdev/hipanel-module-ticket
7
 * @package   hipanel-module-ticket
8
 * @license   BSD-3-Clause
9
 * @copyright Copyright (c) 2015-2016, HiQDev (http://hiqdev.com/)
10
 */
11
12
namespace hipanel\modules\ticket\models;
13
14
use hipanel\models\File;
15
use Yii;
16
17
/**
18
 * Class Ticket.
19
 */
20
class Answer extends \hipanel\base\Model
21
{
22
    use \hipanel\base\ModelTrait;
23
24
    public static $i18nDictionary = 'hipanel:ticket';
25
26
    public static function tableName()
27
    {
28
        return 'thread';
29
    }
30
31
    public function init()
32
    {
33
        $this->on(static::EVENT_BEFORE_INSERT, [$this, 'beforeChange']);
34
        $this->on(static::EVENT_BEFORE_UPDATE, [$this, 'beforeChange']);
35
    }
36
37
    public function load($data, $formName = null)
38
    {
39
        $result = parent::load($data, $formName);
40
        $this->prepareSpentTime();
41
42
        return $result;
43
    }
44
45
    public function beforeChange($event)
0 ignored issues
show
Unused Code introduced by
The parameter $event is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
46
    {
47
        $this->switchId();
48
49
        return true;
50
    }
51
52
    private function switchId()
53
    {
54
        if ($this->scenario === 'update') {
55
            $this->id = $this->answer_id;
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<hipanel\modules\ticket\models\Answer>. 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 answer_id does not exist on object<hipanel\modules\ticket\models\Answer>. 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...
56
        }
57
    }
58
59
    /**
60
     * @return array
61
     */
62
    public function behaviors()
63
    {
64
        return [
65
            [
66
                'class' => 'hipanel\behaviors\File',
67
                'attribute' => 'file',
68
                'targetAttribute' => 'file_ids',
69
                'scenarios' => ['create', 'answer'],
70
            ],
71
        ];
72
    }
73
74
    /**
75
     * @return array
76
     */
77
    public function attributes()
78
    {
79
        return [
80
            'id',
81
            'answer_id',
82
            'message',
83
            'private',
84
            'is_private',
85
            'is_answer',
86
            'changed_num',
87
            'author',
88
            'author_seller',
89
            'email_hash',
90
            'create_time',
91
            'author_id',
92
            'seller_id',
93
            'spent',
94
            'spent_hours',
95
            'is_moved',
96
            'ip',
97
            'file',
98
        ];
99
    }
100
101
    /**
102
     * {@inheritdoc}
103
     */
104
    public function rules()
105
    {
106
        return [
107
            [
108
                ['id', 'answer_id', 'spent', 'spent_hours'],
109
                'integer',
110
                'enableClientValidation' => false,
111
            ],
112
            [
113
                ['message', 'id', 'answer_id'],
114
                'required',
115
                'on' => 'update',
116
            ],
117
            [
118
                ['spent', '!spent_hours'],
119
                'safe',
120
                'on' => 'update',
121
            ],
122
            [
123
                ['is_private'],
124
                'boolean',
125
                'on' => 'update',
126
                'when' => function () {
127
                    return Yii::$app->user->can('support');
128
                },
129
            ],
130
        ];
131
    }
132
133
    /**
134
     * {@inheritdoc}
135
     */
136
    public function attributeLabels()
137
    {
138
        return $this->mergeAttributeLabels([
139
            'author' => Yii::t('hipanel:ticket', 'Author'),
140
            'author_id' => Yii::t('hipanel:ticket', 'Author'),
141
            'recipient' => Yii::t('hipanel:ticket', 'Recipient'),
142
            'is_private' => Yii::t('hipanel:ticket', 'Make private'),
143
            'responsible' => Yii::t('hipanel:ticket', 'Assignee'),
144
            'responsible_id' => Yii::t('hipanel:ticket', 'Assignee'),
145
            'spent' => Yii::t('hipanel:ticket', 'Spent time'),
146
            'create_time' => Yii::t('hipanel:ticket', 'Created'),
147
            'a_reply_time' => Yii::t('hipanel:ticket', 'a_reply_time'),
148
            'file' => Yii::t('hipanel:ticket', 'Files'),
149
            'lastanswer' => Yii::t('hipanel:ticket', 'Last answer'),
150
            'author_seller' => Yii::t('hipanel:ticket', 'Seller'),
151
        ]);
152
    }
153
154
    public function getClient()
155
    {
156
        return $this->author;
0 ignored issues
show
Documentation introduced by
The property author does not exist on object<hipanel\modules\ticket\models\Answer>. 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...
157
    }
158
159
    public function getClient_id()
160
    {
161
        return $this->author_id;
0 ignored issues
show
Documentation introduced by
The property author_id does not exist on object<hipanel\modules\ticket\models\Answer>. 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...
162
    }
163
164
    public function getSeller()
165
    {
166
        return $this->author_seller;
0 ignored issues
show
Documentation introduced by
The property author_seller does not exist on object<hipanel\modules\ticket\models\Answer>. 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...
167
    }
168
169
    public function getSeller_id()
170
    {
171
        return $this->author_seller_id;
0 ignored issues
show
Documentation introduced by
The property author_seller_id does not exist on object<hipanel\modules\ticket\models\Answer>. 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...
172
    }
173
174
    public function prepareSpentTime()
175
    {
176
        list($this->spent_hours, $this->spent) = explode(':', $this->spent, 2);
0 ignored issues
show
Documentation introduced by
The property spent_hours does not exist on object<hipanel\modules\ticket\models\Answer>. 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...
Documentation introduced by
The property spent does not exist on object<hipanel\modules\ticket\models\Answer>. 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...
177
    }
178
179
    public function getThread()
180
    {
181
        return $this->hasOne(Thread::class, ['id' => 'id']);
182
    }
183
184
    public function getFiles()
185
    {
186
        return $this->hasMany(File::class, ['object_id' => 'id']);
187
    }
188
189
    public function scenarioActions()
190
    {
191
        return [
192
            'update' => 'update-answer',
193
        ];
194
    }
195
}
196