Completed
Push — develop ( 46ec4b...680621 )
by Mohamed
03:15
created

CrudTrait   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 9.52%

Importance

Changes 7
Bugs 1 Features 2
Metric Value
wmc 9
c 7
b 1
f 2
lcom 0
cbo 1
dl 0
loc 70
ccs 2
cts 21
cp 0.0952
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A createTag() 0 4 1
A update() 0 4 1
B validOrCreate() 0 21 6
A prepareTagToSave() 0 6 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\Tag;
13
14
use Illuminate\Database\Eloquent;
15
use Tinyissue\Model\Tag;
16
17
/**
18
 * CrudTrait is trait class containing the methods for adding/editing/deleting the Tag model.
19
 *
20
 * @author Mohamed Alsharaf <[email protected]>
21
 *
22
 * @method Eloquent\Model where($column, $operator = null, $value = null, $boolean = 'and')
23
 */
24
trait CrudTrait
25
{
26
    /**
27
     * Create a new tag.
28
     *
29
     * @param array $input
30
     *
31
     * @return mixed
32
     */
33 1
    public function createTag(array $input)
34
    {
35 1
        return $this->fill($this->prepareTagToSave($input))->save();
0 ignored issues
show
Bug introduced by
It seems like fill() 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...
36
    }
37
38
    /**
39
     * Update the tags in the database.
40
     *
41
     * @param array $attributes
42
     *
43
     * @return bool|int
44
     */
45
    public function update(array $attributes = [])
46
    {
47
        return parent::update($this->prepareTagToSave($attributes));
48
    }
49
50
    /**
51
     * Create a new tag if valid or return existing one.
52
     *
53
     * @param string   $name
54
     * @param null|Tag $parent
55
     *
56
     * @return bool|$this
57
     */
58
    public function validOrCreate($name, Tag $parent = null)
59
    {
60
        $group = $parent === null ? true : false;
61
        $tag   = $this->where('name', '=', $name)->first();
62
        if ($tag && $tag->group != $group) {
63
            return false;
64
        }
65
66
        if (!$tag) {
67
            $tag        = new Tag();
68
            $tag->name  = $name;
69
            $tag->group = $group;
70
            if (!is_null($parent)) {
71
                $tag->parent_id = $parent->id;
72
                $tag->setRelation('parent', $parent);
73
            }
74
            $tag->save();
75
        }
76
77
        return $tag;
78
    }
79
80
    /**
81
     * Prepare tag details to save.
82
     *
83
     * @param array $input
84
     *
85
     * @return array
86
     */
87
    protected function prepareTagToSave(array $input)
88
    {
89
        $input['group'] = 0;
90
91
        return $input;
92
    }
93
}
94