Completed
Push — develop ( 671a92...719295 )
by Mohamed
10:02
created

SendMessages::getMessageDataForDeleteNote()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 9

Duplication

Lines 13
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 13
loc 13
ccs 0
cts 0
cp 0
rs 9.4285
cc 1
eloc 9
nc 1
nop 1
crap 2
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\Note;
13
14
use Tinyissue\Model\Project;
15
use Tinyissue\Model\Project\Issue;
16
use Tinyissue\Model\Message\Queue;
17
use Illuminate\Support\Collection;
18
use Tinyissue\Model\User;
19
use Tinyissue\Services\SendMessagesAbstract;
20
21
/**
22
 * SendMessages is a class to manage & process of sending messages about note changes.
23
 *
24
 * @author Mohamed Alsharaf <[email protected]>
25
 */
26
class SendMessages extends SendMessagesAbstract
27
{
28
    protected $template = 'update_note';
29
30
    /**
31
     * Returns the subject text.
32
     *
33
     * @return string
34
     */
35 1
    protected function getSubject()
36
    {
37 1
        return $this->getProject()->name;
38
    }
39
40
    /**
41
     * Note: changes is belongs to the project (no issue here).
42
     *
43
     * @return bool
44
     */
45 1
    protected function getIssue()
46
    {
47 1
        return false;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type declared by the abstract method Tinyissue\Services\SendMessagesAbstract::getIssue of type Tinyissue\Model\Project\Issue.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
48
    }
49
50
    /**
51
     * Returns an instance of Project.
52
     *
53
     * @return Project
54
     */
55 1 View Code Duplication
    protected function getProject()
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...
56
    {
57 1
        if ($this->getModel()) {
58 1
            return $this->getModel()->project;
59
        }
60
61
        // Possible deleted note
62
        if (null === $this->project) {
63
            $projectId     = $this->latestMessage->getDataFromPayload('origin.project_id');
64
            $this->project = (new Project())->find($projectId);
0 ignored issues
show
Documentation Bug introduced by
The method find does not exist on object<Tinyissue\Model\Project>? 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...
65
        }
66
67
        return $this->project;
68
    }
69
70
    /**
71
     * Returns the project id.
72
     *
73
     * @return int
74
     */
75 1
    protected function getProjectId()
76
    {
77 1
        return $this->getProject()->id;
78
    }
79
80
    /**
81
     * Returns message data belongs to adding a note.
82
     *
83
     * @param Queue $queue
84
     *
85
     * @return array
86
     */
87 1
    protected function getMessageDataForAddNote(Queue $queue)
88
    {
89 1
        $messageData                    = [];
90 1
        $messageData['changeByHeading'] = $queue->changeBy->fullname . ' added a note';
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...
91 1
        $messageData['changes']['note'] = [
92 1
            'noLabel' => true,
93 1
            'date'    => $this->getModel()->created_at,
94 1
            'now'     => \Html::format($this->getModel()->body),
95
        ];
96
97 1
        return $messageData;
98
    }
99
100
    /**
101
     * Returns message data belongs to updating a note.
102
     *
103
     * @param Queue $queue
104
     *
105
     * @return array
106
     */
107 1
    protected function getMessageDataForUpdateNote(Queue $queue)
108
    {
109 1
        $messageData                    = [];
110 1
        $messageData['changeByHeading'] = $queue->changeBy->fullname . ' updated a note in ' . link_to($this->getProject()->to(), $this->getProject()->name);
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...
111 1
        $messageData['changes']['note'] = [
112 1
            'noLabel' => true,
113 1
            'date'    => $queue->getDataFromPayload('origin.updated_at'),
114 1
            'was'     => \Html::format($queue->getDataFromPayload('origin.body')),
115 1
            'now'     => \Html::format($queue->getDataFromPayload('dirty.body')),
116
        ];
117
118 1
        return $messageData;
119
    }
120
121
    /**
122
     * Returns message data belongs to deleting a note.
123
     *
124
     * @param Queue $queue
125
     *
126
     * @return array
127
     */
128 View Code Duplication
    protected function getMessageDataForDeleteNote(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...
129
    {
130
        $messageData                    = [];
131
        $messageData['changeByHeading'] = $queue->changeBy->fullname . ' deleted a note in ' . link_to($this->getProject()->to(), $this->getProject()->name);
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...
132
        $messageData['changes']['note'] = [
133
            'noLabel' => true,
134
            '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...
135
            'was'     => \Html::format($queue->getDataFromPayload('origin.body')),
136
            'now'     => '',
137
        ];
138
139
        return $messageData;
140
    }
141
142
    /**
143
     * Return text to be used for the message heading.
144
     *
145
     * @param Queue           $queue
146
     * @param Collection|null $changes
147
     *
148
     * @return string
149
     */
150 1
    protected function getMessageHeading(Queue $queue, Collection $changes = null)
151
    {
152 1
        $heading = parent::getMessageHeading($queue, $changes);
153 1
        $heading .= ' updated a note in ' . link_to($this->getProject()->to(), '#' . $this->getProjectId());
154
155 1
        return $heading;
156
    }
157
158
    /**
159
     * Check that the project is loaded and the note is belongs to the project.
160
     *
161
     * @return bool
162
     */
163 1
    protected function validateData()
164
    {
165 1
        return $this->getProject() && $this->getModel() && $this->getModel()->project_id === $this->getProject()->id;
166
    }
167
168
    /**
169
     * Check if the latest message is about deleting a note.
170
     *
171
     * @return bool
172
     */
173 1
    public function isStatusMessage()
174
    {
175 1
        return $this->latestMessage->event === Queue::DELETE_NOTE;
176
    }
177
}
178