Passed
Push — master ( 37b02e...ebbbe1 )
by James
08:59
created

Preferences::getForUser()   C

Complexity

Conditions 7
Paths 10

Size

Total Lines 29
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 29
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 16
nc 10
nop 3
1
<?php
2
/**
3
 * Preferences.php
4
 * Copyright (c) 2017 [email protected]
5
 *
6
 * This file is part of Firefly III.
7
 *
8
 * Firefly III is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU General Public License as published by
10
 * the Free Software Foundation, either version 3 of the License, or
11
 * (at your option) any later version.
12
 *
13
 * Firefly III is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU General Public License
19
 * along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
20
 */
21
declare(strict_types=1);
22
23
namespace FireflyIII\Support;
24
25
use Cache;
26
use Exception;
27
use FireflyIII\Models\Preference;
28
use FireflyIII\User;
29
use Illuminate\Support\Collection;
30
use Log;
31
use Session;
32
33
/**
34
 * Class Preferences.
35
 */
36
class Preferences
37
{
38
    /**
39
     * @param User   $user
40
     * @param string $search
41
     *
42
     * @return Collection
43
     */
44
    public function beginsWith(User $user, string $search): Collection
45
    {
46
        $set = Preference::where('user_id', $user->id)->where('name', 'LIKE', $search . '%')->get();
47
48
        return $set;
49
    }
50
51
    /**
52
     * @param $name
53
     *
54
     * @return bool
55
     */
56
    public function delete(string $name): bool
57
    {
58
        $fullName = sprintf('preference%s%s', auth()->user()->id, $name);
59
        if (Cache::has($fullName)) {
60
            Cache::forget($fullName);
61
        }
62
        try {
63
            Preference::where('user_id', auth()->user()->id)->where('name', $name)->delete();
64
        } catch (Exception $e) {
65
            // don't care.
66
        }
67
68
        return true;
69
    }
70
71
    /**
72
     * @param string $name
73
     *
74
     * @return Collection
75
     */
76
    public function findByName(string $name): Collection
77
    {
78
        return Preference::where('name', $name)->get();
79
    }
80
81
    /**
82
     * @param      $name
83
     * @param null $default
0 ignored issues
show
Documentation Bug introduced by James Cole
Are you sure the doc-type for parameter $default is correct as it would always require null to be passed?
Loading history...
84
     *
85
     * @return \FireflyIII\Models\Preference|null
86
     */
87
    public function get($name, $default = null)
88
    {
89
        /** @var User $user */
90
        $user = auth()->user();
91
        if (null === $user) {
92
            return $default;
93
        }
94
95
        return $this->getForUser($user, $name, $default);
96
    }
97
98
    /**
99
     * @param \FireflyIII\User $user
100
     * @param array            $list
101
     *
102
     * @return array
103
     */
104
    public function getArrayForUser(User $user, array $list): array
105
    {
106
        $result      = [];
107
        $preferences = Preference::where('user_id', $user->id)->whereIn('name', $list)->get(['id', 'name', 'data']);
108
        /** @var Preference $preference */
109
        foreach ($preferences as $preference) {
110
            $result[$preference->name] = $preference->data;
111
        }
112
        foreach ($list as $name) {
113
            if (!isset($result[$name])) {
114
                $result[$name] = null;
115
            }
116
        }
117
118
        return $result;
119
    }
120
121
    /**
122
     * @param \FireflyIII\User $user
123
     * @param string           $name
124
     * @param null|string      $default
125
     *
126
     * @return \FireflyIII\Models\Preference|null
127
     */
128
    public function getForUser(User $user, $name, $default = null)
129
    {
130
        $fullName = sprintf('preference%s%s', $user->id, $name);
131
        if (Cache::has($fullName)) {
132
            return Cache::get($fullName);
133
        }
134
135
        $preference = Preference::where('user_id', $user->id)->where('name', $name)->first(['id', 'name', 'data']);
136
        if (null !== $preference && null === $preference->data) {
137
            try {
138
                $preference->delete();
139
            } catch (Exception $e) {
140
                Log::debug(sprintf('Could not delete preference #%d', $preference->id));
141
            }
142
            $preference = false;
143
        }
144
145
        if ($preference) {
146
            Cache::forever($fullName, $preference);
147
148
            return $preference;
149
        }
150
        // no preference found and default is null:
151
        if (null === $default) {
152
            // return NULL
153
            return null;
154
        }
155
156
        return $this->setForUser($user, $name, $default);
157
    }
158
159
    /**
160
     * @return string
161
     */
162
    public function lastActivity(): string
163
    {
164
        $lastActivity = microtime();
165
        $preference   = $this->get('lastActivity', microtime());
166
        if (null !== $preference && null !== $preference->data) {
167
            $lastActivity = $preference->data;
168
        }
169
        if (\is_array($lastActivity)) {
170
            $lastActivity = implode(',', $lastActivity);
171
        }
172
173
        return md5($lastActivity);
174
    }
175
176
    /**
177
     * @return bool
178
     */
179
    public function mark(): bool
180
    {
181
        $this->set('lastActivity', microtime());
182
        Session::forget('first');
183
184
        return true;
185
    }
186
187
    /**
188
     * @param   $name
189
     * @param   $value
190
     *
191
     * @return Preference
192
     */
193
    public function set($name, $value): Preference
194
    {
195
        $user = auth()->user();
196
        if (null === $user) {
197
            // make new preference, return it:
198
            $pref       = new Preference;
199
            $pref->name = $name;
0 ignored issues
show
Bug introduced by James Cole
The property name does not seem to exist on FireflyIII\Models\Preference. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
200
            $pref->data = $value;
201
202
            return $pref;
203
        }
204
205
        return $this->setForUser(auth()->user(), $name, $value);
206
    }
207
208
    /**
209
     * @param \FireflyIII\User $user
210
     * @param                  $name
211
     * @param string           $value
212
     *
213
     * @return Preference
214
     */
215
    public function setForUser(User $user, $name, $value): Preference
216
    {
217
        $fullName = sprintf('preference%s%s', $user->id, $name);
218
        Cache::forget($fullName);
219
        $pref = Preference::where('user_id', $user->id)->where('name', $name)->first(['id', 'name', 'data']);
220
221
        if (null !== $pref) {
222
            $pref->data = $value;
223
            $pref->save();
224
225
            Cache::forever($fullName, $pref);
226
227
            return $pref;
228
        }
229
230
        $pref       = new Preference;
231
        $pref->name = $name;
0 ignored issues
show
Bug introduced by James Cole
The property name does not seem to exist on FireflyIII\Models\Preference. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
232
        $pref->data = $value;
233
        $pref->user()->associate($user);
234
235
        $pref->save();
236
237
        Cache::forever($fullName, $pref);
238
239
        return $pref;
240
    }
241
}
242