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

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