Completed
Branch develop-3.0 (4fe777)
by Mohamed
11:06
created

UpdaterRepository::queue()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 25
Code Lines 12

Duplication

Lines 25
Ratio 100 %

Importance

Changes 0
Metric Value
dl 25
loc 25
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 12
nc 3
nop 3
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\Repository\Message\Queue;
13
14
use Illuminate\Database\Eloquent\Model;
15
use Tinyissue\Contracts\Model\UserInterface;
16
use Tinyissue\Contracts\Repository\Message\Queue\UpdaterRepository as MessageQueueUpdater;
17
use Tinyissue\Model\Message;
18
use Tinyissue\Model\Project\Issue;
19
use Tinyissue\Model\User;
20
use Tinyissue\Repository\RepositoryUpdater;
21
22
class UpdaterRepository extends RepositoryUpdater implements MessageQueueUpdater
23
{
24
    /**
25
     * Insert a record into the message queue.
26
     *
27
     * @param string   $name
28
     * @param Model    $model
29
     * @param int|User $changeBy
30
     *
31
     * @return void
32
     */
33 View Code Duplication
    public function queue($name, Model $model, $changeBy)
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...
34
    {
35
        // Get modified attributes
36
        $dirty = $model->getDirty();
37
        $isNew = strpos($name, 'add_') === 0;
38
39
        // Stop if nothing changed
40
        if (!$model->isDirty() && !$isNew) {
41
            return;
42
        }
43
44
        // Get the original value of the modified attributes
45
        $origin = [];
46
        foreach ($dirty as $field => $value) {
47
            $origin[$field] = $model->getOriginal($field, $value);
48
        }
49
50
        // Fill and save to message queue
51
        $fill = $this->getFillAttributes($name, $model, $changeBy, [
52
            'dirty'  => $dirty,
53
            'origin' => $origin,
54
        ]);
55
56
        return $this->model->fill($fill)->save();
57
    }
58
59
    /**
60
     * Insert a record into the message queue about a delete event.
61
     *
62
     * @param string   $name
63
     * @param Model    $model
64
     * @param int|User $changeBy
65
     *
66
     * @return void
67
     */
68 View Code Duplication
    public function queueDelete($name, Model $model, $changeBy)
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...
69
    {
70
        // Fill and save to message queue
71
        $fill = $this->getFillAttributes($name, $model, $changeBy, [
72
            'dirty'  => [],
73
            'origin' => $model->toArray(),
74
        ]);
75
76
        return $this->model->fill($fill)->save();
77
    }
78
79
    /**
80
     * Insert records about tag changes into the message queue.
81
     *
82
     * @param Issue         $issue
83
     * @param array         $added
84
     * @param array         $removed
85
     * @param UserInterface $changeBy
86
     *
87
     * @return mixed
88
     */
89
    public function queueIssueTagChanges(Issue $issue, array $added, array $removed, UserInterface $changeBy)
90
    {
91
        // Fill and save to message queue
92
        $fill = $this->getFillAttributes(Message\Queue::CHANGE_TAG_ISSUE, $issue, $changeBy, [
93
            'added'   => $added,
94
            'removed' => $removed,
95
        ]);
96
97
        return $this->model->fill($fill)->save();
98
    }
99
100
    /**
101
     * Returns an array containing the data needed for the message queue save.
102
     *
103
     * @param string            $name
104
     * @param Model             $model
105
     * @param int|UserInterface $changeBy
106
     * @param array             $payload
107
     *
108
     * @return array
109
     */
110 View Code Duplication
    protected function getFillAttributes($name, Model $model, $changeBy, array $payload)
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...
111
    {
112
        $changeById = (int) ($changeBy instanceof UserInterface ? $changeBy->id : $changeBy);
0 ignored issues
show
Bug introduced by
Accessing id on the interface Tinyissue\Contracts\Model\UserInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
113
114
        $fill                 = [];
115
        $fill['event']        = $name;
116
        $fill['payload']      = $payload;
117
        $fill['model_type']   = get_class($model);
118
        $fill['model_id']     = $model->id;
119
        $fill['change_by_id'] = $changeById;
120
121
        return $fill;
122
    }
123
}
124