Completed
Push — master ( 39a64b...6c0fb3 )
by Andrii
16:00
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
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 7
ccs 0
cts 6
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\behaviors\File;
15
use hipanel\helpers\Markdown;
16
use hipanel\modules\client\models\Client;
17
use stdClass;
18
use Yii;
19
use yii\web\NotFoundHttpException;
20
21
/**
22
 * Class Ticket.
23
 */
24
class Thread extends \hipanel\base\Model
25
{
26
    use \hipanel\base\ModelTrait;
27
28
    public static $i18nDictionary = 'hipanel:ticket';
29
30
    const STATE_OPEN = 'opened';
31
    const STATE_CLOSE = 'closed';
32
33
    public $search_form;
34
35
    public function init()
36
    {
37
        $this->on(static::EVENT_BEFORE_INSERT, [$this, 'beforeChange']);
38
        $this->on(static::EVENT_BEFORE_UPDATE, [$this, 'beforeChange']);
39
    }
40
41
    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...
42
    {
43
        $this->prepareSpentTime();
44
        $this->prepareTopic();
45
46
        return true;
47
    }
48
49
    /**
50
     * @return array
51
     */
52
    public function behaviors()
53
    {
54
        return [
55
            [
56
                'class' => File::class,
57
                'attribute' => 'file',
58
                'targetAttribute' => 'file_ids',
59
                'scenarios' => ['create', 'answer'],
60
            ],
61
        ];
62
    }
63
64
    /**
65
     * @return array
66
     */
67
    public function attributes()
68
    {
69
        return [
70
            'id',
71
            'subject',
72
            'state',
73
            'state_label',
74
            'author_email',
75
            'author',
76
            'author_id',
77
            'responsible_id',
78
            'responsible_email',
79
            'author_seller',
80
            'author_seller_id',
81
            'recipient_id',
82
            'recipient',
83
            'recipient_seller',
84
            'recipient_seller_id',
85
            'replier_id',
86
            'replier',
87
            'replier_seller',
88
            'replier_name',
89
            'responsible',
90
            'priority',
91
            'priority_label',
92
            'spent', 'spent_hours',
93
            'answer_count',
94
            'status',
95
            'reply_time',
96
            'create_time',
97
            'a_reply_time',
98
            'elapsed',
99
            'topics',
100
            'topic',
101
            'watchers',
102
            'watcher',
103
            'add_tag_ids',
104
            'file_ids',
105
            'file',
106
            'message', // 'answer_message',
107
            'is_private',
108
109
            'anonym_name',
110
            'anonym_email',
111
            'anonym_seller',
112
113
            'lastanswer',
114
            'time',
115
            'add_watchers', 'del_watchers',
116
117
            'time_from',
118
            'time_till',
119
120
            'contact',
121
        ];
122
    }
123
124
    /**
125
     * {@inheritdoc}
126
     */
127
    public function rules()
128
    {
129
        $rules = [
130
            [['author_id', 'responsible_id'], 'integer'],
131
            [['subject', 'message'], 'required', 'on' => ['create']],
132
            [['id'], 'required', 'on' => ['answer', 'update-answer', 'open', 'close']],
133
            [
134
                [
135
                    'topics',
136
                    'state',
137
                    'priority',
138
                    'responsible',
139
                    'recipient_id',
140
                    'watchers',
141
                    'spent', 'spent_hours',
142
                    'file_ids',
143
                ],
144
                'safe',
145
                'on' => 'create',
146
            ],
147
            [
148
                [
149
                    'message',
150
                    'topics', 'state', 'priority',
151
                    'responsible', 'recipient_id',
152
                    'watchers', 'add_watchers', 'del_watchers',
153
                    'is_private',
154
                    'file_ids',
155
                    'spent', 'spent_hours',
156
                ],
157
                'safe',
158
                'on' => 'answer',
159
            ],
160
            [['state'], 'safe', 'on' => ['close', 'open']],
161
            // only client-side validation. Answer is actually possible without a message,
162
            // but does not make any sense.
163
            [['message'], 'required', 'on' => ['answer'], 'when' => function () { return false; }],
164
            [['id'], 'integer', 'on' => 'answer'],
165
            [['file'], 'file', 'maxFiles' => 5],
166
            [['lastanswer', 'create_time', 'recipient'], 'safe'],
167
            [['author', 'author_seller'], 'safe', 'when' => Yii::$app->user->can('support')],
168
        ];
169
170
        return $rules;
171
    }
172
173
    /**
174
     * {@inheritdoc}
175
     */
176
    public function attributeLabels()
177
    {
178
        return $this->mergeAttributeLabels([
179
            'author'           => Yii::t('hipanel:ticket', 'Author'),
180
            'author_id'        => Yii::t('hipanel:ticket', 'Author'),
181
            'recipient'        => Yii::t('hipanel:ticket', 'Recipient'),
182
            'is_private'       => Yii::t('hipanel:ticket', 'Make private'),
183
            'responsible'      => Yii::t('hipanel:ticket', 'Assignee'),
184
            'responsible_id'   => Yii::t('hipanel:ticket', 'Assignee'),
185
            'spent'            => Yii::t('hipanel:ticket', 'Spent time'),
186
            'create_time'      => Yii::t('hipanel:ticket', 'Created'),
187
            'a_reply_time'     => Yii::t('hipanel:ticket', 'a_reply_time'),
188
            'file'             => Yii::t('hipanel:ticket', 'Files'),
189
            'lastanswer'       => Yii::t('hipanel:ticket', 'Last answer'),
190
            'author_seller'    => Yii::t('hipanel:ticket', 'Seller'),
191
        ]);
192
    }
193
194
    public function getClient()
195
    {
196
        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...
197
    }
198
199
    public function getClient_id()
200
    {
201
        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...
202
    }
203
204
    public function getSeller()
205
    {
206
        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...
207
    }
208
209
    public function getSeller_id()
210
    {
211
        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...
212
    }
213
214
    public function getThreadUrl()
215
    {
216
        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...
217
    }
218
219
    public static function parseMessage($message)
220
    {
221
        $message = str_replace(["\n\r", "\r\r", "\r\n"], "\n", $message); // "\n\n",
222
        $message = Markdown::process($message);
223
224
        return $message;
225
    }
226
227
    public function prepareSpentTime()
228
    {
229
        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...
230
    }
231
232
    public function prepareTopic()
233
    {
234
        $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...
235
    }
236
237
    public function getWatchersLogin()
238
    {
239
        $results = [];
240
        foreach ((array) $this->watchers as $id => $watcher) {
241
            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...
242
            $results[$id] = $login;
243
        }
244
245
        return $results;
246
    }
247
248
    public function xFormater(array $items)
249
    {
250
        $result = [];
251
        foreach ($items as $id => $label) {
252
            $object = new stdClass();
253
            $object->value = $id;
254
            $object->text = $label;
255
            $result[] = $object;
256
        }
257
258
        return $result;
259
    }
260
261
    public function getAnswers()
262
    {
263
        // TODO: redo API in order to have different `Thread` and `ThreadMessage` models
264
        return $this->hasMany(Answer::class, ['id' => 'id'])->joinWith('files')->indexBy('answer_id');
265
    }
266
267
    /**
268
     * Returns array of client types, that can be set as responsible for the thread.
269
     *
270
     * @return array
271
     */
272
    public static function getResponsibleClientTypes()
273
    {
274
        return [Client::TYPE_SELLER, Client::TYPE_ADMIN, Client::TYPE_MANAGER, Client::TYPE_OWNER];
275
    }
276
277
    /**
278
     * @param integer $id
279
     * @param bool $throwOnError whether to throw an exception when answer is not found in thread
280
     * @throws NotFoundHttpException
281
     * @return Answer
282
     */
283
    public function getAnswer($id, $throwOnError = true)
284
    {
285
        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...
286
            throw new NotFoundHttpException('Answer does not belong to this model');
287
        }
288
289
        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...
290
    }
291
292
    public function getMaxAnswerId()
293
    {
294
        if ($this->isRelationPopulated('answers')) {
295
            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...
296
        }
297
298
        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...
299
    }
300
301
    public function scenarioActions()
302
    {
303
        return [
304
            'open' => 'answer',
305
            'close' => 'answer',
306
        ];
307
    }
308
}
309