Completed
Push — develop ( fea319...7a0090 )
by Mohamed
07:15
created

SendMessagesAbstract   C

Complexity

Total Complexity 55

Size/Duplication

Total Lines 573
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Test Coverage

Coverage 92.59%

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 55
c 2
b 1
f 0
lcom 1
cbo 9
dl 0
loc 573
ccs 150
cts 162
cp 0.9259
rs 6.8

26 Methods

Rating   Name   Duplication   Size   Complexity  
A setMailer() 0 6 1
validateData() 0 1 ?
isStatusMessage() 0 1 ?
A getSubject() 0 4 1
B getMessageData() 0 27 3
A getCombineMessageData() 0 19 2
A getMessageHeading() 0 11 3
A getProjectUsers() 0 12 2
A getModel() 0 4 1
getIssue() 0 1 ?
getProject() 0 1 ?
getProjectId() 0 1 ?
A getExcludeUsers() 0 8 2
A addToExcludeUsers() 0 6 1
A getUserById() 0 10 2
A getMessages() 0 8 2
A sendMessage() 0 11 3
B getUserMessageData() 0 31 4
D wantToReceiveMessage() 0 35 10
A setup() 0 18 2
A processDirectMessages() 0 3 1
A populateData() 0 3 1
A sendMessages() 0 11 3
A sendMessageToAll() 0 6 1
B loadIssueCreatorToProjectUsers() 0 31 4
B process() 0 40 6

How to fix   Complexity   

Complex Class

Complex classes like SendMessagesAbstract often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use SendMessagesAbstract, and based on these observations, apply Extract Interface, too.

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\Services;
13
14
use Illuminate\Mail\Message as MailMessage;
15
use Illuminate\Support\Collection;
16
use Illuminate\Contracts\Mail\Mailer;
17
use Tinyissue\Http\Requests\FormRequest\Note;
18
use Tinyissue\Model\Message;
19
use Tinyissue\Model\Project;
20
use Tinyissue\Model\Project\Issue;
21
use Tinyissue\Model\User;
22
23
/**
24
 * SendMessagesAbstract is an abstract class with for objects that requires sending messages.
25
 *
26
 * @author Mohamed Alsharaf <[email protected]>
27
 */
