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

HasOneNote::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 2
Bugs 0 Features 0
Metric Value
dl 0
loc 7
ccs 3
cts 3
cp 1
rs 9.4285
c 2
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
 * Trait     HasOneNote
7
 *
8
 * @package  Arcanedev\LaravelNotes\Traits
9
 * @author   ARCANEDEV <[email protected]>
10
 *
11
 * @property  \Arcanedev\LaravelNotes\Models\Note  note
12
 */
13
trait HasOneNote
14
{
15
    /* -----------------------------------------------------------------
16
     |  Relationships
17
     | -----------------------------------------------------------------
18
     */
19
20
    /**
21
     * Relation to ONE note.
22
     *
23
     * @return \Illuminate\Database\Eloquent\Relations\MorphOne
24
     */
25 15
    public function note()
26
    {
27 15
        return $this->morphOne(config('notes.notes.model'), 'noteable');
0 ignored issues
show
Bug introduced by
It seems like morphOne() 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...
28
    }
29
30
    /* -----------------------------------------------------------------
31
     |  Main Methods
32
     | -----------------------------------------------------------------
33
     */
34
35
    /**
36
     * Create a note.
37
     *
38
     * @param  string                                    $content
39
     * @param  \Illuminate\Database\Eloquent\Model|null  $author
40
     * @param  bool                                      $reload
41
     *
42
     * @return \Arcanedev\LaravelNotes\Models\Note
43
     */
44 15
    public function createNote($content, $author = null, $reload = true)
45
    {
46 15
        if ($this->note) $this->note->delete();
47
48
        /** @var \Arcanedev\LaravelNotes\Models\Note $note */
49 15
        $note = $this->note()->create(
50 15
            $this->prepareNoteAttributes($content, $author)
51
        );
52
53 15
        if ($reload) $this->load(['note']);
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...
54
55 15
        return $note;
56
    }
57
58
    /**
59
     * Update a note.
60
     *
61
     * @param  string                                    $content
62
     * @param  \Illuminate\Database\Eloquent\Model|null  $author
63
     * @param  bool                                      $reload
64
     *
65
     * @return bool
66
     */
67 3
    public function updateNote($content, Model $author = null, $reload = true)
68
    {
69 3
        $updated = $this->note->update(
70 3
            $this->prepareNoteAttributes($content, $author)
71
        );
72
73 3
        if ($reload) $this->load(['note']);
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...
74
75 3
        return $updated;
76
    }
77
78
    /* -----------------------------------------------------------------
79
     |  Other Methods
80
     | -----------------------------------------------------------------
81
     */
82
83
    /**
84
     * Prepare note attributes.
85
     *
86
     * @param  string                                    $content
87
     * @param  \Illuminate\Database\Eloquent\Model|null  $author
88
     *
89
     * @return array
90
     */
91 15
    protected function prepareNoteAttributes($content, Model $author = null)
92
    {
93
        return [
94 15
            'author_id' => is_null($author) ? $this->getCurrentAuthorId() : $author->getKey(),
95 15
            'content'   => $content,
96
        ];
97
    }
98
99
    /**
100
     * Get the current author's id.
101
     *
102
     * @return int|null
103
     */
104 12
    protected function getCurrentAuthorId()
105
    {
106 12
        return null;
107
    }
108
}
109