Completed
Push — develop ( cf7e5a...f29fb9 )
by Mohamed
07:01
created

SendMessages::wantToReceiveMessage()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 18
ccs 0
cts 0
cp 0
rs 9.2
cc 4
eloc 9
nc 4
nop 2
crap 20
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 bool
34 1
     */
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 1
    {
37 1
        if (!$this->issue instanceof Issue) {
38 1
            if ($this->getModel()) {
39
                $this->issue = $this->getModel()->issue;
40
            } else {
41 1
                // Possible deleted comment
42 1
                $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 1
47
        return $this->issue;
48
    }
49
50
    /**
51
     * Returns an instance of Project.
52
     *
53
     * @return Project
54 1
     */
55
    protected function getProject()
56 1
    {
57
        return $this->getIssue()->project;
58
    }
59
60
    /**
61
     * Returns the project id.
62
     *
63
     * @return int
64 1
     */
65
    protected function getProjectId()
66 1
    {
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 1
     */
77
    protected function getMessageDataForAddComment(Queue $queue)
78 1
    {
79 1
        $messageData                    = [];
80 1
        $messageData['changeByHeading'] = $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...
81 1
            ' commented on ' . link_to($this->getIssue()->to(), '#' . $this->getIssue()->id);
0 ignored issues
show
Bug introduced by
The method to cannot be called on $this->getIssue() (of type boolean).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
82 1
        $messageData['changes']['comment'] = [
83 1
            'noLabel' => true,
84 1
            'date'    => $this->getModel()->created_at,
85
            'now'     => \Html::format($this->getModel()->comment),
86
        ];
87 1
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 1
     */
98 View Code Duplication
    protected function getMessageDataForUpdateComment(Queue $queue)
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...
99 1
    {
100 1
        $messageData                    = [];
101 1
        $messageData['changeByHeading'] = $queue->changeBy->fullname . ' updated a comment in ' . link_to($this->getIssue()->to(),
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...
Bug introduced by
The method to cannot be called on $this->getIssue() (of type boolean).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
102 1
                '#' . $this->getIssue()->id);
103 1
        $messageData['changes']['comment'] = [
104 1
            'noLabel' => true,
105 1
            'date'    => $queue->getDataFromPayload('origin.updated_at'),
106 1
            'was'     => \Html::format($queue->getDataFromPayload('origin.comment')),
107
            'now'     => \Html::format($queue->getDataFromPayload('dirty.comment')),
108
        ];
109 1
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 1
     */
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 1
    {
122 1
        $messageData                    = [];
123 1
        $messageData['changeByHeading'] = $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...
124
            ' deleted a comment from ' . link_to($this->getIssue()->to(), '#' . $this->getIssue()->id);
0 ignored issues
show
Bug introduced by
The method to cannot be called on $this->getIssue() (of type boolean).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
125 1
126 1
        $messageData['changes']['comment'] = [
127 1
            'noLabel' => true,
128 1
            'date'    => $queue->created_at,
0 ignored issues
show
Documentation introduced by
The property created_at 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...
129 1
            'was'     => \Html::format($queue->getDataFromPayload('origin.comment')),
130
            'now'     => '',
131
        ];
132 1
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 1
     */
144
    protected function getMessageHeading(Queue $queue, Collection $changes = null)
145 1
    {
146 1
        $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...
147
            ' commented on ' . link_to($this->getIssue()->to(), '#' . $this->getIssue()->id);
0 ignored issues
show
Bug introduced by
The method to cannot be called on $this->getIssue() (of type boolean).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
148 1
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 1
     */
157
    protected function validateData()
158 1
    {
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 1
     */
167 View Code Duplication
    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 1
        // Set the relation of assigned in issue object from the fetch data
170 1
        $this->issue->setRelation('assigned', $this->getUserById($this->issue->assigned_to));
171 1
        $creator = $this->getUserById($this->issue->created_by);
172 1
        if ($creator) {
173
            $this->issue->setRelation('user', $creator);
174 1
        }
175 1
        $this->loadIssueCreatorToProjectUsers();
176
    }
177
178
    /**
179
     * Check if the latest message is about deleting a comment.
180
     *
181
     * @return bool
182 1
     */
183
    public function isStatusMessage()
184 1
    {
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)) {
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...
208
                return false;
209
            }
210
        }
211
212
        return true;
213
    }
214
}
215