28
abstract class SendMessagesAbstract
29
{
30
    /**
31
     * Instance of issue that this message belong to.
32
     *
33
     * @var Issue
34
     */
35
    protected $issue;
36
37
    /**
38
     * Instance of project that this message belong to.
39
     *
40
     * @var Issue
41
     */
42
    protected $project;
43
44
    /**
45
     * The latest message queued.
46
     *
47
     * @var Message\Queue
48
     */
49
    protected $latestMessage;
50
51
    /**
52
     * Collection of all of the queued messages.
53
     *
54
     * @var Collection
55
     */
56
    protected $allMessages;
57
58
    /**
59
     * Instance of a queued message that is for adding a record (ie. adding issue).
60
     *
61
     * @var Message\Queue
62
     */
63
    protected $addMessage;
64
    /**
65
     * Collection of users that must not receive messages.
66
     *
67
     * @var Collection
68
     */
69
    protected $excludeUsers;
70
    /**
71
     * Collection of all of the project users that should receive messages.
72
     *
73
     * @var Collection
74
     */
75
    protected $projectUsers;
76
    /**
77
     * Collection of full subscribers that will always receive messages.
78
     *
79
     * @var Collection
80
     */
81
    protected $fullSubscribers;
82
    /**
83
     * Name of message template.
84
     *
85
     * @var string
86
     */
87
    protected $template;
88
89
    /**
90
     * @var Mailer
91
     */
92
    protected $mailer;
93
94
    /**
95
     * Collection of all messages.
96
     *
97
     * @var Collection
98
     */
99
    protected $messages;
100
101
    /**
102
     * Set instance of Mailer.
103
     *
104
     * @param Mailer $mailer
105
     *
106
     * @return $this
107
     */
108 4
    public function setMailer(Mailer $mailer)
109
    {
110 4
        $this->mailer = $mailer;
111
112 4
        return $this;
113
    }
114
115
    /**
116
     * The main method to process the massages queue and send them.
117
     *
118
     * @param Message\Queue $latestMessage
119
     * @param Collection    $changes
120
     *
121
     * @return void
122
     */
123 4
    public function process(Message\Queue $latestMessage, Collection $changes)
124
    {
125 4
        $this->setup($latestMessage, $changes);
126
127
        // Is model deleted?
128 4
        if (!$this->getModel()) {
129 2
            return $this->sendMessageToAll($this->latestMessage);
130
        }
131
132 4
        if (!$this->validateData()) {
133
            return;
134
        }
135
136 4
        $this->processDirectMessages();
137
138
        // Skip if no users found
139 4
        if ($this->getProjectUsers()->isEmpty()) {
140
            return;
141
        }
142
143 4
        $this->populateData();
144
145
        // Send the latest message if it is about status (ie. closed issue)
146 4
        if ($this->isStatusMessage()) {
147
            return $this->sendMessageToAll($this->latestMessage);
148
        }
149
150
        // Get message data for all of the messages combined & for add message queue if exists
151 4
        $addMessageData = [];
152 4
        if ($this->addMessage) {
153 4
            $addMessageData = $this->getMessageData($this->addMessage);
154
        }
155 4
        $everythingMessageData = $this->getCombineMessageData($this->allMessages);
156
157
        // Send messages to project users
158 4
        $this->sendMessages($this->getProjectUsers(), [
159 4
            'addMessage' => $addMessageData,
160 4
            'everything' => $everythingMessageData,
161
        ]);
162 4
    }
163
164
    /**
165
     * Setup properties needed for the process.
166
     *
167
     * @param Message\Queue $latestMessage
168
     * @param Collection    $allMessages
169
     *
170
     * @return void
171
     */
172 4
    protected function setup(Message\Queue $latestMessage, Collection $allMessages)
173
    {
174
        // Set queue messages
175 4
        $this->latestMessage = $latestMessage;
176 4
        $this->allMessages   = $allMessages;
177
178
        // Exclude the user who made the change from receiving messages
179 4
        $this->addToExcludeUsers($this->latestMessage->changeBy);
0 ignored issues
show
Documentation introduced by
$this->latestMessage->changeBy is of type object<Tinyissue\Model\Project\User>, but the function expects a object<Tinyissue\Model\User>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
180
181
        // Extract add model message if exists
182 4
        if ($this->getModel()) {
183 4
            $addMessageIdentifier = Message\Queue::getAddEventNameFromModel($this->getModel());
184 4
            $this->addMessage     = $this->allMessages->where('event', $addMessageIdentifier)->first();
185
        }
186
187
        // Make sure to load issue
188 4
        $this->getIssue();
189 4
    }
190
191
    /**
192
     * Whether or not we have all the needed properties.
193
     *
194
     * @return bool
195
     */
196
    abstract protected function validateData();
197
198
    /**
199
     * Process any messages queue that is to send messages to specific users.
200
     * For example, assign issue to user to message the user about the issue.
201
     *
202
     * @return void
203
     */
204 2
    protected function processDirectMessages()
205
    {
206 2
    }
207
208
    /**
209
     * Populate any data or properties.
210
     *
211
     * @return void
212
     */
213 1
    protected function populateData()
214
    {
215 1
    }
216
217
    /**
218
     * Whether or not the latest message is about status change such as closed issue.
219
     *
220
     * @return bool
221
     */
222
    abstract public function isStatusMessage();
223
224
    /**
225
     * Returns the message subject.
226
     *
227
     * @return string
228
     */
229 3
    protected function getSubject()
230
    {
231 3
        return '#' . $this->issue->id . ' / ' . $this->issue->title;
232
    }
233
234
    /**
235
     * Returns an array of data needed for the message.
236
     *
237
     * @param Message\Queue $queue
238
     * @param array         $extraData
239
     *
240
     * @return array
241
     */
242 4
    protected function getMessageData(Message\Queue $queue, array $extraData = [])
243
    {
244
        // Generic info for all messages emails
245 4
        $messageData                         = [];
246 4
        $messageData['issue']                = $this->getIssue();
247 4
        $messageData['project']              = $this->getProject();
248 4
        $messageData['changes']              = [];
249 4
        $messageData['changes']['change_by'] = [
250 4
            'now' => $queue->changeBy->fullname,
0 ignored issues
show
Documentation introduced by
The property fullname does not exist on object<Tinyissue\Model\Project\User>. 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...
251
        ];
252 4
        if ($this->getIssue()) {
253 3
            $messageData['changes']['change_by']['url'] = $this->getIssue()->to();
254
        } else {
255 1
            $messageData['changes']['change_by']['url'] = $this->getProject()->to();
256
        }
257 4
        $messageData['changeByImage']   = $queue->changeBy->image;
0 ignored issues
show
Documentation introduced by
The property image does not exist on object<Tinyissue\Model\Project\User>. 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...
258 4
        $messageData['changeByHeading'] = $this->getMessageHeading($queue);
259 4
        $messageData['event']           = $queue->event;
260
261
        // Info specific to a message type
262 4
        $method = 'getMessageDataFor' . ucfirst(camel_case($queue->event));
263 4
        if (method_exists($this, $method)) {
264 4
            $messageData = array_replace_recursive($messageData, $this->{$method}($queue, $extraData));
265
        }
266
267 4
        return $messageData;
268
    }
269
270
    /**
271
     * Loop through all of the messages and combine its message data.
272
     *
273
     * @param Collection $changes
274
     *
275
     * @return array
276
     */
277 4
    protected function getCombineMessageData(Collection $changes)
278
    {
279 4
        $everything = [];
280
        $changes->reverse()->each(function (Message\Queue $queue) use (&$everything) {
281 4
            if (!$everything) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $everything of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
282 4
                $everything = $this->getMessageData($queue);
283
            } else {
284 4
                $messageData = $this->getMessageData($queue);
285 4
                $everything['changes'] = array_merge($everything['changes'], $messageData['changes']);
286
            }
287 4
        });
288 4
        $latestMessage                 = $changes->first();
289 4
        $everything['changeByHeading'] = $this->getMessageHeading($latestMessage, $changes);
290 4
        $everything['event']           = $latestMessage->event;
291 4
        $messageData                   = $this->getMessageData($latestMessage);
292 4
        $everything                    = array_replace_recursive($everything, $messageData);
293
294 4
        return $everything;
295
    }
