Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
| Total | |
0.00% |
0 / 1 |
|
71.43% |
5 / 7 |
CRAP | |
82.35% |
28 / 34 |
| ConfigController | |
0.00% |
0 / 1 |
|
71.43% |
5 / 7 |
11.66 | |
82.35% |
28 / 34 |
| __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.24 | |
70.00% |
7 / 10 |
|||
| show | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
| update | |
0.00% |
0 / 1 |
2.15 | |
66.67% |
6 / 9 |
|||
| destroy | |
100.00% |
1 / 1 |
1 | |
100.00% |
5 / 5 |
|||
| validateData | |
100.00% |
1 / 1 |
2 | |
100.00% |
6 / 6 |
|||
| 1 | <?php |
| 2 | |
| 3 | namespace Qmp\Laravel\Tracker\Controllers; |
| 4 | |
| 5 | use App\User; |
| 6 | use Illuminate\Http\Response; |
| 7 | use Illuminate\Http\Request; |
| 8 | use Illuminate\Support\Arr; |
| 9 | use Illuminate\Support\Facades\Log; |
| 10 | use Illuminate\Support\Facades\Redis; |
| 11 | use Illuminate\Support\Str; |
| 12 | use Illuminate\Validation\Rule; |
| 13 | use Qmp\Laravel\MicroService\Controllers\AbstractMicroServiceController; |
| 14 | use Qmp\Laravel\Tracker\Models\Config; |
| 15 | |
| 16 | class ConfigController extends AbstractMicroServiceController |
| 17 | { |
| 18 | public function __construct(Request $request) |
| 19 | { |
| 20 | parent::__construct($request); |
| 21 | } |
| 22 | |
| 23 | /** |
| 24 | * Display a listing of the resource. |
| 25 | * |
| 26 | * @return \Illuminate\Http\JsonResponse |
| 27 | */ |
| 28 | public function index() |
| 29 | { |
| 30 | |
| 31 | |
| 32 | return response()->json(Config::all()); |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * Store a newly created resource in storage. |
| 37 | * |
| 38 | * @param \Illuminate\Http\Request $request |
| 39 | * @return \Illuminate\Http\JsonResponse |
| 40 | */ |
| 41 | public function store(Request $request) |
| 42 | { |
| 43 | $validatedData = $this->validateData($request); |
| 44 | |
| 45 | try { |
| 46 | if (is_null($request->id)) { |
| 47 | $validatedData = array_merge(['id' => uniqid()], $validatedData); |
| 48 | } |
| 49 | |
| 50 | $validatedData = Arr::add($validatedData, 'mongo.database', 'tracker'); |
| 51 | |
| 52 | $config = Config::create($validatedData); |
| 53 | |
| 54 | Redis::publish('tracker', json_encode(['action' => 'save-config', 'configId' => $config->id, 'message' => $config->name . " - save-config"])); |
| 55 | return response()->json(['status' => 'ok', 'datas' => $config->ToArray()], Response::HTTP_CREATED); |
| 56 | } catch (\Exception $e) { |
| 57 | Log::debug('Unable to store config tracker :' . var_export(['message' => $e->getMessage(), 'line' => $e->getLine(), 'file' => $e->getFile()], true)); |
| 58 | return response()->json(['status' => 'ko', 'message' => $e->getMessage()], Response::HTTP_UNPROCESSABLE_ENTITY); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Display the specified resource. |
| 64 | * |
| 65 | * @return \Illuminate\Http\JsonResponse |
| 66 | */ |
| 67 | public function show(Config $config) |
| 68 | { |
| 69 | return response()->json($config); |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Update the specified resource in storage. |
| 74 | * |
| 75 | * @param \Illuminate\Http\Request $request |
| 76 | * @return \Illuminate\Http\JsonResponse |
| 77 | */ |
| 78 | public function update(Request $request, $id) |
| 79 | { |
| 80 | $validatedData = $this->validateData($request, $id); |
| 81 | |
| 82 | try { |
| 83 | $model = Config::findOrFail($id); |
| 84 | |
| 85 | $validatedData = Arr::add($validatedData, 'mongo.database', 'tracker'); |
| 86 | |
| 87 | $model->update($validatedData); |
| 88 | |
| 89 | Redis::publish('tracker', json_encode(['action' => 'update-config', 'configId' => $model->id, 'message' => $model->name . " - update-config"])); |
| 90 | return response()->json(["status" => "ok", "datas" => $model->toArray()], Response::HTTP_OK); |
| 91 | } catch (\Exception $e) { |
| 92 | Log::debug('Unable to update config tracker:' . var_export(['message' => $e->getMessage(), 'line' => $e->getLine(), 'file' => $e->getFile()], true)); |
| 93 | return response()->json(["status" => "ko", "message" => $e->getMessage()], Response::HTTP_UNPROCESSABLE_ENTITY); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Remove the specified resource from storage. |
| 99 | * |
| 100 | * @return \Illuminate\Http\Response |
| 101 | */ |
| 102 | public function destroy($id) |
| 103 | { |
| 104 | $config = Config::find($id); |
| 105 | $name = $config->name; |
| 106 | $config->delete(); |
| 107 | |
| 108 | Redis::publish('tracker', json_encode(['action' => 'delete-config', 'configId' => $id, 'message' => $name . " - delete-config"])); |
| 109 | return response()->json(["status" => "ok"]); |
| 110 | } |
| 111 | |
| 112 | protected function validateData(Request $request, $id = null) |
| 113 | { |
| 114 | $rules = [ |
| 115 | 'name' => 'required', |
| 116 | 'active' => 'required', |
| 117 | 'ip' => 'required', |
| 118 | 'mongo.port' => 'required', |
| 119 | 'mongo.user' => 'required', |
| 120 | 'mongo.password' => 'required', |
| 121 | 'redis.port' => 'required', |
| 122 | 'redis.user' => 'required', |
| 123 | 'redis.password' => 'required', |
| 124 | 'ssh.port' => 'required', |
| 125 | 'ssh.user' => 'required', |
| 126 | 'ssh.password' => 'required', |
| 127 | 'ssh.root' => 'required', |
| 128 | 'ssh.active' => 'required', |
| 129 | ]; |
| 130 | |
| 131 | if ($id != null) { |
| 132 | $rules['id'] = [ |
| 133 | 'required', |
| 134 | Rule::in([$id]) |
| 135 | ]; |
| 136 | } |
| 137 | |
| 138 | return $request->validate($rules); |
| 139 | } |
| 140 | } |