Completed
Push — master ( 93b9ce...88a281 )
by Andrii
14:54
created

Answer::attributes()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 24
ccs 0
cts 23
cp 0
rs 8.9713
cc 1
eloc 21
nc 1
nop 0
crap 2
1
<?php
2
/**
3
 * HiPanel tickets module
4
 *
5
 * @link      https://github.com/hiqdev/hipanel-module-ticket
6
 * @package   hipanel-module-ticket
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2015-2017, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hipanel\modules\ticket\models;
12
13
use hipanel\models\File;
14
use Yii;
15
16
/**
17
 * Class Ticket.
18
 */
19
class Answer extends \hipanel\base\Model
20
{
21
    use \hipanel\base\ModelTrait;
22
23
    public static $i18nDictionary = 'hipanel:ticket';
24
25
    public static function tableName()
26
    {
27
        return 'thread';
28
    }
29
30
    public function init()
31
    {
32
        $this->on(static::EVENT_BEFORE_INSERT, [$this, 'beforeChange']);
33
        $this->on(static::EVENT_BEFORE_UPDATE, [$this, 'beforeChange']);
34
    }
35
36
    public function load($data, $formName = null)
37
    {
38
        $result = parent::load($data, $formName);
39
        $this->prepareSpentTime();
40
41
        return $result;
42
    }
43
44
    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...
45
    {
46
        $this->switchId();
47
48
        return true;
49
    }
50
51
    private function switchId()
52
    {
53
        if ($this->scenario === 'update') {
54
            $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...
55
        }
56
    }
57
58
    /**
59
     * @return array
60
     */
61
    public function behaviors()
62
    {
63
        return [
64
            [
65
                'class' => 'hipanel\behaviors\File',
66
                'attribute' => 'file',
67
                'targetAttribute' => 'file_ids',
68
                'scenarios' => ['create', 'answer'],
69
            ],
70
        ];
71
    }
72
73
    /**
74
     * @return array
75
     */
76
    public function attributes()
77
    {
78
        return [
79
            'id',
80
            'answer_id',
81
            'message',
82
            'private',
83
            'is_private',
84
            'is_answer',
85
            'changed_num',
86
            'author',
87
            'author_id',
88
            'author_seller',
89
            'seller_id',
90
            'account',
91
            'email_hash',
92
            'create_time',
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