296
297
    /**
298
     * Return text to be used for the message heading.
299
     *
300
     * @param Message\Queue   $queue
301
     * @param Collection|null $changes
302
     *
303
     * @return string
304
     */
305 4
    protected function getMessageHeading(Message\Queue $queue, Collection $changes = null)
306
    {
307 4
        $heading = $queue->changeBy->fullname . ' ';
0 ignored issues
show
Documentation introduced by
The property fullname does not exist on object<Tinyissue\Model\Project\User>. 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...
308
309
        // If other users have made changes too
310 4
        if (!is_null($changes) && $changes->unique('change_by_id')->count() > 1) {
311 1
            $heading .= '& others ';
312
        }
313
314 4
        return $heading;
315
    }
316
317
    /**
318
     * Returns collection of all users in a project that should receive the messages.
319
     *
320
     * @return Collection
321
     */
322 4
    protected function getProjectUsers()
323
    {
324 4
        if (null === $this->projectUsers) {
325 4
            $this->projectUsers = (new Project\User())
326 4
                ->with('message', 'user', 'user.role')
327 4
                ->whereNotIn('user_id', $this->getExcludeUsers()->lists('id'))
328 4
                ->where('project_id', '=', $this->getProjectId())
329 4
                ->get();
330
        }
331
332 4
        return $this->projectUsers;
333
    }
334
335
    /**
336
     * Returns the model that is belong to the queue message.
337
     *
338
     * @return \Illuminate\Database\Eloquent\Model
339
     */
340 4
    protected function getModel()
341
    {
342 4
        return $this->latestMessage->model;
0 ignored issues
show
Documentation introduced by
The property model does not exist on object<Tinyissue\Model\Message\Queue>. 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...
343
    }
