Completed
Push — develop ( b779dc...323e79 )
by Mohamed
06:59
created

QueueTrait::queueUpdate()   C

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 3
Bugs 1 Features 0
Metric Value
c 3
b 1
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
trait QueueTrait
24
{
25
    /**
26
     * Insert update issue to message queue.
27
     *
28
     * @param Issue $issue
29
     * @param int|User $changeBy
30
     *
31
     * @return void
32
     */
33 6
    public function queueUpdate(Issue $issue, $changeBy)
34
    {
35
        // Number of changed attributes
36 6
        $countChanges = count($issue->getDirty());
37
38
        // Whether or not the assignee has changed
39 6
        $noMessageForMe = false;
40
41
        // is Closed?
42 6
        if (!$issue->isOpen()) {
43 5
            return (new Queue())->queue(Queue::CLOSE_ISSUE, $issue, $changeBy);
44
        }
45
46
        // is Reopened?
47 2
        if ((int) $issue->getOriginal('status') === Issue::STATUS_CLOSED) {
48 1
            return (new Queue())->queue(Queue::REOPEN_ISSUE, $issue, $changeBy);
49
        }
50
51
        // If the assignee has changed and it is not the logged in user who made the action
52 1
        if ($issue->assigned_to !== $issue->getOriginal('assigned_to', $issue->assigned_to)
53 1
            && $changeBy->id !== $issue->assigned_to
54
        ) {
55
            (new Queue())->queue(Queue::ASSIGN_ISSUE, $issue, $changeBy);
56
            $noMessageForMe = $issue->assigned_to;
57
        }
58
59
        // If the update was just for assigning user, then skip update issue
60 1
        if (!($countChanges === 1 && $noMessageForMe !== false)) {
61 1
            return (new Queue())->queue(Queue::UPDATE_ISSUE, $issue, $changeBy);
62
        }
63
    }
64
65
    /**
66
     * Insert add issue to message queue.
67
     *
68
     * @param Issue $issue
69
     * @param int|User $changeBy
70
     *
71
     * @return void
72
     */
73 29
    public function queueAdd(Issue $issue, $changeBy)
74
    {
75 29
        return (new Queue())->queue(Queue::ADD_ISSUE, $issue, $changeBy);
76
    }
77
78
    /**
79
     * Insert assign issue to message queue.
80
     *
81
     * @param Issue $issue
82
     * @param int|User $changeBy
83
     *
84
     * @return void
85
     */
86 3
    public function queueAssign(Issue $issue, $changeBy)
87
    {
88
        // If the assignee has changed and it is not the logged in user who made the action
89 3
        $changeBy = $changeBy instanceof User? $changeBy->id : $changeBy;
90 3
        if ($issue->assigned_to > 0 && $changeBy !== $issue->assigned_to) {
91 3
            return (new Queue())->queue(Queue::ASSIGN_ISSUE, $issue, $changeBy);
92
        }
93
    }
94
95
    /**
96
     * Insert issue tag changes to message queue.
97
     *
98
     * @param Issue $issue
99
     * @param array $addedTags
100
     * @param array $removedTags
101
     * @param int|User $changeBy
102
     *
103
     * @return mixed
104
     */
105 2
    public function queueChangeTags(Issue $issue, array $addedTags, array $removedTags, $changeBy)
106
    {
107 2
        $queue = new Queue();
108
109 2
        return $queue->queueIssueTagChanges($issue, $addedTags, $removedTags, $changeBy);
0 ignored issues
show
Bug introduced by
It seems like $changeBy defined by parameter $changeBy on line 105 can also be of type integer; however, Tinyissue\Model\Traits\M...:queueIssueTagChanges() does only seem to accept object<Tinyissue\Model\User>, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
110
    }
111
}
112