Completed
Push — master ( 714012...8b2f7d )
by Ariel
08:14
created

Preferenceable   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 85.71%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 9
c 2
b 0
f 0
lcom 0
cbo 2
dl 0
loc 62
ccs 18
cts 21
cp 0.8571
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A preferences() 0 4 1
A pref() 0 12 3
B cast() 0 19 5
1
<?php
2
3
namespace Timegridio\Concierge\Traits;
4
5
use Timegridio\Concierge\Models\Preference;
6
7
trait Preferenceable
8
{
9
    /**
10
     * Preferences morph.
11
     *
12
     * @return \Illuminate\Database\Eloquent\Relations\MorphMany
13
     */
14 6
    public function preferences()
15
    {
16 6
        return $this->morphMany(Preference::class, 'preferenceable');
0 ignored issues
show
Bug introduced by
It seems like morphMany() 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...
17
    }
18
19
    /**
20
     * Get or set preference value.
21
     *
22
     * @param  mixed $key
23
     * @param  mixed $value
24
     * @param  string $type
25
     *
26
     * @return mixed  Value.
27
     */
28 5
    public function pref($key, $value = null, $type = 'string')
29
    {
30 5
        if (isset($value)) {
31 4
            $this->preferences()->updateOrCreate(['key' => $key], ['value' => $this->cast($value, $type),
32 4
                                                                   'type'  => $type, ]);
33
34 4
            return $value;
35
        }
36 5
        $default = Preference::getDefault($this, $key);
37
38 5
        return($pref = $this->preferences()->forKey($key)->first()) ? $pref->value() : $default->value();
39
    }
40
41
    /**
42
     * Cast value.
43
     *
44
     * @param  mixed $value
45
     * @param  mixed $type
46
     *
47
     * @return mixed Value.
48
     */
49 4
    private function cast($value, $type)
50
    {
51
        switch ($type) {
52 4
            case 'bool':
53 1
                return boolval($value);
54
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
55 3
            case 'int':
56 1
                return intval($value);
57
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
58 2
            case 'float':
59 1
                return floatval($value);
60
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
61 1
            case 'string':
62 1
                return $value;
63
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
64
            default:
65
                return $value;
66
        }
67
    }
68
}
69