PreferenceRepository   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 153
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 3

Importance

Changes 10
Bugs 0 Features 0
Metric Value
wmc 14
c 10
b 0
f 0
lcom 2
cbo 3
dl 0
loc 153
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A has() 0 4 1
A presenter() 0 8 2
A findByKeyOrNew() 0 17 3
A findAllByNamespace() 0 4 1
A __construct() 0 11 2
A get() 0 4 1
A set() 0 13 2
A value() 0 8 2
1
<?php namespace Anomaly\PreferencesModule\Preference;
2
3
use Anomaly\PreferencesModule\Preference\Contract\PreferenceInterface;
4
use Anomaly\PreferencesModule\Preference\Contract\PreferenceRepositoryInterface;
5
use Anomaly\Streams\Platform\Addon\FieldType\FieldTypePresenter;
6
use Anomaly\Streams\Platform\Entry\EntryRepository;
7
use Anomaly\UsersModule\User\Contract\UserInterface;
8
use Illuminate\Contracts\Auth\Guard;
9
10
/**
11
 * Class PreferenceRepositoryInterface
12
 *
13
 * @link          http://pyrocms.com/
14
 * @author        PyroCMS, Inc. <[email protected]>
15
 * @author        Ryan Thompson <[email protected]>
16
 */
17
class PreferenceRepository extends EntryRepository implements PreferenceRepositoryInterface
18
{
19
20
    /**
21
     * The authentication guard.
22
     *
23
     * @var Guard
24
     */
25
    protected $auth;
26
27
    /**
28
     * The preference model.
29
     *
30
     * @var PreferenceModel
31
     */
32
    protected $model;
33
34
    /**
35
     * The preferences collection.
36
     *
37
     * @var PreferenceCollection
38
     */
39
    protected $preferences;
40
41
    /**
42
     * Create a new PreferenceRepositoryInterface instance.
43
     *
44
     * @param Guard           $auth
45
     * @param PreferenceModel $model
46
     */
47
    public function __construct(Guard $auth, PreferenceModel $model)
48
    {
49
        $this->auth  = $auth;
50
        $this->model = $model;
51
52
        $this->preferences = new PreferenceCollection();
53
54
        if ($user = $this->auth->user()) {
0 ignored issues
show
Unused Code introduced by Ryan Thompson
$user is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
55
            $this->preferences = $this->model->belongingToUser($auth->getUser())->get();
56
        }
57
    }
58
59
    /**
60
     * Return if a preference exists or not.
61
     *
62
     * @param $key
63
     * @return bool
64
     */
65
    public function has($key)
66
    {
67
        return $this->preferences->has($key);
68
    }
69
70
    /**
71
     * Get a preference.
72
     *
73
     * @param $key
74
     * @return null|PreferenceInterface
75
     */
76
    public function get($key)
77
    {
78
        return $this->preferences->get($key);
79
    }
80
81
    /**
82
     * Set a preferences value.
83
     *
84
     * @param $key
85
     * @param $value
86
     * @return bool
87
     */
88
    public function set($key, $value)
89
    {
90
        if (!$user = $this->auth->getUser()) {
91
            throw new \Exception('The user could not be determined.');
92
        }
93
94
        $preference = $this->findByKeyOrNew($key);
95
96
        $preference->setUser($user);
97
        $preference->setValue($value);
98
99
        return $this->save($preference);
100
    }
101
102
    /**
103
     * Get a preference value presenter instance.
104
     *
105
     * @param                          $key
106
     * @param  null                    $default
107
     * @return FieldTypePresenter|null
108
     */
109
    public function value($key, $default = null)
110
    {
111
        if ($preference = $this->get($key)) {
112
            return $preference->getValue();
113
        }
114
115
        return $default;
116
    }
117
118
    /**
119
     * Return the field type
120
     * presenter for a preference.
121
     *
122
     * @param $key
123
     * @return FieldTypePresenter|null
124
     */
125
    public function presenter($key)
126
    {
127
        if ($preference = $this->get($key)) {
128
            return $preference->getFieldTypePresenter('value');
129
        }
130
131
        return null;
132
    }
133
134
    /**
135
     * Find a preference by it's key
136
     * or return a new instance.
137
     *
138
     * @param $key
139
     * @return PreferenceInterface
140
     */
141
    public function findByKeyOrNew($key)
142
    {
143
        /* @var UserInterface $user */
144
        if (!$user = $this->auth->getUser()) {
145
            throw new \Exception('The user could not be determined.');
146
        }
147
148
        if (!$preference = $this->model->where('key', $key)->where('user_id', $user->getId())->first()) {
149
150
            $preference = $this->model->newInstance();
151
152
            $preference->setKey($key);
153
            $preference->setUser($user);
154
        }
155
156
        return $preference;
157
    }
158
159
    /**
160
     * Find all preferences with namespace.
161
     *
162
     * @param $namespace
163
     * @return PreferenceCollection
164
     */
165
    public function findAllByNamespace($namespace)
166
    {
167
        return $this->model->where('key', 'LIKE', $namespace . '%')->get();
168
    }
169
}
170