Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 43
0.00% covered (danger)
0.00%
0 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
OriginController
0.00% covered (danger)
0.00%
0 / 43
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 / 17
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\Origin;
8use App\Repository\ConfigurationRepository;
9use App\Service\PosService;
10use App\Traits\PosTrait;
11use File;
12use Illuminate\Http\Request;
13use Illuminate\Http\Response;
14use Illuminate\Support\Facades\Auth;
15use Illuminate\Support\Facades\DB;
16use Illuminate\Support\Str;
17use Illuminate\Validation\Rule;
18
19class OriginController 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['origins'] = Origin::query()->get();
38
39        return view('admin.configuration.origin')->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
52        try {
53            DB::beginTransaction();
54            $table_name = 'Origin';
55            $origin = $this->repository::storeConfig($table_name, $data);
56            if ($request->hasFile('thumbnail')) {
57                $data['thumbnail'] = $this->FileProcessing($request->file('thumbnail'), PosService::ORIGIN_IMAGE, 429, 500);
58                $origin->update(['thumbnail' => $data['thumbnail']]);
59            }
60            DB::commit();
61
62            return redirect()->back()->with('success', 'Origin Successfully Inserted');
63        } catch (\Throwable $e) {
64            DB::rollBack();
65            report($e);
66
67            return back()->withInput()->with('error', 'Could not save the origin.');
68        }
69
70    }
71
72    /**
73     * Display the specified resource.
74     *
75     * @param  int  $id
76     * @return Response
77     */
78    public function show($id)
79    {
80        return 'hello data';
81    }
82
83    /**
84     * Show the form for editing the specified resource.
85     *
86     * @param  int  $id
87     * @return Response
88     */
89    public function edit($id)
90    {
91        $data['origin'] = Origin::findOrFail($id);
92        $data['origins'] = Origin::query()->get();
93
94        return view('admin.configuration.origin')->with($data);
95    }
96
97    /**
98     * Update the specified resource in storage.
99     *
100     * @param  int  $id
101     * @return Response
102     */
103    public function update(Request $request, $id)
104    {
105        $data['request'] = $request->validate([
106            'name' => ['required', Rule::unique('origins')->ignore($id)],
107            'description' => 'nullable',
108        ]);
109        try {
110            DB::beginTransaction();
111            $data['value'] = ['slug' => Str::slug($request->name), 'created_by' => Auth::user()->id];
112            $data = array_merge($data['value'], $data['request']);
113            if ($request->hasFile('thumbnail')) {
114                $data['thumbnail'] = $this->FileProcessing($request->file('thumbnail'), PosService::ORIGIN_IMAGE, 429, 400);
115            }
116            $origin = Origin::findOrFail($id);
117            $origin->update($data);
118            DB::commit();
119
120            return redirect()->route('admin.origin.index')->with('success', 'Origin Successfully Updated');
121        } catch (\Throwable $e) {
122            DB::rollBack();
123            report($e);
124
125            return back()->withInput()->with('error', 'Could not update the origin.');
126        }
127    }
128
129    /**
130     * Remove the specified resource from storage.
131     *
132     * @param  int  $id
133     * @return Response
134     */
135    public function destroy($id)
136    {
137        $origin = Origin::findOrFail($id);
138        $originImage = $origin->thumbnail;
139        if (file_exists($originImage)) {
140            File::Delete($originImage);
141        }
142        $origin->delete();
143
144        return redirect()->back()->with('delete', 'Origin Deleted Successfully');
145    }
146}