Passed
Push — master ( cf47da...53c71b )
by James
21:48 queued 09:49
created

Preferences::setForUser()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 39
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 23
dl 0
loc 39
rs 8.9297
c 0
b 0
f 0
cc 6
nc 5
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
            Log::debug(sprintf('Could not delete preference: %s', $e->getMessage()));
66
            // don't care.
67
        }
68
69
        return true;
70
    }
71
72
    /**
73
     * @param string $name
74
     *
75
     * @return Collection
76
     */
77
    public function findByName(string $name): Collection
78
    {
79
        return Preference::where('name', $name)->get();
80
    }
81
82
    /**
83
     * @param string $name
84
     * @param mixed  $default
85
     *
86
     * @return \FireflyIII\Models\Preference|null
87
     */
88
    public function get(string $name, $default = null): ?Preference
89
    {
90
        /** @var User $user */
91
        $user = auth()->user();
92
        if (null === $user) {
93
            return $default;
94
        }
95
96
        return $this->getForUser($user, $name, $default);
97
    }
98
99
    /**
100
     * @param \FireflyIII\User $user
101
     * @param array            $list
102
     *
103
     * @return array
104
     */
105
    public function getArrayForUser(User $user, array $list): array
106
    {
107
        $result      = [];
108
        $preferences = Preference::where('user_id', $user->id)->whereIn('name', $list)->get(['id', 'name', 'data']);
109
        /** @var Preference $preference */
110
        foreach ($preferences as $preference) {
111
            $result[$preference->name] = $preference->data;
112
        }
113
        foreach ($list as $name) {
114
            if (!isset($result[$name])) {
115
                $result[$name] = null;
116
            }
117
        }
118
119
        return $result;
120
    }
121
122
    /**
123
     * @param \FireflyIII\User $user
124
     * @param string           $name
125
     * @param null|string      $default
126
     *
127
     * @return \FireflyIII\Models\Preference|null
128
     */
129
    public function getForUser(User $user, string $name, $default = null): ?Preference
130
    {
131
        $fullName = sprintf('preference%s%s', $user->id, $name);
132
        if (Cache::has($fullName)) {
133
            return Cache::get($fullName);
134
        }
135
136
        $preference = Preference::where('user_id', $user->id)->where('name', $name)->first(['id', 'name', 'data', 'updated_at', 'created_at']);
137
        if (null !== $preference && null === $preference->data) {
138
            try {
139
                $preference->delete();
140
            } catch (Exception $e) {
141
                Log::debug(sprintf('Could not delete preference #%d: %s', $preference->id, $e->getMessage()));
142
            }
143
            $preference = null;
144
        }
145
146
        if (null !== $preference) {
147
            Cache::forever($fullName, $preference);
148
149
            return $preference;
150
        }
151
        // no preference found and default is null:
152
        if (null === $default) {
153
            // return NULL
154
            return null;
155
        }
156
157
        return $this->setForUser($user, $name, $default);
158
    }
159
160
    /**
161
     * @return string
162
     */
163
    public function lastActivity(): string
164
    {
165
        $lastActivity = microtime();
166
        $preference   = $this->get('lastActivity', microtime());
167
168
        if (null !== $preference && null !== $preference->data) {
169
            $lastActivity = $preference->data;
170
        }
171
        if (\is_array($lastActivity)) {
172
            $lastActivity = implode(',', $lastActivity);
173
        }
174
175
        return md5($lastActivity);
176
    }
177
178
    /**
179
     *
180
     */
181
    public function mark(): void
182
    {
183
        $this->set('lastActivity', microtime());
184
        Session::forget('first');
185
    }
186
187
    /**
188
     * @param string $name
189
     * @param mixed  $value
190
     *
191
     * @return \FireflyIII\Models\Preference
192
     */
193
    public function set(string $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;
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 string           $name
211
     * @param mixed            $value
212
     *
213
     * @return Preference
214
     */
215
    public function setForUser(User $user, string $name, $value): Preference
216
    {
217
        $fullName = sprintf('preference%s%s', $user->id, $name);
218
        Cache::forget($fullName);
219
        /** @var Preference $pref */
220
        $pref = Preference::where('user_id', $user->id)->where('name', $name)->first(['id', 'name', 'data', 'updated_at', 'created_at']);
221
222
        if (null !== $pref && null === $value) {
223
            try {
224
                $pref->delete();
225
            } catch (Exception $e) {
226
                Log::error(sprintf('Could not delete preference: %s', $e->getMessage()));
227
            }
228
229
            return new Preference;
230
        }
231
        if (null === $value) {
232
            return new Preference;
233
        }
234
235
        if (null !== $pref) {
236
            $pref->data = $value;
237
            $pref->save();
238
239
            Cache::forever($fullName, $pref);
240
241
            return $pref;
242
        }
243
244
        $pref       = new Preference;
245
        $pref->name = $name;
246
        $pref->data = $value;
247
        $pref->user()->associate($user);
248
249
        $pref->save();
250
251
        Cache::forever($fullName, $pref);
252
253
        return $pref;
254
    }
255
}
256