Passed
Pull Request — master (#71)
by Adeniyi
05:43
created

AddressController   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 156
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 156
rs 10
c 0
b 0
f 0
wmc 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 12 1
A destroy() 0 8 1
A update() 0 9 1
A __construct() 0 12 1
A store() 0 6 1
A edit() 0 27 2
A show() 0 3 1
A index() 0 13 2
1
<?php
2
3
namespace App\Http\Controllers\Admin\Addresses;
4
5
use App\Shop\Addresses\Address;
6
use App\Shop\Addresses\Repositories\AddressRepository;
7
use App\Shop\Addresses\Repositories\Interfaces\AddressRepositoryInterface;
8
use App\Shop\Addresses\Requests\CreateAddressRequest;
9
use App\Shop\Addresses\Requests\UpdateAddressRequest;
10
use App\Shop\Addresses\Transformations\AddressTransformable;
11
use App\Shop\Cities\City;
12
use App\Shop\Cities\Repositories\Interfaces\CityRepositoryInterface;
13
use App\Shop\Countries\Country;
14
use App\Shop\Countries\Repositories\CountryRepository;
15
use App\Shop\Countries\Repositories\Interfaces\CountryRepositoryInterface;
16
use App\Shop\Customers\Repositories\Interfaces\CustomerRepositoryInterface;
17
use App\Http\Controllers\Controller;
18
use App\Shop\Provinces\Repositories\Interfaces\ProvinceRepositoryInterface;
19
20
class AddressController extends Controller
21
{
22
    use AddressTransformable;
0 ignored issues
show
introduced by
The trait App\Shop\Addresses\Trans...ns\AddressTransformable requires some properties which are not provided by App\Http\Controllers\Adm...esses\AddressController: $name, $customer_id, $province_id, $city_id, $country_id
Loading history...
23
24
    private $addressRepo;
25
    private $customerRepo;
26
    private $countryRepo;
27
    private $provinceRepo;
28
    private $cityRepo;
29
30
    public function __construct(
31
        AddressRepositoryInterface $addressRepository,
32
        CustomerRepositoryInterface $customerRepository,
33
        CountryRepositoryInterface $countryRepository,
34
        ProvinceRepositoryInterface $provinceRepository,
35
        CityRepositoryInterface $cityRepository
36
    ) {
37
        $this->addressRepo = $addressRepository;
38
        $this->customerRepo = $customerRepository;
39
        $this->countryRepo = $countryRepository;
40
        $this->provinceRepo = $provinceRepository;
41
        $this->cityRepo = $cityRepository;
42
    }
43
    /**
44
     * Display a listing of the resource.
45
     *
46
     * @return \Illuminate\Http\Response
47
     */
48
    public function index()
49
    {
50
        $list = $this->addressRepo->listAddress('created_at', 'desc');
51
52
        if (request()->has('q')) {
53
            $list = $this->addressRepo->searchAddress(request()->input('q'));
54
        }
55
56
        $addresses = $list->map(function (Address $address) {
57
            return $this->transformAddress($address);
58
        })->all();
59
60
        return view('admin.addresses.list', ['addresses' => $this->addressRepo->paginateArrayResults($addresses)]);
0 ignored issues
show
Bug Best Practice introduced by
The expression return view('admin.addre...ayResults($addresses))) returns the type Illuminate\View\View which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
61
    }
62
63
    /**
64
     * Show the form for creating a new resource.
65
     *
66
     * @return \Illuminate\Http\Response
67
     */
68
    public function create()
69
    {
70
        $countries = $this->countryRepo->listCountries();
71
        $country = $this->countryRepo->findCountryById(1);
72
73
        $customers = $this->customerRepo->listCustomers();
74
75
        return view('admin.addresses.create', [
0 ignored issues
show
Bug Best Practice introduced by
The expression return view('admin.addre...op\Cities\City::all())) returns the type Illuminate\View\View which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
76
            'customers' => $customers,
77
            'countries' => $countries,
78
            'provinces' => $country->provinces,
79
            'cities' => City::all()
80
        ]);
81
    }
82
83
    /**
84
     * Store a newly created resource in storage.
85
     *
86
     * @param  CreateAddressRequest $request
87
     * @return \Illuminate\Http\Response
88
     */
89
    public function store(CreateAddressRequest $request)
90
    {
91
        $this->addressRepo->createAddress($request->except('_token', '_method'));
92
93
        $request->session()->flash('message', 'Creation successful');
94
        return redirect()->route('admin.addresses.index');
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect()->route('admin.addresses.index') returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
95
    }
96
97
    /**
98
     * Display the specified resource.
99
     *
100
     * @param  int  $id
101
     * @return \Illuminate\Http\Response
102
     */
103
    public function show(int $id)
104
    {
105
        return view('admin.addresses.show', ['address' => $this->addressRepo->findAddressById($id)]);
0 ignored issues
show
Bug Best Practice introduced by
The expression return view('admin.addre...>findAddressById($id))) returns the type Illuminate\View\View which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
106
    }
107
108
    /**
109
     * Show the form for editing the specified resource.
110
     *
111
     * @param  int  $id
112
     * @return \Illuminate\Http\Response
113
     */
114
    public function edit(int $id)
115
    {
116
        $countries = $this->countryRepo->listCountries();
117
118
        $country = $countries->filter(function ($country) {
119
            return $country == env('COUNTRY_ID', 1);
120
        })->first();
121
122
        $countryRepo = new CountryRepository(new Country());
123
        if (!empty($country)) {
124
            $countryRepo = new CountryRepository($country);
125
        }
126
127
        $address = $this->addressRepo->findAddressById($id);
128
        $addressRepo = new AddressRepository($address);
129
        $customer = $addressRepo->findCustomer();
130
131
        return view('admin.addresses.edit', [
0 ignored issues
show
Bug Best Practice introduced by
The expression return view('admin.addre...rId' => $customer->id)) returns the type Illuminate\View\View which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
132
            'address' => $address,
133
            'countries' => $countries,
134
            'countryId' => $address->country->id,
135
            'provinces' => $countryRepo->findProvinces(),
136
            'provinceId' => $address->province->id,
137
            'cities' => $this->cityRepo->listCities(),
138
            'cityId' => $address->city_id,
139
            'customers' => $this->customerRepo->listCustomers(),
140
            'customerId' => $customer->id
141
        ]);
142
    }
143
144
    /**
145
     * Update the specified resource in storage.
146
     *
147
     * @param  UpdateAddressRequest $request
148
     * @param  int  $id
149
     * @return \Illuminate\Http\Response
150
     */
151
    public function update(UpdateAddressRequest $request, $id)
152
    {
153
        $address = $this->addressRepo->findAddressById($id);
154
155
        $update = new AddressRepository($address);
156
        $update->updateAddress($request->except('_method', '_token'));
157
158
        $request->session()->flash('message', 'Update successful');
159
        return redirect()->route('admin.addresses.edit', $id);
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect()->route...n.addresses.edit', $id) returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
Bug introduced by
$id of type integer is incompatible with the type array expected by parameter $parameters of Illuminate\Routing\Redirector::route(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

159
        return redirect()->route('admin.addresses.edit', /** @scrutinizer ignore-type */ $id);
Loading history...
160
    }
161
162
    /**
163
     * Remove the specified resource from storage.
164
     *
165
     * @param  int  $id
166
     * @return \Illuminate\Http\Response
167
     */
168
    public function destroy($id)
169
    {
170
        $address = $this->addressRepo->findAddressById($id);
171
        $delete = new AddressRepository($address);
172
        $delete->deleteAddress();
173
174
        request()->session()->flash('message', 'Delete successful');
175
        return redirect()->route('admin.addresses.index');
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect()->route('admin.addresses.index') returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
176
    }
177
}
178