Completed
Push — master ( f09330...3a5f22 )
by ARCANEDEV
04:42
created

HasManyNotes::prepareNoteAttributes()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 3
cts 3
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
     |  Relationships
19
     | -----------------------------------------------------------------
20
     */
21
22
    /**
23
     * The notes relationship.
24
     *
25
     * @return \Illuminate\Database\Eloquent\Relations\MorphMany
26
     */
27 15
    public function notes()
28
    {
29 15
        return $this->morphMany(config('notes.notes.model'), 'noteable');
30
    }
31
32
    /* -----------------------------------------------------------------
33
     |  Main Methods
34
     | -----------------------------------------------------------------
35
     */
36
37
    /**
38
     * Create a note.
39
     *
40
     * @param  string                                    $content
41
     * @param  \Illuminate\Database\Eloquent\Model|null  $author
42
     * @param  bool                                      $reload
43
     *
44
     * @return \Arcanedev\LaravelNotes\Models\Note
45
     */
46 15
    public function createNote($content, $author = null, $reload = true)
47
    {
48
        /** @var \Arcanedev\LaravelNotes\Models\Note $note */
49 15
        $note = $this->notes()->create(
50 15
            $this->prepareNoteAttributes($content, $author)
51
        );
52
53 15
        $relations = array_merge(['notes'], method_exists($this, 'authoredNotes') ? ['authoredNotes'] : []);
54
55 15
        if ($reload) $this->load($relations);
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...
56
57 15
        return $note;
58
    }
59
60
    /**
61
     * Retrieve a note by its ID.
62
     *
63
     * @param  int  $id
64
     *
65
     * @return \Illuminate\Database\Eloquent\Model
66
     */
67 3
    public function findNote($id)
68
    {
69 3
        return $this->notes()->where('id', $id)->first();
70
    }
71
72
    /* -----------------------------------------------------------------
73
     |  Other Methods
74
     | -----------------------------------------------------------------
75
     */
76
77
    /**
78
     * Prepare note attributes.
79
     *
80
     * @param  string                                    $content
81
     * @param  \Illuminate\Database\Eloquent\Model|null  $author
82
     *
83
     * @return array
84
     */
85 15
    protected function prepareNoteAttributes($content, Model $author = null)
86
    {
87
        return [
88 15
            'author_id' => is_null($author) ? $this->getCurrentAuthorId() : $author->getKey(),
89 15
            'content'   => $content,
90
        ];
91
    }
92
93
    /**
94
     * Get the current author's id.
95
     *
96
     * @return int|null
97
     */
98 6
    protected function getCurrentAuthorId()
99
    {
100 6
        return null;
101
    }
102
}
103