Completed
Push — master ( 228148...182bca )
by Andrii
06:39
created

Answer::getThreadViewTitle()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

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 4
ccs 0
cts 4
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 2
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 Yii;
15
use yii\helpers\Html as Html;
16
17
/**
18
 * Class Ticket.
19
 */
20
class Answer extends \hipanel\base\Model
21
{
22
    use \hipanel\base\ModelTrait;
23
24
    public static function index()
25
    {
26
        return 'threads';
27
    }
28
29
    public static function type()
30
    {
31
        return 'thread';
32
    }
33
34
    public static $i18nDictionary = 'hipanel/ticket';
35
36
    public function init()
37
    {
38
        $this->on(static::EVENT_BEFORE_INSERT, [$this, 'beforeChange']);
39
        $this->on(static::EVENT_BEFORE_UPDATE, [$this, 'beforeChange']);
40
    }
41
42
    public function load($data, $formName = null)
43
    {
44
        $result = parent::load($data, $formName);
45
        $this->prepareSpentTime();
46
47
        return $result;
48
    }
49
50
    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...
51
    {
52
        $this->switchId();
53
54
        return true;
55
    }
56
57
    private function switchId()
58
    {
59
        if ($this->scenario === 'update') {
60
            $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...
61
        }
62
    }
63
64
    /**
65
     * @return array
66
     */
67
    public function behaviors()
68
    {
69
        return [
70
            [
71
                'class'          => 'hipanel\behaviors\File',
72
                'attribute'      => 'file',
73
                'savedAttribute' => 'file_ids',
74
                'scenarios'      => ['create', 'answer'],
75
            ],
76
        ];
77
    }
78
79
    /**
80
     * @return array
81
     */
82
    public function attributes()
83
    {
84
        return [
85
            'id',
86
            'answer_id',
87
            'message',
88
            'private',
89
            'is_private',
90
            'is_answer',
91
            'changed_num',
92
            'author',
93
            'author_seller',
94
            'email_hash',
95
            'create_time',
96
            'author_id',
97
            'seller_id',
98
            'spent',
99
            'spent_hours',
100
            'is_moved',
101
            'ip',
102
            'files',
103
            'file',
104
        ];
105
    }
106
107
    /**
108
     * {@inheritdoc}
109
     */
110
    public function rules()
111
    {
112
        return [
113
            [
114
                ['id', 'answer_id', 'spent', 'spent_hours'],
115
                'integer',
116
                'enableClientValidation' => false,
117
            ],
118
            [
119
                ['message', 'id', 'answer_id'],
120
                'required',
121
                'on' => 'update',
122
            ],
123
            [
124
                ['spent', '!spent_hours'],
125
                'safe',
126
                'on' => 'update',
127
            ],
128
            [
129
                ['is_private'],
130
                'boolean',
131
                'on' => 'update',
132
                'when' => function () {
133
                    return Yii::$app->user->can('support');
134
                },
135
            ],
136
        ];
137
    }
138
139
    /**
140
     * {@inheritdoc}
141
     */
142 View Code Duplication
    public function attributeLabels()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
143
    {
144
        return $this->mergeAttributeLabels([
145
            'author'           => Yii::t('hipanel/ticket', 'Author'),
146
            'author_id'        => Yii::t('hipanel/ticket', 'Author'),
147
            'recipient'        => Yii::t('hipanel/ticket', 'Recipient'),
148
            'is_private'       => Yii::t('hipanel/ticket', 'Make private'),
149
            'responsible'      => Yii::t('hipanel/ticket', 'Assignee'),
150
            'responsible_id'   => Yii::t('hipanel/ticket', 'Assignee'),
151
            'spent'            => Yii::t('hipanel/ticket', 'Spent time'),
152
            'create_time'      => Yii::t('hipanel/ticket', 'Created'),
153
            'a_reply_time'     => Yii::t('hipanel/ticket', 'a_reply_time'),
154
            'file'             => Yii::t('hipanel/ticket', 'Files'),
155
            'lastanswer'       => Yii::t('hipanel/ticket', 'Last answer'),
156
            'author_seller'    => Yii::t('hipanel/ticket', 'Author\'s seller'),
157
        ]);
158
    }
159
160
    public function getClient()
161
    {
162
        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...
163
    }
164
165
    public function getClientId()
166
    {
167
        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...
168
    }
169
170
    public function getSeller()
171
    {
172
        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...
173
    }
174
175
    public function getSellerId()
176
    {
177
        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...
178
    }
179
180
    public function prepareSpentTime()
181
    {
182
        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...
183
    }
184
185
    public function getThread()
186
    {
187
        return $this->hasOne(Thread::class, ['id' => 'id']);
188
    }
189
190
    public function scenarioCommands()
191
    {
192
        return [
193
            'update' => 'update-answer',
194
        ];
195
    }
196
}
197