Completed
Push — develop ( 86c33f...64265e )
by Mohamed
07:18
created

CrudTrait   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 1 Features 0
Metric Value
wmc 2
c 3
b 1
f 0
lcom 1
cbo 2
dl 0
loc 40
ccs 13
cts 13
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A delete() 0 6 1
A createNote() 0 16 1
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 2
    public function createNote(array $input)
43
    {
44 2
        $this->body       = $input['note_body'];
45 2
        $this->project_id = $this->project->id;
46 2
        $this->created_by = $this->createdBy->id;
47 2
        $this->save();
48
49
        // Add to user's activity log
50 2
        $this->activity()->save(new Model\User\Activity([
51 2
            'type_id'   => Model\Activity::TYPE_NOTE,
52 2
            'parent_id' => $this->project->id,
53 2
            'user_id'   => $this->createdBy->id,
54
        ]));
55
56 2
        return $this;
57
    }
58
59
    /**
60
     * Delete a note.
61
     *
62
     * @return bool|null
63
     *
64
     * @throws \Exception
65
     */
66 1
    public function delete()
67
    {
68 1
        $this->activity()->delete();
69
70 1
        return parent::delete();
71
    }
72
}
73