Completed
Push — master ( 981cf5...1a86ba )
by Dmitry
19:31
created

Thread::scenarioActions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 7
ccs 0
cts 7
cp 0
rs 9.4285
cc 1
eloc 4
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\behaviors\File;
14
use hipanel\helpers\Markdown;
15
use hipanel\modules\client\models\Client;
16
use stdClass;
17
use Yii;
18
use yii\web\NotFoundHttpException;
19
20
/**
21
 * Class Ticket.
22
 */
23
class Thread extends \hipanel\base\Model
24
{
25
    use \hipanel\base\ModelTrait;
26
27
    public static $i18nDictionary = 'hipanel:ticket';
28
29
    const STATE_OPEN = 'opened';
30
    const STATE_CLOSE = 'closed';
31
32
    public $search_form;
33
34
    public function init()
35
    {
36
        $this->on(static::EVENT_BEFORE_INSERT, [$this, 'beforeChange']);
37
        $this->on(static::EVENT_BEFORE_UPDATE, [$this, 'beforeChange']);
38
    }
39
40
    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...
41
    {
42
        $this->prepareSpentTime();
43
        $this->prepareTopic();
44
45
        return true;
46
    }
47
48
    /**
49
     * @return array
50
     */
51 View Code Duplication
    public function behaviors()
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...
52
    {
53
        return [
54
            [
55
                'class' => File::class,
56
                'attribute' => 'file',
57
                'targetAttribute' => 'file_ids',
58
                'scenarios' => ['create', 'answer'],
59
            ],
60
        ];
61
    }
62
63
    /**
64
     * @return array
65
     */
66
    public function attributes()
67
    {
68
        return [
69
            'id',
70
            'subject',
71
            'state',
72
            'state_label',
73
            'author_email',
74
            'author',
75
            'author_id',
76
            'responsible_id',
77
            'responsible_email',
78
            'author_seller',
79
            'author_seller_id',
80
            'recipient_id',
81
            'recipient',
82
            'recipient_seller',
83
            'recipient_seller_id',
84
            'replier_id',
85
            'replier',
86
            'replier_seller',
87
            'replier_name',
88
            'responsible',
89
            'priority',
90
            'priority_label',
91
            'spent', 'spent_hours',
92
            'answer_count',
93
            'status',
94
            'reply_time',
95
            'create_time',
96
            'a_reply_time',
97
            'elapsed',
98
            'topics',
99
            'topic',
100
            'watchers',
101
            'watcher',
102
            'add_tag_ids',
103
            'file_ids',
104
            'file',
105
            'message', // 'answer_message',
106
            'is_private',
107
108
            'anonym_name',
109
            'anonym_email',
110
            'anonym_seller',
111
112
            'lastanswer',
113
            'time',
114
            'add_watchers', 'del_watchers',
115
116
            'time_from',
117
            'time_till',
118
119
            'contact',
120
        ];
121
    }
122
123
    /**
124
     * {@inheritdoc}
125
     */
126
    public function rules()
127
    {
128
        $rules = [
129
            [['author_id', 'responsible_id'], 'integer'],
130
            [['subject', 'message'], 'required', 'on' => ['create']],
131
            [['id'], 'required', 'on' => ['answer', 'update-answer', 'open', 'close']],
132
            [
133
                [
134
                    'topics',
135
                    'state',
136
                    'priority',
137
                    'responsible',
138
                    'recipient_id',
139
                    'watchers',
140
                    'spent', 'spent_hours',
141
                    'file_ids',
142
                ],
143
                'safe',
144
                'on' => 'create',
145
            ],
146
            [
147
                [
148
                    'message',
149
                    'topics', 'state', 'priority',
150
                    'responsible', 'recipient_id',
151
                    'watchers', 'add_watchers', 'del_watchers',
152
                    'is_private',
153
                    'file_ids',
154
                    'spent', 'spent_hours',
155
                ],
156
                'safe',
157
                'on' => 'answer',
158
            ],
159
            [['state'], 'safe', 'on' => ['close', 'open']],
160
            // only client-side validation. Answer is actually possible without a message,
161
            // but does not make any sense.
162
            [['message'], 'required', 'on' => ['answer'], 'when' => function () {
163
                return false;
164
            }],
165
            [['id'], 'integer', 'on' => 'answer'],
166
            [['file'], 'file', 'maxFiles' => 5],
167
            [['lastanswer', 'create_time', 'recipient'], 'safe'],
168
            [['author', 'author_seller'], 'safe', 'when' => Yii::$app->user->can('support')],
169
        ];
170
171
        return $rules;
172
    }
173
174
    /**
175
     * {@inheritdoc}
176
     */
177
    public function attributeLabels()
178
    {
179
        return $this->mergeAttributeLabels([
180
            'author'           => Yii::t('hipanel:ticket', 'Author'),
181
            'author_id'        => Yii::t('hipanel:ticket', 'Author'),
182
            'recipient'        => Yii::t('hipanel:ticket', 'Recipient'),
183
            'is_private'       => Yii::t('hipanel:ticket', 'Make private'),
184
            'responsible'      => Yii::t('hipanel:ticket', 'Assignee'),
185
            'responsible_id'   => Yii::t('hipanel:ticket', 'Assignee'),
186
            'spent'            => Yii::t('hipanel:ticket', 'Spent time'),
187
            'create_time'      => Yii::t('hipanel:ticket', 'Created'),
188
            'a_reply_time'     => Yii::t('hipanel:ticket', 'a_reply_time'),
189
            'file'             => Yii::t('hipanel:ticket', 'Files'),
190
            'lastanswer'       => Yii::t('hipanel:ticket', 'Last answer'),
191
            'author_seller'    => Yii::t('hipanel:ticket', 'Seller'),
192
        ]);
193
    }
194
195
    public function getClient()
196
    {
197
        return $this->author;
0 ignored issues
show
Documentation introduced by
The property author does not exist on object<hipanel\modules\ticket\models\Thread>. 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...
198
    }
199
200
    public function getClient_id()
201
    {
202
        return $this->author_id;
0 ignored issues
show
Documentation introduced by
The property author_id does not exist on object<hipanel\modules\ticket\models\Thread>. 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...
203
    }
204
205
    public function getSeller()
206
    {
207
        return $this->author_seller;
0 ignored issues
show
Documentation introduced by
The property author_seller does not exist on object<hipanel\modules\ticket\models\Thread>. 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...
208
    }
209
210
    public function getSeller_id()
211
    {
212
        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\Thread>. 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...
213
    }
214
215
    public function getThreadUrl()
216
    {
217
        return ['@ticket/view', 'id' => $this->id];
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<hipanel\modules\ticket\models\Thread>. 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...
218
    }
219
220
    public static function parseMessage($message)
221
    {
222
        $message = str_replace(["\n\r", "\r\r", "\r\n"], "\n", $message); // "\n\n",
223
        $message = Markdown::process($message);
224
225
        return $message;
226
    }
227
228
    public function prepareSpentTime()
229
    {
230
        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\Thread>. 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\Thread>. 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...
231
    }
232
233
    public function prepareTopic()
234
    {
235
        $this->topics = is_array($this->topics) ? implode(',', $this->topics) : $this->topics;
0 ignored issues
show
Documentation introduced by
The property topics does not exist on object<hipanel\modules\ticket\models\Thread>. 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 topics does not exist on object<hipanel\modules\ticket\models\Thread>. 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...
236
    }
237
238
    public function getWatchersLogin()
239
    {
240
        $results = [];
241
        foreach ((array) $this->watchers as $id => $watcher) {
242
            list($login, $email) = explode(' ', $watcher);
0 ignored issues
show
Unused Code introduced by
The assignment to $email is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
243
            $results[$id] = $login;
244
        }
245
246
        return $results;
247
    }
248
249
    public function xFormater(array $items)
250
    {
251
        $result = [];
252
        foreach ($items as $id => $label) {
253
            $object = new stdClass();
254
            $object->value = $id;
255
            $object->text = $label;
256
            $result[] = $object;
257
        }
258
259
        return $result;
260
    }
261
262
    public function getAnswers()
263
    {
264
        // TODO: redo API in order to have different `Thread` and `ThreadMessage` models
265
        return $this->hasMany(Answer::class, ['id' => 'id'])->joinWith('files')->indexBy('answer_id');
266
    }
267
268
    /**
269
     * Returns array of client types, that can be set as responsible for the thread.
270
     *
271
     * @return array
272
     */
273
    public static function getResponsibleClientTypes()
274
    {
275
        return [Client::TYPE_SELLER, Client::TYPE_ADMIN, Client::TYPE_MANAGER, Client::TYPE_OWNER];
276
    }
277
278
    /**
279
     * @param integer $id
280
     * @param bool $throwOnError whether to throw an exception when answer is not found in thread
281
     * @throws NotFoundHttpException
282
     * @return Answer
283
     */
284
    public function getAnswer($id, $throwOnError = true)
285
    {
286
        if (!isset($this->answers[$id]) && $throwOnError) {
0 ignored issues
show
Documentation introduced by
The property answers does not exist on object<hipanel\modules\ticket\models\Thread>. 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...
287
            throw new NotFoundHttpException('Answer does not belong to this model');
288
        }
289
290
        return $this->answers[$id];
0 ignored issues
show
Documentation introduced by
The property answers does not exist on object<hipanel\modules\ticket\models\Thread>. 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...
291
    }
292
293
    public function getMaxAnswerId()
294
    {
295
        if ($this->isRelationPopulated('answers')) {
296
            return max(array_keys($this->answers));
0 ignored issues
show
Documentation introduced by
The property answers does not exist on object<hipanel\modules\ticket\models\Thread>. 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...
297
        }
298
299
        return $this->id;
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<hipanel\modules\ticket\models\Thread>. 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...
300
    }
301
302
    public function scenarioActions()
303
    {
304
        return [
305
            'open' => 'answer',
306
            'close' => 'answer',
307
        ];
308
    }
309
}
310