QueueTrait::queueUpdate()   C
last analyzed

Complexity

Conditions 7
Paths 6

Size

Total Lines 31
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 7.4822

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 31
ccs 11
cts 14
cp 0.7856
rs 6.7272
cc 7
eloc 13
nc 6
nop 2
crap 7.4822
1
<?php
2
3
/*
4
 * This file is part of the Tinyissue package.
5
 *
6
 * (c) Mohamed Alsharaf <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Tinyissue\Model\Traits\Project\Issue;
13
14
use Tinyissue\Model\Project\Issue;
15
use Tinyissue\Model\Message\Queue;
16
use Tinyissue\Model\User;
17
18
/**
19
 * QueueTrait is trait class for adding method to insert records into a queue.
20
 *
21
 * @author Mohamed Alsharaf <[email protected]>
22
 *
23
 * @property static $this
24
 */
25
trait QueueTrait
26
{
27
    /**
28
     * Insert update issue to message queue.
29
     *
30
     * @param Issue $issue
31
     * @param User  $changeBy
32
     *
33
     * @return void
34
     */
35 11
    public function queueUpdate(Issue $issue, User $changeBy)
36
    {
37
        // Number of changed attributes
38 11
        $countChanges = count($issue->getDirty());
39
40
        // Whether or not the assignee has changed
41 11
        $noMessageForMe = false;
42
43
        // is Closed?
44 11
        if (!$issue->isOpen()) {
45 7
            return (new Queue())->queue(Queue::CLOSE_ISSUE, $issue, $changeBy);
46
        }
47
48
        // is Reopened?
49 7
        if ((int) $issue->getOriginal('status') === Issue::STATUS_CLOSED) {
50 2
            return (new Queue())->queue(Queue::REOPEN_ISSUE, $issue, $changeBy);
51
        }
52
53
        // If the assignee has changed and it is not the logged in user who made the action
54 6
        if ($issue->assigned_to !== $issue->getOriginal('assigned_to', $issue->assigned_to)
55 6
            && $changeBy->id !== $issue->assigned_to
56
        ) {
57
            (new Queue())->queue(Queue::ASSIGN_ISSUE, $issue, $changeBy);
58
            $noMessageForMe = $issue->assigned_to;
59
        }
60
61
        // If the update was just for assigning user, then skip update issue
62 6
        if (!($countChanges === 1 && $noMessageForMe !== false)) {
63 6
            return (new Queue())->queue(Queue::UPDATE_ISSUE, $issue, $changeBy);
64
        }
65
    }
66
67
    /**
68
     * Insert add issue to message queue.
69
     *
70
     * @param Issue $issue
71
     * @param User  $changeBy
72
     *
73
     * @return void
74
     */
75 37
    public function queueAdd(Issue $issue, User $changeBy)
76
    {
77 37
        return (new Queue())->queue(Queue::ADD_ISSUE, $issue, $changeBy);
78
    }
79
80
    /**
81
     * Insert assign issue to message queue.
82
     *
83
     * @param Issue $issue
84
     * @param User  $changeBy
85
     *
86
     * @return void
87
     */
88 3
    public function queueAssign(Issue $issue, User $changeBy)
89
    {
90
        // If the assignee has changed and it is not the logged in user who made the action
91 3
        if ($issue->assigned_to > 0 && $changeBy->id !== $issue->assigned_to) {
92 3
            return (new Queue())->queue(Queue::ASSIGN_ISSUE, $issue, $changeBy);
93
        }
94
    }
95
96
    /**
97
     * Insert issue tag changes to message queue.
98
     *
99
     * @param Issue $issue
100
     * @param array $addedTags
101
     * @param array $removedTags
102
     * @param User  $changeBy
103
     *
104
     * @return mixed
105
     */
106 7
    public function queueChangeTags(Issue $issue, array $addedTags, array $removedTags, User $changeBy)
107
    {
108 7
        $queue = new Queue();
109
110 7
        return $queue->queueIssueTagChanges($issue, $addedTags, $removedTags, $changeBy);
111
    }
112
}
113