Passed
Push — dev6 ( 3fa4bc...200ba5 )
by Ron
18:44
created

CustomerBookmarkController   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
eloc 15
c 1
b 0
f 0
dl 0
loc 34
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 29 3
1
<?php
2
3
namespace App\Http\Controllers\Customers;
4
5
use Exception;
6
use Illuminate\Support\Facades\Log;
7
8
use App\Http\Controllers\Controller;
9
use App\Models\UserCustomerBookmark;
10
use App\Http\Requests\Customers\CustomerBookmarkRequest;
11
12
class CustomerBookmarkController extends Controller
13
{
14
    /**
15
     * Toggle a customer bookmark on and off
16
     */
17
    public function __invoke(CustomerBookmarkRequest $request)
18
    {
19
        if($request->state)
20
        {
21
            try
22
            {
23
                UserCustomerBookmark::create([
24
                    'user_id' => $request->user()->user_id,
25
                    'cust_id' => $request->cust_id,
26
                ]);
27
            }
28
            catch(Exception $e)
29
            {
30
                //  If for some reason the add fails, trigger error
31
                Log::critical('User '.$request->user()->username.' is trying to bookmark a customer that is already bookmarked', [
32
                    'cust_id' => $request->cust_id,
33
                    'state'   => $request->state,
34
                    'user_id' => $request->user()->user_id,
35
                ]);
36
                abort(409, 'Bookmark already exists');
37
            }
38
        }
39
        else
40
        {
41
            //  Remove the bookmark
42
            UserCustomerBookmark::where('user_id', $request->user()->user_id)->where('cust_id', $request->cust_id)->first()->delete();
43
        }
44
45
        return response()->noContent();
46
    }
47
}
48