TaskEventBuilder::buildTitleWithoutAuthor()   C
last analyzed

Complexity

Conditions 12
Paths 12

Size

Total Lines 29
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 12
eloc 25
nc 12
nop 2
dl 0
loc 29
rs 5.1612
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/*
4
 * This file is part of Jitamin.
5
 *
6
 * Copyright (C) Jitamin Team
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 Jitamin\Bus\EventBuilder;
13
14
use Jitamin\Bus\Event\TaskEvent;
15
use Jitamin\Model\TaskModel;
16
17
/**
18
 * Class TaskEventBuilder.
19
 */
20
class TaskEventBuilder extends BaseEventBuilder
21
{
22
    /**
23
     * TaskId.
24
     *
25
     * @var int
26
     */
27
    protected $taskId = 0;
28
29
    /**
30
     * Task.
31
     *
32
     * @var array
33
     */
34
    protected $task = [];
35
36
    /**
37
     * Extra values.
38
     *
39
     * @var array
40
     */
41
    protected $values = [];
42
43
    /**
44
     * Changed values.
45
     *
46
     * @var array
47
     */
48
    protected $changes = [];
49
50
    /**
51
     * Set TaskId.
52
     *
53
     * @param int $taskId
54
     *
55
     * @return $this
56
     */
57
    public function withTaskId($taskId)
58
    {
59
        $this->taskId = $taskId;
60
61
        return $this;
62
    }
63
64
    /**
65
     * Set task.
66
     *
67
     * @param array $task
68
     *
69
     * @return $this
70
     */
71
    public function withTask(array $task)
72
    {
73
        $this->task = $task;
74
75
        return $this;
76
    }
77
78
    /**
79
     * Set values.
80
     *
81
     * @param array $values
82
     *
83
     * @return $this
84
     */
85
    public function withValues(array $values)
86
    {
87
        $this->values = $values;
88
89
        return $this;
90
    }
91
92
    /**
93
     * Set changes.
94
     *
95
     * @param array $changes
96
     *
97
     * @return $this
98
     */
99
    public function withChanges(array $changes)
100
    {
101
        $this->changes = $changes;
102
103
        return $this;
104
    }
105
106
    /**
107
     * Build event data.
108
     *
109
     * @return TaskEvent|null
110
     */
111
    public function buildEvent()
112
    {
113
        $eventData = [];
114
        $eventData['task_id'] = $this->taskId;
115
        $eventData['task'] = $this->taskFinderModel->getDetails($this->taskId);
0 ignored issues
show
Documentation introduced by
The property taskFinderModel does not exist on object<Jitamin\Bus\EventBuilder\TaskEventBuilder>. 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...
116
117
        if (empty($eventData['task'])) {
118
            $this->logger->debug(__METHOD__.': Task not found');
0 ignored issues
show
Documentation introduced by
The property logger does not exist on object<Jitamin\Bus\EventBuilder\TaskEventBuilder>. 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...
119
120
            return;
121
        }
122
123
        if (!empty($this->changes)) {
124
            if (empty($this->task)) {
125
                $this->task = $eventData['task'];
126
            }
127
128
            $eventData['changes'] = array_diff_assoc($this->changes, $this->task);
129
            unset($eventData['changes']['date_modification']);
130
        }
131
132
        return new TaskEvent(array_merge($eventData, $this->values));
133
    }
134
135
    /**
136
     * Get event title with author.
137
     *
138
     * @param string $author
139
     * @param string $eventName
140
     * @param array  $eventData
141
     *
142
     * @return string
143
     */
144
    public function buildTitleWithAuthor($author, $eventName, array $eventData)
145
    {
146
        switch ($eventName) {
147
            case TaskModel::EVENT_ASSIGNEE_CHANGE:
148
                $assignee = $eventData['task']['assignee_name'] ?: $eventData['task']['assignee_username'];
149
150
                if (!empty($assignee)) {
151
                    return l('%s changed the assignee of the task #%d to %s', $author, $eventData['task']['id'], $assignee);
152
                }
153
154
                return l('%s removed the assignee of the task %s', $author, l('#%d', $eventData['task']['id']));
155
            case TaskModel::EVENT_UPDATE:
156
                return l('%s updated the task #%d', $author, $eventData['task']['id']);
157
            case TaskModel::EVENT_CREATE:
158
                return l('%s created the task #%d', $author, $eventData['task']['id']);
159
            case TaskModel::EVENT_CLOSE:
160
                return l('%s closed the task #%d', $author, $eventData['task']['id']);
161
            case TaskModel::EVENT_OPEN:
162
                return l('%s opened the task #%d', $author, $eventData['task']['id']);
163
            case TaskModel::EVENT_MOVE_COLUMN:
164
                return l(
165
                    '%s moved the task #%d to the column "%s"',
166
                    $author,
167
                    $eventData['task']['id'],
168
                    $eventData['task']['column_title']
169
                );
170
            case TaskModel::EVENT_MOVE_POSITION:
171
                return l(
172
                    '%s moved the task #%d to the position %d in the column "%s"',
173
                    $author,
174
                    $eventData['task']['id'],
175
                    $eventData['task']['position'],
176
                    $eventData['task']['column_title']
177
                );
178
            case TaskModel::EVENT_MOVE_SWIMLANE:
179
                if ($eventData['task']['swimlane_id'] == 0) {
180
                    return l('%s moved the task #%d to the first swimlane', $author, $eventData['task']['id']);
181
                }
182
183
                return l(
184
                    '%s moved the task #%d to the swimlane "%s"',
185
                    $author,
186
                    $eventData['task']['id'],
187
                    $eventData['task']['swimlane_name']
188
                );
189
190
            case TaskModel::EVENT_USER_MENTION:
191
                return l('%s mentioned you in the task #%d', $author, $eventData['task']['id']);
192
            default:
193
                return '';
194
        }
195
    }
196
197
    /**
198
     * Get event title without author.
199
     *
200
     * @param string $eventName
201
     * @param array  $eventData
202
     *
203
     * @return string
204
     */
205
    public function buildTitleWithoutAuthor($eventName, array $eventData)
206
    {
207
        switch ($eventName) {
208
            case TaskModel::EVENT_CREATE:
209
                return l('New task #%d: %s', $eventData['task']['id'], $eventData['task']['title']);
210
            case TaskModel::EVENT_UPDATE:
211
                return l('Task updated #%d', $eventData['task']['id']);
212
            case TaskModel::EVENT_CLOSE:
213
                return l('Task #%d closed', $eventData['task']['id']);
214
            case TaskModel::EVENT_OPEN:
215
                return l('Task #%d opened', $eventData['task']['id']);
216
            case TaskModel::EVENT_MOVE_COLUMN:
217
                return l('Column changed for task #%d', $eventData['task']['id']);
218
            case TaskModel::EVENT_MOVE_POSITION:
219
                return l('New position for task #%d', $eventData['task']['id']);
220
            case TaskModel::EVENT_MOVE_SWIMLANE:
221
                return l('Swimlane changed for task #%d', $eventData['task']['id']);
222
            case TaskModel::EVENT_ASSIGNEE_CHANGE:
223
                return l('Assignee changed on task #%d', $eventData['task']['id']);
224
            case TaskModel::EVENT_OVERDUE:
225
                $nb = count($eventData['tasks']);
226
227
                return $nb > 1 ? l('%d overdue tasks', $nb) : l('Task #%d is overdue', $eventData['tasks'][0]['id']);
228
            case TaskModel::EVENT_USER_MENTION:
229
                return l('You were mentioned in the task #%d', $eventData['task']['id']);
230
            default:
231
                return '';
232
        }
233
    }
234
}
235