Completed
Push — master ( c76184...34678c )
by ARCANEDEV
12s
created

HasManyNotes   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 1
dl 0
loc 93
ccs 16
cts 16
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A notes() 0 4 1
A createNote() 0 11 2
A findNote() 0 4 1
A prepareNoteAttributes() 0 7 2
A getCurrentAuthorId() 0 4 1
1
<?php namespace Arcanedev\LaravelNotes\Traits;
2
3
use Illuminate\Database\Eloquent\Model;
4
5
/**
6
 * Class     HasManyNotes
7
 *
8
 * @package  Arcanedev\LaravelNotes\Traits
9
 * @author   ARCANEDEV <[email protected]>
10
 *
11
 * @property  \Illuminate\Database\Eloquent\Collection  notes
12
 *
13
 * @method    \Illuminate\Database\Eloquent\Relations\MorphMany  morphMany(string $related, string $name, string $type = null, string $id = null, string $localKey = null)
14
 */
15
trait HasManyNotes
16
{
17
    /* -----------------------------------------------------------------
18
     |  Traits
19
     | -----------------------------------------------------------------
20
     */
21
22
    use ConfigHelper;
23
24
    /* -----------------------------------------------------------------
25
     |  Relationships
26
     | -----------------------------------------------------------------
27
     */
28
29
    /**
30
     * The notes relationship.
31
     *
32
     * @return \Illuminate\Database\Eloquent\Relations\MorphMany
33
     */
34 9
    public function notes()
35
    {
36 9
        return $this->morphMany($this->getModelFromConfig('notes'), 'noteable');
37
    }
38
39
    /* -----------------------------------------------------------------
40
     |  Main Methods
41
     | -----------------------------------------------------------------
42
     */
43
44
    /**
45
     * Create a note.
46
     *
47
     * @param  string                                    $content
48
     * @param  \Illuminate\Database\Eloquent\Model|null  $author
49
     * @param  bool                                      $reload
50
     *
51
     * @return \Arcanedev\LaravelNotes\Models\Note
52
     */
53 9
    public function createNote($content, $author = null, $reload = true)
54
    {
55
        /** @var \Arcanedev\LaravelNotes\Models\Note $note */
56 9
        $note = $this->notes()->create(
57 9
            $this->prepareNoteAttributes($content, $author)
58 3
        );
59
60 9
        if ($reload) $this->load(['notes']);
0 ignored issues
show
Bug introduced by
It seems like load() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
61
62 9
        return $note;
63
    }
64
65
    /**
66
     * Retrieve a note by its ID.
67
     *
68
     * @param  int  $id
69
     *
70
     * @return \Illuminate\Database\Eloquent\Model
71
     */
72 3
    public function findNote($id)
73
    {
74 3
        return $this->notes()->where('id', $id)->first();
75
    }
76
77
    /* -----------------------------------------------------------------
78
     |  Other Methods
79
     | -----------------------------------------------------------------
80
     */
81
82
    /**
83
     * Prepare note attributes.
84
     *
85
     * @param  string                                    $content
86
     * @param  \Illuminate\Database\Eloquent\Model|null  $author
87
     *
88
     * @return array
89
     */
90 9
    protected function prepareNoteAttributes($content, Model $author = null)
91
    {
92
        return [
93 9
            'author_id' => is_null($author) ? $this->getCurrentAuthorId() : $author->getKey(),
94 9
            'content'   => $content,
95 3
        ];
96
    }
97
98
    /**
99
     * Get the current author's id.
100
     *
101
     * @return int|null
102
     */
103 6
    protected function getCurrentAuthorId()
104
    {
105 6
        return null;
106
    }
107
}
108