344
345
    /**
346
     * Returns an instance of project issue.
347
     *
348
     * @return Issue
349
     */
350
    abstract protected function getIssue();
351
352
    /**
353
     * Returns an instance of project.
354
     *
355
     * @return Project
356
     */
357
    abstract protected function getProject();
358
359
    /**
360
     * Returns the id of a project.
361
     *
362
     * @return int
363
     */
364
    abstract protected function getProjectId();
365
366
    /**
367
     * Returns collection of all of the users that must not receive messages.
368
     *
369
     * @return Collection
370
     */
371 4
    protected function getExcludeUsers()
372
    {
373 4
        if (null === $this->excludeUsers) {
374 4
            $this->excludeUsers = collect([]);
375
        }
376
377 4
        return $this->excludeUsers;
378
    }
379
380
    /**
381
     * Exclude a user from receiving messages.
382
     *
383
     * @param User $user
384
     *
385
     * @return $this
386
     */
387 4
    protected function addToExcludeUsers(User $user)
388
    {
389 4
        $this->getExcludeUsers()->push($user);
390
391 4
        return $this;
392
    }
393
394
    /**
395
     * Find user by id. This search the project users and fallback to excluded list of users.
396
     *
397
     * @param int $userId
398
     *
399
     * @return User
400
     */
401 3
    protected function getUserById($userId)
402
    {
403 3
        $projectUser = $this->getProjectUsers()->where('user_id', $userId, false)->first();
404
405 3
        if (!$projectUser) {
406 3
            return $this->getExcludeUsers()->where('id', $userId, false)->first();
407
        }
408
409 1
        return $projectUser->user;
410
    }
411
412
    /**
413
     * Returns collection of all messages.
414
     *
415
     * @return Collection
416
     */
417 4
    protected function getMessages()
418
    {
419 4
        if (null === $this->messages) {
420 4
            $this->messages = (new Message())->orderBy('id', 'ASC')->get();
0 ignored issues
show
Documentation Bug introduced by
The method orderBy does not exist on object<Tinyissue\Model\Message>? 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...
421
        }
422
423 4
        return $this->messages;
424
    }
425
426
    /**
427
     * Send a message to a user.
428
     *
429
     * @param User  $user
430
     * @param array $data
431
     *
432
     * @return mixed
433
     */
434 4
    private function sendMessage(User $user, array $data)
435
    {
436
        // Make sure the data contains changes
437 4
        if (!array_key_exists('changes', $data) && count($data['changes']) > 1) {
438
            return;
439
        }
440
441 4
        return $this->mailer->send('email.' . $this->template, $data, function (MailMessage $message) use ($user) {
442 4
            $message->to($user->email, $user->fullname)->subject($this->getSubject());
443 4
        });
444
    }
445
446
    /**
447
     * Send a message to a collection of users, or send customised message per use logic.
448
     *
449
     * @param Collection $users
450
     * @param array      $data
451
     *
452
     * @return void
453
     */
454 4
    protected function sendMessages(Collection $users, array $data)
455
    {
456 4
        foreach ($users as $user) {
457 4
            $userMessageData = $this->getUserMessageData($user->user_id, $data);
458 4
            if (!$this->wantToReceiveMessage($user, $userMessageData)) {
459 4
                continue;
460
            }
461
462 4
            $this->sendMessage($user->user, $userMessageData);
463
        }
464 4
    }
465
466
    /**
467
     * Get customised message per user logic.
468
     *
469
     * @param int   $userId
470
     * @param array $messagesData
471
     *
472
     * @return array
473
     */
474 4
    protected function getUserMessageData($userId, array $messagesData)
