Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
| Total | |
0.00% |
0 / 1 |
|
41.67% |
5 / 12 |
CRAP | |
69.88% |
58 / 83 |
| CompanyController | |
0.00% |
0 / 1 |
|
41.67% |
5 / 12 |
49.42 | |
69.88% |
58 / 83 |
| __construct | |
100.00% |
1 / 1 |
1 | |
100.00% |
2 / 2 |
|||
| index | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
| store | |
0.00% |
0 / 1 |
3.01 | |
90.00% |
9 / 10 |
|||
| show | |
100.00% |
1 / 1 |
1 | |
100.00% |
2 / 2 |
|||
| update | |
0.00% |
0 / 1 |
3.01 | |
90.91% |
10 / 11 |
|||
| createHistory | |
0.00% |
0 / 1 |
9.08 | |
90.00% |
18 / 20 |
|||
| destroy | |
100.00% |
1 / 1 |
1 | |
100.00% |
3 / 3 |
|||
| validateData | |
100.00% |
1 / 1 |
2 | |
100.00% |
6 / 6 |
|||
| updateSites | |
0.00% |
0 / 1 |
5.16 | |
58.33% |
7 / 12 |
|||
| updateSite | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 3 |
|||
| addSite | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 6 |
|||
| delSite | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 7 |
|||
| 1 | <?php |
| 2 | |
| 3 | namespace Qmp\Laravel\Company\Controllers; |
| 4 | |
| 5 | use Illuminate\Database\Eloquent\Collection; |
| 6 | use Illuminate\Http\JsonResponse; |
| 7 | use Illuminate\Http\Response; |
| 8 | use Illuminate\Http\Request; |
| 9 | use Illuminate\Support\Facades\Log; |
| 10 | use Qmp\Laravel\MicroService\Controllers\AbstractMicroServiceController; |
| 11 | use Illuminate\Validation\Rule; |
| 12 | use Exception; |
| 13 | |
| 14 | use Qmp\Laravel\Company\Models\Company; |
| 15 | |
| 16 | use Qmp\Laravel\MicroService\Client\Client; |
| 17 | use Qmp\Laravel\MicroService\Client\Tools\Request as ClientRequest; |
| 18 | |
| 19 | class CompanyController extends AbstractMicroServiceController |
| 20 | { |
| 21 | public function __construct(Request $request) |
| 22 | { |
| 23 | //$this->middleware('perm:xxx'); |
| 24 | |
| 25 | parent::__construct($request); |
| 26 | } |
| 27 | |
| 28 | /** |
| 29 | * Display a listing of the resource. |
| 30 | * |
| 31 | * @return Collection |
| 32 | */ |
| 33 | public function index(): Collection |
| 34 | { |
| 35 | return Company::all(); |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * Store a newly created resource in storage. |
| 40 | * |
| 41 | * @param \Illuminate\Http\Request $request |
| 42 | * @return \Illuminate\Http\JsonResponse |
| 43 | */ |
| 44 | public function store(Request $request): JsonResponse |
| 45 | { |
| 46 | $validatedData = $this->validateData($request); |
| 47 | |
| 48 | try { |
| 49 | $validatedData = array_merge($validatedData, ['history' => []]); |
| 50 | if (!isset($validatedData['sites'])) { |
| 51 | $validatedData['sites'] = []; |
| 52 | } |
| 53 | |
| 54 | $company = Company::create($validatedData); |
| 55 | |
| 56 | $this->updateSites($company, $validatedData['sites']); |
| 57 | |
| 58 | return response()->json(['status' => 'ok', 'datas' => $company->ToArray()], Response::HTTP_CREATED); |
| 59 | } catch (Exception $e) { |
| 60 | Log::debug('Unable to store company : ' . var_export(['message' => $e->getMessage(), 'line' => $e->getLine(), 'file' => $e->getFile()], true)); |
| 61 | return response()->json(['status' => 'ko', 'message' => $e->getMessage()], Response::HTTP_UNPROCESSABLE_ENTITY); |
| 62 | } |
| 63 | |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Display the specified resource. |
| 68 | * |
| 69 | * @return \Illuminate\Http\JsonResponse |
| 70 | */ |
| 71 | public function show($id): JsonResponse |
| 72 | { |
| 73 | $company = Company::findOrFail($id); |
| 74 | return response()->json($company); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Update the specified resource in storage. |
| 79 | * |
| 80 | * @param \Illuminate\Http\Request $request |
| 81 | * @return \Illuminate\Http\JsonResponse |
| 82 | */ |
| 83 | public function update(Request $request, $id): JsonResponse |
| 84 | { |
| 85 | $validatedData = $this->validateData($request, $id); |
| 86 | $model = Company::findOrFail($id); |
| 87 | |
| 88 | try { |
| 89 | // Save old values to history object |
| 90 | $validatedData['history'] = $this->createHistory($model, $validatedData); |
| 91 | if (!isset($validatedData['sites'])) { |
| 92 | $validatedData['sites'] = []; |
| 93 | } |
| 94 | |
| 95 | $this->updateSites($model, $validatedData['sites']); |
| 96 | |
| 97 | $result = $model->update($validatedData); |
| 98 | |
| 99 | return response()->json(["status" => "ok", "datas" => $model->toArray()], Response::HTTP_OK); |
| 100 | } catch (Exception $e) { |
| 101 | Log::debug('Unable to update company: ' . var_export(['message' => $e->getMessage(), 'line' => $e->getLine(), 'file' => $e->getFile()], true)); |
| 102 | return response()->json(["status" => "ko", "message" => $e->getMessage()], Response::HTTP_UNPROCESSABLE_ENTITY); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | protected function createHistory(Company $model, array $dataToSave) |
| 107 | { |
| 108 | $keysToRemove = ['history', 'contacts', '_id', 'created_at', 'updated_at']; |
| 109 | $old = array_diff_key($model->toArray(), array_flip($keysToRemove)); |
| 110 | $dataToSave = array_diff_key($dataToSave, array_flip($keysToRemove)); |
| 111 | $history = !is_array($model->history) ? [] : $model->history; |
| 112 | $updateHistory = false; |
| 113 | $changes = []; |
| 114 | foreach($dataToSave as $key => $value) { |
| 115 | if (!array_key_exists($key, $old)) { |
| 116 | $updateHistory = true; |
| 117 | $changes[$key] = null; |
| 118 | } else if ($value !== $old[$key]) { |
| 119 | $updateHistory = true; |
| 120 | $changes[$key] = $old[$key]; |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | if ($updateHistory) { |
| 125 | $date = $model->updated_at ? $model->updated_at : new \DateTime(); |
| 126 | if (is_a($date, Illuminate\Support\Carbon::class) || is_a($date, \DateTime::class)) { |
| 127 | $date = $date->format('Y-m-d H:i:s'); |
| 128 | } |
| 129 | $history[(string) $date] = $changes; |
| 130 | } |
| 131 | |
| 132 | krsort($history); |
| 133 | |
| 134 | return $history; |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Remove the specified resource from storage. |
| 139 | * |
| 140 | * @return \Illuminate\Http\JsonResponse |
| 141 | */ |
| 142 | public function destroy($id): JsonResponse |
| 143 | { |
| 144 | $company = Company::findOrFail($id); |
| 145 | $company->delete(); |
| 146 | return response()->json(["status" => "ok"]); |
| 147 | } |
| 148 | |
| 149 | protected function validateData(Request $request, $id = null) |
| 150 | { |
| 151 | $rules =[ |
| 152 | 'name' => 'required', |
| 153 | 'adress' => 'required', |
| 154 | 'city' => 'required', |
| 155 | 'zipcode' => 'required', |
| 156 | 'country' => 'required', |
| 157 | 'publisher' => 'required|in:0,1', |
| 158 | 'advertiser' => 'required|in:0,1', |
| 159 | 'tva' => 'required', |
| 160 | 'vatRate' => 'required', |
| 161 | 'bic' => 'present', |
| 162 | 'iban' => 'present', |
| 163 | 'siret' => 'present', |
| 164 | 'contacts' => 'present|array', |
| 165 | 'active' => 'required|present', |
| 166 | 'description' => 'present', |
| 167 | 'sites' => 'nullable|array', |
| 168 | ]; |
| 169 | |
| 170 | if ($id != null) { |
| 171 | $rules['_id'] = [ |
| 172 | 'required', |
| 173 | Rule::in([$id]) |
| 174 | ]; |
| 175 | } |
| 176 | |
| 177 | return $request->validate($rules); |
| 178 | } |
| 179 | |
| 180 | protected function updateSites($company, $sites) |
| 181 | { |
| 182 | $sitesToRemove = $company->sites; |
| 183 | $sitesToSave = []; |
| 184 | |
| 185 | foreach($sites as $site) { |
| 186 | if (($key = array_search($site, $sitesToRemove)) !== false) { |
| 187 | unset($sitesToRemove[$key]); |
| 188 | } |
| 189 | $this->updateSite($company->_id, $site); |
| 190 | |
| 191 | $sitesToSave[] = $site; |
| 192 | } |
| 193 | |
| 194 | foreach ($sitesToRemove as $site) { |
| 195 | $this->updateSite(null, $site); |
| 196 | } |
| 197 | |
| 198 | $company->sites = $sitesToSave; |
| 199 | $company->save(); |
| 200 | } |
| 201 | |
| 202 | protected function updateSite($companyId, $siteId) |
| 203 | { |
| 204 | $request = ClientRequest::createObject('service_site', "site/$siteId/update-company", ['body' => ['company_id' => $companyId]]); |
| 205 | $response = Client::systemSend('post', $request); |
| 206 | } |
| 207 | |
| 208 | public function addSite($companyId, $siteId) |
| 209 | { |
| 210 | $company = Company::find($companyId); |
| 211 | $sites = $company->sites; |
| 212 | $sites[] = $siteId; |
| 213 | $company->sites = array_unique($sites); |
| 214 | $company->save(); |
| 215 | |
| 216 | return response()->json(['status' => 'ok']); |
| 217 | } |
| 218 | |
| 219 | public function delSite($companyId, $siteId) |
| 220 | { |
| 221 | $company = Company::find($companyId); |
| 222 | $sites = $company->sites; |
| 223 | $company->sites = array_filter($sites, function($site) use ($siteId) { |
| 224 | return $site !== $siteId; |
| 225 | }); |
| 226 | $company->save(); |
| 227 | |
| 228 | return response()->json(['status' => 'ok']); |
| 229 | } |
| 230 | } |