Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
12.50% covered (danger)
12.50%
5 / 40
20.00% covered (danger)
20.00%
1 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
AdministratorsController
12.50% covered (danger)
12.50%
5 / 40
20.00% covered (danger)
20.00%
1 / 5
126.22
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 list_administrators
40.00% covered (danger)
40.00%
2 / 5
0.00% covered (danger)
0.00%
0 / 1
2.86
 create_administrator
22.22% covered (danger)
22.22%
2 / 9
0.00% covered (danger)
0.00%
0 / 1
7.23
 update_administrator
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
12
 delete_administrator
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 1
20
1<?php
2
3namespace App\Http\Controllers;
4
5use App\Exceptions\AppException;
6use App\Http\Controllers\Concerns\ChecksRole;
7use App\Http\Requests\StoreAdministratorRequest;
8use App\Http\Requests\UpdateAdministratorRequest;
9use App\Http\Resources\AdministratorResource;
10use App\Models\Administrator;
11use App\Models\Client;
12use Illuminate\Support\Facades\App;
13
14class AdministratorsController extends Controller
15{
16    use ChecksRole;
17
18    public function __construct()
19    {
20        App::setLocale(request()->header('Locale-Id'));
21    }
22
23    /**
24     * GET /administrators
25     * List all administrators with their assigned commercial.
26     */
27    public function list_administrators()
28    {
29        try {
30            $data = Administrator::with('assignedCommercial')->orderBy('name')->get();
31
32            return AdministratorResource::collection($data);
33
34        } catch (\Exception $e) {
35            report(AppException::fromException($e, 'LIST_ADMINISTRATORS_EXCEPTION'));
36
37            return response(['message' => 'KO', 'error' => $e->getMessage()]);
38        }
39    }
40
41    /**
42     * POST /administrators
43     * Create a new administrator.
44     */
45    public function create_administrator(StoreAdministratorRequest $request)
46    {
47        if (! $this->canManage()) {
48            return $this->forbidden();
49        }
50
51        try {
52            $data = $request->validated();
53
54            Administrator::create($data);
55
56            $result = Administrator::with('assignedCommercial')->orderBy('name')->get();
57
58            return AdministratorResource::collection($result);
59
60        } catch (\Exception $e) {
61            report(AppException::fromException($e, 'CREATE_ADMINISTRATOR_EXCEPTION'));
62
63            return response(['message' => 'KO', 'error' => $e->getMessage()]);
64        }
65    }
66
67    /**
68     * PUT /administrators/{id}
69     * Update an administrator.
70     */
71    public function update_administrator(UpdateAdministratorRequest $request, $id)
72    {
73        if (! $this->canManage()) {
74            return $this->forbidden();
75        }
76
77        try {
78            $id = (int) $id;
79            $data = $request->validated();
80
81            Administrator::where('id', $id)->update($data);
82
83            $result = Administrator::with('assignedCommercial')->orderBy('name')->get();
84
85            return AdministratorResource::collection($result);
86
87        } catch (\Exception $e) {
88            report(AppException::fromException($e, 'UPDATE_ADMINISTRATOR_EXCEPTION'));
89
90            return response(['message' => 'KO', 'error' => $e->getMessage()]);
91        }
92    }
93
94    /**
95     * DELETE /administrators/{id}
96     * Delete only if not in use by any client.
97     */
98    public function delete_administrator($id)
99    {
100        if (! $this->canDelete()) {
101            return $this->forbidden();
102        }
103
104        try {
105            $id = (int) $id;
106
107            $count = Client::where('administrator_id', $id)->count();
108
109            if ($count > 0) {
110                return response([
111                    'message' => 'KO',
112                    'error' => "Cannot delete because it is in use by {$count} client(s).",
113                ]);
114            }
115
116            Administrator::where('id', $id)->delete();
117
118            $result = Administrator::with('assignedCommercial')->orderBy('name')->get();
119
120            return AdministratorResource::collection($result);
121
122        } catch (\Exception $e) {
123            report(AppException::fromException($e, 'DELETE_ADMINISTRATOR_EXCEPTION'));
124
125            return response(['message' => 'KO', 'error' => $e->getMessage()]);
126        }
127    }
128}