TaskLinkEventBuilder   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 78
Duplicated Lines 19.23 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 15
loc 78
rs 10
c 0
b 0
f 0
wmc 9
lcom 1
cbo 2

4 Methods

Rating   Name   Duplication   Size   Complexity  
A withTaskLinkId() 0 6 1
A buildEvent() 15 15 2
A buildTitleWithAuthor() 0 10 3
A buildTitleWithoutAuthor() 0 10 3

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 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\TaskLinkEvent;
15
use Jitamin\Model\TaskLinkModel;
16
17
/**
18
 * Class TaskLinkEventBuilder.
19
 */
20
class TaskLinkEventBuilder extends BaseEventBuilder
21
{
22
    protected $taskLinkId = 0;
23
24
    /**
25
     * Set taskLinkId.
26
     *
27
     * @param int $taskLinkId
28
     *
29
     * @return $this
30
     */
31
    public function withTaskLinkId($taskLinkId)
32
    {
33
        $this->taskLinkId = $taskLinkId;
34
35
        return $this;
36
    }
37
38
    /**
39
     * Build event data.
40
     *
41
     * @return TaskLinkEvent|null
42
     */
43 View Code Duplication
    public function buildEvent()
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...
44
    {
45
        $taskLink = $this->taskLinkModel->getById($this->taskLinkId);
0 ignored issues
show
Documentation introduced by
The property taskLinkModel does not exist on object<Jitamin\Bus\Event...r\TaskLinkEventBuilder>. 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...
46
47
        if (empty($taskLink)) {
48
            $this->logger->debug(__METHOD__.': TaskLink not found');
0 ignored issues
show
Documentation introduced by
The property logger does not exist on object<Jitamin\Bus\Event...r\TaskLinkEventBuilder>. 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...
49
50
            return;
51
        }
52
53
        return new TaskLinkEvent([
54
            'task_link' => $taskLink,
55
            'task'      => $this->taskFinderModel->getDetails($taskLink['task_id']),
0 ignored issues
show
Documentation introduced by
The property taskFinderModel does not exist on object<Jitamin\Bus\Event...r\TaskLinkEventBuilder>. 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...
56
        ]);
57
    }
58
59
    /**
60
     * Get event title with author.
61
     *
62
     * @param string $author
63
     * @param string $eventName
64
     * @param array  $eventData
65
     *
66
     * @return string
67
     */
68
    public function buildTitleWithAuthor($author, $eventName, array $eventData)
69
    {
70
        if ($eventName === TaskLinkModel::EVENT_CREATE_UPDATE) {
71
            return l('%s set a new internal link for the task #%d', $author, $eventData['task']['id']);
72
        } elseif ($eventName === TaskLinkModel::EVENT_DELETE) {
73
            return l('%s removed an internal link for the task #%d', $author, $eventData['task']['id']);
74
        }
75
76
        return '';
77
    }
78
79
    /**
80
     * Get event title without author.
81
     *
82
     * @param string $eventName
83
     * @param array  $eventData
84
     *
85
     * @return string
86
     */
87
    public function buildTitleWithoutAuthor($eventName, array $eventData)
88
    {
89
        if ($eventName === TaskLinkModel::EVENT_CREATE_UPDATE) {
90
            return l('A new internal link for the task #%d have been defined', $eventData['task']['id']);
91
        } elseif ($eventName === TaskLinkModel::EVENT_DELETE) {
92
            return l('Internal link removed for the task #%d', $eventData['task']['id']);
93
        }
94
95
        return '';
96
    }
97
}
98