475
    {
476 4
        if (array_key_exists('event', $messagesData)) {
477 2
            return $messagesData;
478
        }
479
480
        // Possible message data
481 4
        $addMessageData        = $messagesData['addMessage'];
482 4
        $everythingMessageData = $messagesData['everything'];
483
484
        // Check if the user has seen the model data and made a change
485 4
        $changeMadeByUser = $this->allMessages->where('change_by_id', $userId);
486
487
        // This user has never seen this model data
488 4
        if (!$changeMadeByUser->count()) {
489 4
            if ($this->addMessage) {
490 4
                return $addMessageData;
491
            }
492
493 2
            return $everythingMessageData;
494
        }
495
496
        // This user has seen this model data
497
        // Get all of the changes that may happened later.
498
        // Combine them and send message to the user about these changes.
499 1
        $everythingMessageData = $this->getCombineMessageData(
500 1
            $this->allMessages->forget($changeMadeByUser->keys()->toArray())
501
        );
502
503 1
        return $everythingMessageData;
504
    }
505
506
    /**
507
     * Whether or not the user wants to receive the message.
508
     *
509
     * @param Project\User $user
510
     * @param array        $data
511
     *
512
     * @return bool
513
     */
514 4
    protected function wantToReceiveMessage(Project\User $user, array $data)
515
    {
516
        /** @var Message $message */
517 4
        $message = $user->message;
0 ignored issues
show
Bug introduced by
The property message does not seem to exist. Did you mean message_id?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
518 4
        if (!$message) {
519 4
            $roleName = $user->user->role->role;
0 ignored issues
show
Documentation introduced by
The property user does not exist on object<Tinyissue\Model\Project\User>. 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...
520 4
            $message  = $this->getMessages()->where('name', Message::$defaultMessageToRole[$roleName])->first();
521
        }
522
523
        // No message to send,
524
        // - if we can't find message object or
525
        // - messages are disabled or
526
        // - event is inactive for the user message setting
527 4
        if (!$message || $message->isDisabled() || !$message->isActiveEvent($data['event'])) {
528 2
            return false;
529
        }
530
531
        // Wants to see all updates in project
532 4
        if ((bool) $message->in_all_issues === true) {
533 4
            return true;
534
        }
535
536 4
        if (!$this->getIssue()) {
537 1
            return false;
538
        }
539
540
        // For issue only send messages if user is assignee or creator
541 3
        $creator  = $this->getIssue()->user;
542 3
        $assignee = $this->getIssue()->assigned;
0 ignored issues
show
Bug introduced by
The property assigned does not seem to exist. Did you mean assigned_to?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
543 3
        if ($user->user_id === $creator->id || ($assignee && $user->user_id === $assignee->id)) {
544 1
            return true;
545
        }
546
547 3
        return false;
548
    }
549
550
    /**
551
     * Send a message to al users in project and full subscribes.
552
     *
553
     * @param Message\Queue $queue
554
     *
555
     * @return void
556
     */
557 2
    protected function sendMessageToAll(Message\Queue $queue)
558
    {
559 2
        $messageData = $this->getMessageData($queue);
560
561 2
        $this->sendMessages($this->getProjectUsers(), $messageData);
562 2
    }
563
564
    /**
565
     * Load the creator of an issue to the collection of project users. So we can send message to creator if needed.
566
     *
567
     * @return void
568
     */
569 3
    protected function loadIssueCreatorToProjectUsers()
570
    {
571
        // Stop if we can't get the issue
572 3
        if (!$this->getIssue()) {
573
            return;
574
        }
575
576
        // Get issue creator
577 3
        $creator = $this->getIssue()->user;
578
579
        // Stop if creator excluded from messages
580 3
        $excluded = $this->getExcludeUsers()->where('id', $creator->id, false)->first();
581 3
        if ($excluded) {
582 3
            return;
583
        }
584
585
        // Stop if the creator already part of the project users
586 1
        $existInProject = $this->getProjectUsers()->where('user_id', $creator->id, false)->first();
587 1
        if ($existInProject) {
588 1
            return;
589
        }
590
591
        // Create virtual project user object & add to collection
592
        $userProject = new Project\User([
593
            'user_id'    => $creator->id,
594
            'project_id' => $this->getProjectId(),
595
        ]);
596
        $userProject->setRelation('user', $creator);
597
        $userProject->setRelation('project', $this->getProject());
598
        $this->getProjectUsers()->push($userProject);
599
    }
600
}
601