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

HasOneNote::getCurrentAuthorId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 1
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
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
 * @method    \Illuminate\Database\Eloquent\Relations\MorphOne  morphOne(string $related, string $name, string $type = null, string $id = null, string $localKey = null)
14
 */
15
trait HasOneNote
16
{
17
    /* -----------------------------------------------------------------
18
     |  Traits
19
     | -----------------------------------------------------------------
20
     */
21
22
    use ConfigHelper;
23
24
    /* -----------------------------------------------------------------
25
     |  Relationships
26
     | -----------------------------------------------------------------
27
     */
28
29
    /**
30
     * Relation to ONE note.
31
     *
32
     * @return \Illuminate\Database\Eloquent\Relations\MorphOne
33
     */
34 15
    public function note()
35
    {
36 15
        return $this->morphOne($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 15
    public function createNote($content, $author = null, $reload = true)
54
    {
55 15
        if ($this->note) $this->note->delete();
56
57
        /** @var \Arcanedev\LaravelNotes\Models\Note $note */
58 15
        $note = $this->note()->create(
59 15
            $this->prepareNoteAttributes($content, $author)
60 5
        );
61
62 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...
63
64 15
        return $note;
65
    }
66
67
    /**
68
     * Update a note.
69
     *
70
     * @param  string                                    $content
71
     * @param  \Illuminate\Database\Eloquent\Model|null  $author
72
     * @param  bool                                      $reload
73
     *
74
     * @return bool
75
     */
76 3
    public function updateNote($content, Model $author = null, $reload = true)
77
    {
78 3
        $updated = $this->note->update(
79 3
            $this->prepareNoteAttributes($content, $author)
80 1
        );
81
82 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...
83
84 3
        return $updated;
85 1
    }
86
87
    /* -----------------------------------------------------------------
88
     |  Other Methods
89
     | -----------------------------------------------------------------
90
     */
91
92
    /**
93
     * Prepare note attributes.
94
     *
95
     * @param  string                                    $content
96
     * @param  \Illuminate\Database\Eloquent\Model|null  $author
97
     *
98
     * @return array
99
     */
100 15
    protected function prepareNoteAttributes($content, Model $author = null)
101
    {
102
        return [
103 15
            'author_id' => is_null($author) ? $this->getCurrentAuthorId() : $author->getKey(),
104 15
            'content'   => $content,
105 5
        ];
106
    }
107
108
    /**
109
     * Get the current author's id.
110
     *
111
     * @return int|null
112
     */
113 12
    protected function getCurrentAuthorId()
114
    {
115 12
        return null;
116
    }
117
}
118