EloquentWidgetRepository::updateOrCreateForUser()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
rs 9.4286
cc 2
eloc 5
nc 2
nop 2
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