Completed
Push — master ( 195b94...7a36f1 )
by Mohamed
04:50
created

SendMessages   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 189
Duplicated Lines 15.34 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 3
Bugs 1 Features 0
Metric Value
wmc 19
lcom 1
cbo 5
dl 29
loc 189
rs 10
c 3
b 1
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A getIssue() 14 14 3
A getProject() 0 4 1
A getProjectId() 0 4 1
A getMessageDataForAddComment() 0 13 1
A getMessageDataForUpdateComment() 0 14 1
A getMessageDataForDeleteComment() 15 15 1
A getMessageHeading() 0 7 1
A validateData() 0 4 3
A populateData() 0 10 2
A isStatusMessage() 0 4 1
A wantToReceiveMessage() 0 18 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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\Project\Issue\Comment;
13
14
use Illuminate\Support\Collection;
15
use Tinyissue\Model\Message\Queue;
16
use Tinyissue\Model\Project\Issue;
17
use Tinyissue\Model\Project;
18
use Tinyissue\Model\User;
19
use Tinyissue\Services\SendMessagesAbstract;
20
21
/**
22
 * SendMessages is a class to manage & process of sending messages about comment changes.
23
 *
24
 * @author Mohamed Alsharaf <[email protected]>
25
 */
26
class SendMessages extends SendMessagesAbstract
27
{
28
    protected $template = 'update_comment';
29
30
    /**
31
     * Returns an instance of Issue.
32
     *
33
     * @return Issue
34
     */
35 View Code Duplication
    protected function getIssue()
1 ignored issue
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...
36
    {
37
        if (!$this->issue instanceof Issue) {
38
            if ($this->getModel()) {
39
                $this->issue = $this->getModel()->issue;
40
            } else {
41
                // Possible deleted comment
42
                $issueId     = $this->latestMessage->getDataFromPayload('origin.issue.id');
43
                $this->issue = (new Issue())->find($issueId);
0 ignored issues
show
Documentation Bug introduced by
The method find does not exist on object<Tinyissue\Model\Project\Issue>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
44
            }
45
        }
46
47
        return $this->issue;
48
    }
49
50
    /**
51
     * Returns an instance of Project.
52
     *
53
     * @return Project
54
     */
55
    protected function getProject()
56
    {
57
        return $this->getIssue()->project;
58
    }
59
60
    /**
61
     * Returns the project id.
62
     *
63
     * @return int
64
     */
65
    protected function getProjectId()
66
    {
67
        return $this->getIssue()->project_id;
68
    }
69
70
    /**
71
     * Returns message data belongs to adding a comment.
72
     *
73
     * @param Queue $queue
74
     *
75
     * @return array
76
     */
77
    protected function getMessageDataForAddComment(Queue $queue)
78
    {
79
        $messageData                    = [];
80
        $messageData['changeByHeading'] = $queue->changeBy->fullname .
81
            ' commented on ' . link_to($this->getIssue()->to(), '#' . $this->getIssue()->id);
82
        $messageData['changes']['comment'] = [
83
            'noLabel' => true,
84
            'date'    => $this->getModel()->created_at,
85
            'now'     => \Html::format($this->getModel()->comment),
86
        ];
87
88
        return $messageData;
89
    }
90
91
    /**
92
     * Returns message data belongs to updating a comment.
93
     *
94
     * @param Queue $queue
95
     *
96
     * @return array
97
     */
98
    protected function getMessageDataForUpdateComment(Queue $queue)
99
    {
100
        $messageData                    = [];
101
        $messageData['changeByHeading'] = $queue->changeBy->fullname . ' updated a comment in ' . link_to($this->getIssue()->to(),
102
                '#' . $this->getIssue()->id);
103
        $messageData['changes']['comment'] = [
104
            'noLabel' => true,
105
            'date'    => $queue->getDataFromPayload('origin.updated_at'),
106
            'was'     => \Html::format($queue->getDataFromPayload('origin.comment')),
107
            'now'     => \Html::format($queue->getDataFromPayload('dirty.comment')),
108
        ];
109
110
        return $messageData;
111
    }
112
113
    /**
114
     * Returns message data belongs to deleting a note.
115
     *
116
     * @param Queue $queue
117
     *
118
     * @return array
119
     */
120 View Code Duplication
    protected function getMessageDataForDeleteComment(Queue $queue)
1 ignored issue
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...
121
    {
122
        $messageData                    = [];
123
        $messageData['changeByHeading'] = $queue->changeBy->fullname .
124
            ' deleted a comment from ' . link_to($this->getIssue()->to(), '#' . $this->getIssue()->id);
125
126
        $messageData['changes']['comment'] = [
127
            'noLabel' => true,
128
            'date'    => $queue->created_at,
129
            'was'     => \Html::format($queue->getDataFromPayload('origin.comment')),
130
            'now'     => '',
131
        ];
132
133
        return $messageData;
134
    }
135
136
    /**
137
     * Return text to be used for the message heading.
138
     *
139
     * @param Queue           $queue
140
     * @param Collection|null $changes
141
     *
142
     * @return string
143
     */
144
    protected function getMessageHeading(Queue $queue, Collection $changes = null)
145
    {
146
        $heading = $queue->changeBy->fullname .
147
            ' commented on ' . link_to($this->getIssue()->to(), '#' . $this->getIssue()->id);
148
149
        return $heading;
150
    }
151
152
    /**
153
     * Check that the issue comment is loaded and the comment is belongs to the issue.
154
     *
155
     * @return bool
156
     */
157
    protected function validateData()
158
    {
159
        return $this->getIssue() && $this->getModel() && $this->getModel()->issue_id === $this->getIssue()->id;
160
    }
161
162
    /**
163
     * Populate assigned relation in the current issue.
164
     *
165
     * @return void
166
     */
167
    protected function populateData()
1 ignored issue
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...
168
    {
169
        // Set the relation of assigned in issue object from the fetch data
170
        $this->issue->setRelation('assigned', $this->getUserById($this->issue->assigned_to));
171
        $creator = $this->getUserById($this->issue->created_by);
172
        if ($creator) {
173
            $this->issue->setRelation('user', $creator);
174
        }
175
        $this->loadIssueCreatorToProjectUsers();
176
    }
177
178
    /**
179
     * Check if the latest message is about deleting a comment.
180
     *
181
     * @return bool
182
     */
183
    public function isStatusMessage()
184
    {
185
        return $this->latestMessage->event === Queue::DELETE_COMMENT;
186
    }
187
188
    /**
189
     * Whether or not the user wants to receive the message.
190
     *
191
     * @param Project\User $user
192
     * @param array        $data
193
     *
194
     * @return bool
195
     */
196
    protected function wantToReceiveMessage(Project\User $user, array $data)
197
    {
198
        $status = parent::wantToReceiveMessage($user, $data);
199
200
        if (!$status) {
201
            return false;
202
        }
203
204
        // Check if user allowed to receive the message
205
        $tags = $this->getIssue()->tags;
206
        foreach ($tags as $tag) {
207
            if (!$tag->allowMessagesToUser($user->user)) {
208
                return false;
209
            }
210
        }
211
212
        return true;
213
    }
214
}
215