Completed
Push — develop ( f7252d...97603d )
by Mohamed
04:44
created

CrudTrait::createNote()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 16
ccs 0
cts 10
cp 0
rs 9.4286
cc 1
eloc 10
nc 1
nop 1
crap 2
1
<?php
2
3
/*
4
 * This file is part of the Tinyissue package.
5
 *
6
 * (c) Mohamed Alsharaf <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Tinyissue\Model\Traits\Project\Note;
13
14
use Illuminate\Database\Eloquent;
15
use Illuminate\Database\Eloquent\Relations;
16
use Tinyissue\Model;
17
18
/**
19
 * CrudTrait is trait class containing the methods for adding/editing/deleting the Project\Note model
20
 *
21
 * @author Mohamed Alsharaf <[email protected]>
22
 *
23
 * @property int                  $project_id
24
 * @property int                  $created_by
25
 * @property string               $body
26
 * @property Model\Project        $project
27
 * @property Model\User           $createdBy
28
 *
29
 * @method   Relations\BelongsTo  belongsTo($related, $foreignKey = null, $otherKey = null, $relation = null)
30
 * @method   Eloquent\Model       save()
31
 * @method   Relations\HasOne     activity()
32
 */
33
trait CrudTrait
34
{
35
    /**
36
     * Create a new note
37
     *
38
     * @param array $input
39
     *
40
     * @return $this
41
     */
42
    public function createNote(array $input)
43
    {
44
        $this->body = $input['note_body'];
45
        $this->project_id = $this->project->id;
46
        $this->created_by = $this->createdBy->id;
47
        $this->save();
48
49
        // Add to user's activity log
50
        $this->activity()->save(new Model\User\Activity([
51
            'type_id'   => Model\Activity::TYPE_NOTE,
52
            'parent_id' => $this->project->id,
53
            'user_id'   => $this->createdBy->id,
54
        ]));
55
56
        return $this;
57
    }
58
59
    /**
60
     * Delete a note
61
     *
62
     * @return bool|null
63
     *
64
     * @throws \Exception
65
     */
66
    public function delete()
67
    {
68
        $this->activity()->delete();
69
70
        return parent::delete();
71
    }
72
}
73