MailingListController::create()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 1
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use App\Models\MailingList;
6
use Illuminate\Http\Request;
7
use App\Jobs\ImportSubscriptions;
8
use Maatwebsite\Excel\Facades\Excel;
9
use App\Http\Requests\MailingListRequest;
10
11
/**
12
 * Class MailingListController.
13
 */
14
class MailingListController extends Controller
15
{
16
    /**
17
     * @param Request $request
18
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
19
     */
20
    public function index(Request $request)
21
    {
22
        $lists = MailingList::filter($request->all())
23
            ->with('subscriptions')
24
            ->paginateFilter(15, ['id', 'name', 'public']);
25
26
        return view('lists.index', compact('lists'));
27
    }
28
29
    /**
30
     * @param MailingList $list
31
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
32
     * @throws \Illuminate\Auth\Access\AuthorizationException
33
     */
34
    public function show(MailingList $list)
35
    {
36
        $this->authorize('view', $list);
37
38
        $list->load('subscriptions', 'campaigns');
39
40
        return view('lists.show', compact('list'));
41
    }
42
43
    /**
44
     * @param MailingList $list
45
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
46
     * @throws \Illuminate\Auth\Access\AuthorizationException
47
     */
48
    public function edit(MailingList $list)
49
    {
50
        $this->authorize('update', $list);
51
52
        return view('lists.edit', compact('list'));
53
    }
54
55
    /**
56
     * @param MailingList $list
57
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
58
     * @throws \Illuminate\Auth\Access\AuthorizationException
59
     */
60
    public function new(MailingList $list)
61
    {
62
        $this->authorize('create', $list);
63
64
        return view('lists.new', compact('list'));
65
    }
66
67
    /**
68
     * @param MailingList $list
69
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
70
     */
71
    public function preImport(MailingList $list)
72
    {
73
        return view('lists.import', compact('list'));
74
    }
75
76
    /**
77
     * @param MailingListRequest $request
78
     * @return \Illuminate\Http\RedirectResponse
79
     */
80
    public function create(MailingListRequest $request)
81
    {
82
        $list = auth()->user()->mailingList()->create($request->all());
83
84
        notify()->flash($list->name, 'success', [
85
            'timer' => 2000,
86
            'text' => 'Successfully created!',
87
        ]);
88
89
        return redirect()->route('lists.show', $list);
90
    }
91
92
    /**
93
     * @param MailingListRequest $request
94
     * @param MailingList $list
95
     * @return \Illuminate\Http\RedirectResponse
96
     * @throws \Illuminate\Auth\Access\AuthorizationException
97
     */
98
    public function update(MailingListRequest $request, MailingList $list)
99
    {
100
        $this->authorize('update', $list);
101
102
        $list->update($request->all());
103
104
        notify()->flash($list->name, 'success', [
105
            'timer' => 2000,
106
            'text' => trans('general.success.update'),
107
        ]);
108
109
        return redirect()->route('lists.show', $list);
0 ignored issues
show
Bug introduced by
$list of type App\Models\MailingList 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

109
        return redirect()->route('lists.show', /** @scrutinizer ignore-type */ $list);
Loading history...
110
    }
111
112
    /**
113
     * @param MailingList $list
114
     * @return \Illuminate\Http\RedirectResponse
115
     * @throws \Illuminate\Auth\Access\AuthorizationException
116
     * @throws \Exception
117
     */
118
    public function delete(MailingList $list)
119
    {
120
        $this->authorize('delete', $list);
121
122
        $list->delete();
123
124
        notify()->flash($list->name, 'success', [
125
            'timer' => 2000,
126
            'text' => trans('general.success.delete'),
127
        ]);
128
129
        return redirect()->route('lists.index');
130
    }
131
132
    /**
133
     * @param MailingList $list
134
     * @throws \Illuminate\Auth\Access\AuthorizationException
135
     */
136
    public function export(MailingList $list)
137
    {
138
        $this->authorize('export', $list);
139
140
        $subscriptions = $list->subscriptions;
141
142
        Excel::create('Subscriptions-'.$list->name, function ($excel) use ($subscriptions) {
143
            $excel->sheet('Subscriptions', function ($sheet) use ($subscriptions) {
144
                $sheet->fromArray($subscriptions);
145
            });
146
        })->export('csv');
147
    }
148
149
    /**
150
     * @param Request $request
151
     * @param MailingList $list
152
     * @return \Illuminate\Http\RedirectResponse
153
     * @throws \Illuminate\Auth\Access\AuthorizationException
154
     */
155
    public function import(Request $request, MailingList $list)
156
    {
157
        $this->authorize('import', $list);
158
159
        if ($request->file('file')->isValid()) {
160
            $file = $request->file('file')->getRealPath();
161
162
            $results = Excel::load($file)->toArray();
163
164
            $this->dispatch(new ImportSubscriptions($request->user(), $list, $results));
165
166
            notify()->flash('Wohoo!', 'success', [
167
                'timer' => 2000,
168
                'text' => trans('general.success.import'),
169
            ]);
170
171
            return redirect()->route('lists.show', $list);
0 ignored issues
show
Bug introduced by
$list of type App\Models\MailingList 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

171
            return redirect()->route('lists.show', /** @scrutinizer ignore-type */ $list);
Loading history...
172
        }
173
    }
174
}
175