Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 42
0.00% covered (danger)
0.00%
0 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
ColorController
0.00% covered (danger)
0.00%
0 / 42
0.00% covered (danger)
0.00%
0 / 7
156
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 index
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 store
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
12
 show
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 edit
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 update
0.00% covered (danger)
0.00%
0 / 16
0.00% covered (danger)
0.00%
0 / 1
12
 destroy
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2
3namespace App\Http\Controllers\admin;
4
5use App\Http\Controllers\Controller;
6use App\Http\Requests\ConfigurationRequest;
7use App\Models\Color;
8use App\Repository\ConfigurationRepository;
9use App\Service\PosService;
10use App\Traits\PosTrait;
11use Illuminate\Http\Request;
12use Illuminate\Http\Response;
13use Illuminate\Support\Facades\Auth;
14use Illuminate\Support\Facades\DB;
15use Illuminate\Support\Facades\File;
16use Illuminate\Support\Str;
17use Illuminate\Validation\Rule;
18
19class ColorController extends Controller
20{
21    use PosTrait;
22
23    protected $repository;
24
25    public function __construct(ConfigurationRepository $repository)
26    {
27        $this->repository = $repository;
28    }
29
30    /**
31     * Display a listing of the resource.
32     *
33     * @return Response
34     */
35    public function index()
36    {
37        $data['colors'] = Color::query()->get();
38
39        return view('admin.configuration.color')->with($data);
40    }
41
42    /**
43     * Store a newly created resource in storage.
44     *
45     * @param  Request  $request
46     * @return Response
47     */
48    public function store(ConfigurationRequest $request)
49    {
50        $data['request'] = $request->validated();
51        try {
52            DB::beginTransaction();
53            $table_name = 'Color';
54            $color = $this->repository::storeConfig($table_name, $data);
55            if ($request->hasFile('thumbnail')) {
56                $data['thumbnail'] = $this->FileProcessing($request->file('thumbnail'), PosService::COLOR_IMAGE, 429, 500);
57                $color->update(['thumbnail' => $data['thumbnail']]);
58            }
59            DB::commit();
60
61            return redirect()->back()->with('success', 'Color Successfully Inserted');
62        } catch (\Throwable $e) {
63            DB::rollBack();
64            report($e);
65
66            return back()->withInput()->with('error', 'Could not save the color.');
67        }
68
69    }
70
71    /**
72     * Display the specified resource.
73     *
74     * @param  int  $id
75     * @return Response
76     */
77    public function show($id)
78    {
79        return 'hello data';
80    }
81
82    /**
83     * Show the form for editing the specified resource.
84     *
85     * @param  int  $id
86     * @return Response
87     */
88    public function edit($id)
89    {
90        $data['color'] = Color::findOrFail($id);
91        $data['colors'] = Color::query()->get();
92
93        return view('admin.configuration.color')->with($data);
94    }
95
96    /**
97     * Update the specified resource in storage.
98     *
99     * @param  int  $id
100     * @return Response
101     */
102    public function update(Request $request, $id)
103    {
104        $data['request'] = $request->validate([
105            'name' => ['required', Rule::unique('colors')->ignore($id)],
106            'description',
107        ]);
108        try {
109            DB::beginTransaction();
110            $data['value'] = ['slug' => Str::slug($request->name), 'created_by' => Auth::user()->id];
111            $data = array_merge($data['value'], $data['request']);
112            if ($request->hasFile('thumbnail')) {
113                $data['thumbnail'] = $this->FileProcessing($request->file('thumbnail'), PosService::COLOR_IMAGE, 429, 400);
114            }
115            $category = Color::findOrFail($id)->update($data);
116            DB::commit();
117
118            return redirect()->route('admin.color.index')->with('success', 'Color Successfully Updated');
119        } catch (\Throwable $e) {
120            DB::rollBack();
121            report($e);
122
123            return back()->withInput()->with('error', 'Could not update the color.');
124        }
125    }
126
127    /**
128     * Remove the specified resource from storage.
129     *
130     * @param  int  $id
131     * @return Response
132     */
133    public function destroy($id)
134    {
135        $color = Color::findOrFail($id);
136        $colorImage = $color->thumbnail;
137        if (file_exists($colorImage)) {
138            File::Delete($colorImage);
139        }
140        $color->delete();
141
142        return redirect()->back()->with('delete', 'Color Deleted Successfully');
143    }
144}