Completed
Push — master ( 195b94...7a36f1 )
by Mohamed
04:50
created

CrudTrait::delete()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
crap 1
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\Traits\Project\Note;
13
14
use Illuminate\Database\Eloquent;
15
use Tinyissue\Model;
16
17
/**
18
 * CrudTrait is trait class containing the methods for adding/editing/deleting the Project\Note model.
19
 *
20
 * @author Mohamed Alsharaf <[email protected]>
21
 *
22
 * @property static $this
23
 */
24
trait CrudTrait
25
{
26
    /**
27
     * Create a new note.
28
     *
29
     * @param array $input
30
     *
31
     * @return $this
32
     */
33
    public function createNote(array $input)
34
    {
35
        $this->body       = $input['note_body'];
0 ignored issues
show
Bug introduced by
The property body does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
36
        $this->project_id = $this->project->id;
0 ignored issues
show
Bug introduced by
The property project_id does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
Bug introduced by
The property project does not seem to exist. Did you mean project_id?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
37
        $this->created_by = $this->createdBy->id;
0 ignored issues
show
Bug introduced by
The property created_by does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
Bug introduced by
The property createdBy does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
38
39
        // Add event on successful save
40
        static::saved(function (Model\Project\Note $note) {
41
            $this->queueAdd($note, $note->createdBy);
42 2
        });
43
44 2
        $this->save();
45 2
46 2
        // Add to user's activity log
47 2
        $this->activity()->save(new Model\User\Activity([
48
            'type_id'   => Model\Activity::TYPE_NOTE,
49
            'parent_id' => $this->project->id,
0 ignored issues
show
Bug introduced by
The property project does not seem to exist. Did you mean project_id?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
50 2
            'user_id'   => $this->createdBy->id,
51 2
        ]));
52 2
53 2
        return $this;
54
    }
55
56 2
    /**
57
     * Update the note body.
58
     *
59
     * @param string     $body
60
     * @param Model\User $user
61
     *
62
     * @return Eloquent\Model
63
     */
64
    public function updateBody($body, Model\User $user)
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...
65
    {
66 1
        $this->body = $body;
67
68 1
        // Add event on successful save
69
        static::saved(function (Model\Project\Note $note) use ($user) {
70 1
            $this->queueUpdate($note, $user);
71
        });
72
73
        return $this->save();
74
    }
75
76
    /**
77
     * Delete a note.
78
     *
79
     * @param Model\User $user
80
     *
81
     * @return bool|null
82
     *
83
     * @throws \Exception
84
     */
85
    public function deleteNote(Model\User $user)
86
    {
87
        $this->activity()->delete();
88
89
        // Add event on successful delete
90
        static::deleted(function (Model\Project\Note $note) use ($user) {
91
            $this->queueDelete($note, $user);
92
        });
93
94
        return $this->delete();
0 ignored issues
show
Bug introduced by
The method delete() does not exist on Tinyissue\Model\Traits\Project\Note\CrudTrait. Did you maybe mean deleteNote()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
95
    }
96
97
    abstract public function activity();
98
    abstract public function save(array $options = []);
99
    abstract public function queueDelete(Model\Project\Note $note, Model\User $changeBy);
100
    abstract public function queueUpdate(Model\Project\Note $note, Model\User $changeBy);
101
    abstract public function queueAdd(Model\Project\Note $note, Model\User $changeBy);
102
    abstract public function fill(array $attributes);
103
}
104