Completed
Push — master ( b78cbc...6c45fe )
by ARCANEDEV
16s
created

HasManyNotes::prepareNoteAttributes()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 2
crap 2
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 3
    public function notes()
35
    {
36 3
        return $this->morphMany($this->getModelFromConfig('notes'), 'noteable');
37
    }
38
39
    /* -----------------------------------------------------------------
40
     |  Main Methods
41
     | -----------------------------------------------------------------
42
     */
43
    /**
44
     * Create a note.
45
     *
46
     * @param  string                                    $content
47
     * @param  \Illuminate\Database\Eloquent\Model|null  $author
48
     * @param  bool                                      $reload
49
     *
50
     * @return \Arcanedev\LaravelNotes\Models\Note
51
     */
52 3
    public function createNote($content, $author = null, $reload = true)
53
    {
54
        /** @var \Arcanedev\LaravelNotes\Models\Note $note */
55 3
        $note = $this->notes()->create(
56 3
            $this->prepareNoteAttributes($content, $author)
57 1
        );
58
59 3
        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...
60
61 3
        return $note;
62
    }
63
64
    /**
65
     * Retrieve a note by its ID.
66
     *
67
     * @param  int  $id
68
     *
69
     * @return \Illuminate\Database\Eloquent\Model
70
     */
71
    public function findNote($id)
72
    {
73
        return $this->notes()->where('id', $id)->first();
74
    }
75
76
    /* -----------------------------------------------------------------
77
     |  Other Methods
78
     | -----------------------------------------------------------------
79
     */
80
81
    /**
82
     * Prepare note attributes.
83
     *
84
     * @param  string                                    $content
85
     * @param  \Illuminate\Database\Eloquent\Model|null  $author
86
     *
87
     * @return array
88
     */
89 3
    protected function prepareNoteAttributes($content, Model $author = null)
90
    {
91
        return [
92 3
            'author_id' => is_null($author) ? $this->getCurrentAuthorId() : $author->getKey(),
93 3
            'content'   => $content,
94 1
        ];
95
    }
96
97
    /**
98
     * Get the current author's id.
99
     *
100
     * @return int|null
101
     */
102
    protected function getCurrentAuthorId()
103
    {
104
        return null;
105
    }
106
}
107