EloquentWidgetRepository::findForUser()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php namespace Modules\Dashboard\Repositories\Eloquent;
2
3
use Modules\Core\Repositories\Eloquent\EloquentBaseRepository;
4
use Modules\Dashboard\Repositories\WidgetRepository;
5
6
class EloquentWidgetRepository extends EloquentBaseRepository implements WidgetRepository
7
{
8
    /**
9
     * Find the saved state of widgets for the given user id
10
     * @param int $userId
11
     * @return string
12
     */
13
    public function findForUser($userId)
14
    {
15
        return $this->model->whereUserId($userId)->first();
16
    }
17
18
    /**
19
     * Update or create the given widgets for given user
20
     * @param array $widgets
21
     * @return void
22
     */
23
    public function updateOrCreateForUser($widgets, $userId)
24
    {
25
        $widget = $this->findForUser($userId);
26
27
        if ($widget) {
28
            return $this->update($widget, ['widgets' => $widgets]);
29
        }
30
31
        return $this->create(['widgets' => $widgets, 'user_id' => $userId]);
32
    }
33
}
34