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

UpdaterRepository   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 4
dl 0
loc 77
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A create() 0 23 1
A updateBody() 0 11 1
A delete() 0 11 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\Repository\Project\Note;
13
14
use Illuminate\Database\Eloquent;
15
use Tinyissue\Contracts\Repository\Project\Note\UpdaterRepository as NoteUpdater;
16
use Tinyissue\Model;
17
use Tinyissue\Model\Project;
18
use Tinyissue\Model\User;
19
use Tinyissue\Repository\RepositoryUpdater;
20
21
class UpdaterRepository extends RepositoryUpdater implements NoteUpdater
22
{
23
    public function __construct(Project\Note $model)
24
    {
25
        $this->model = $model;
26
    }
27
28
    /**
29
     * Create a new note.
30
     *
31
     * @param array $input
32
     *
33
     * @return $this
34
     */
35
    public function create(array $input)
36
    {
37
        $this->model->body       = $input['note_body'];
38
        $this->model->project_id = $this->project->id;
0 ignored issues
show
Bug introduced by
The property project 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...
39
        $this->model->created_by = $this->createdBy->id;
0 ignored issues
show
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...
40
41
        // Add event on successful save
42
        Model\Project\Note::saved(function (Model\Project\Note $note) {
43
            $this->queueAdd($note, $note->createdBy);
0 ignored issues
show
Bug introduced by
The method queueAdd() does not seem to exist on object<Tinyissue\Reposit...Note\UpdaterRepository>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
44
        });
45
46
        $this->model->save();
47
48
        // Add to user's activity log
49
        // @todo abstract this?
50
        $this->model->activity()->save(new Model\User\Activity([
51
            'type_id'   => Model\Activity::TYPE_NOTE,
52
            'parent_id' => $this->model->project->id,
53
            'user_id'   => $this->model->createdBy->id,
54
        ]));
55
56
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this; (Tinyissue\Repository\Pro...\Note\UpdaterRepository) is incompatible with the return type of the parent method Tinyissue\Repository\RepositoryUpdater::create of type boolean.

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...
57
    }
58
59
    /**
60
     * Update the note body.
61
     *
62
     * @param string     $body
63
     * @param Model\User $user
64
     *
65
     * @return Eloquent\Model
66
     */
67
    public function updateBody($body, Model\User $user)
68
    {
69
        $this->model->body = $body;
70
71
        // Add event on successful save
72
        Model\Project\Note::saved(function (Model\Project\Note $note) use ($user) {
73
            $this->queueUpdate($note, $user);
0 ignored issues
show
Bug introduced by
The method queueUpdate() does not exist on Tinyissue\Repository\Pro...\Note\UpdaterRepository. Did you maybe mean update()?

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...
74
        });
75
76
        return $this->model->save();
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->model->save(); (boolean) is incompatible with the return type declared by the interface Tinyissue\Contracts\Repo...rRepository::updateBody of type Tinyissue\Model\Project\Note.

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...
77
    }
78
79
    /**
80
     * Delete a note.
81
     *
82
     * @return bool|null
83
     *
84
     * @throws \Exception
85
     */
86
    public function delete()
87
    {
88
        $this->model->activity()->delete();
89
90
        // Add event on successful delete
91
        Model\Project\Note::deleted(function (Model\Project\Note $note) {
92
            $this->queueDelete($note, $this->getLoggedUser());
0 ignored issues
show
Bug introduced by
The method queueDelete() does not exist on Tinyissue\Repository\Pro...\Note\UpdaterRepository. Did you maybe mean delete()?

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...
93
        });
94
95
        return $this->model->delete();
96
    }
97
}
98