1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BRKFun\NotificationOptions\Controllers; |
4
|
|
|
|
5
|
|
|
use BRKFun\NotificationOptions\Exceptions\NotificationNotFoundException; |
6
|
|
|
use BRKFun\NotificationOptions\Models\NotificationOption; |
7
|
|
|
use BRKFun\NotificationOptions\Requests\NotificationRequest; |
8
|
|
|
use Illuminate\Contracts\View\Factory; |
9
|
|
|
use Illuminate\Database\Eloquent\Builder; |
10
|
|
|
use Illuminate\Database\Eloquent\Model; |
11
|
|
|
use Illuminate\Http\Request; |
12
|
|
|
use Illuminate\Routing\Controller; |
13
|
|
|
use Illuminate\View\View; |
14
|
|
|
|
15
|
|
|
class NotificationController extends Controller |
16
|
|
|
{ |
17
|
|
|
private $notificationName; |
18
|
|
|
private $email; |
19
|
|
|
private $token; |
20
|
|
|
|
21
|
|
View Code Duplication |
public function store(NotificationRequest $request) |
|
|
|
|
22
|
|
|
{ |
23
|
|
|
$this->setAttributes($request); |
24
|
|
|
try { |
25
|
|
|
$notificationOption = $this->controlAttributes(); |
26
|
|
|
$notificationOption->value = 1; |
27
|
|
|
$notificationOption->token = tokenSetter(); |
28
|
|
|
$notificationOption->save(); |
29
|
|
|
} catch (NotificationNotFoundException $exception) { |
30
|
|
|
return $this->failureResponse($exception); |
31
|
|
|
} |
32
|
|
|
return $this->successResponse(); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param NotificationRequest $request |
37
|
|
|
* @return Factory|View |
38
|
|
|
*/ |
39
|
|
View Code Duplication |
public function destroy(NotificationRequest $request) |
|
|
|
|
40
|
|
|
{ |
41
|
|
|
$this->setAttributes($request); |
42
|
|
|
try { |
43
|
|
|
$notificationOption = $this->controlAttributes(); |
44
|
|
|
$notificationOption->value = 0; |
45
|
|
|
$notificationOption->token = tokenSetter(); |
46
|
|
|
$notificationOption->save(); |
47
|
|
|
} catch (NotificationNotFoundException $exception) { |
48
|
|
|
return $this->failureResponse($exception); |
49
|
|
|
} |
50
|
|
|
return $this->successResponse(); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
protected function successResponse() |
54
|
|
|
{ |
55
|
|
|
return view(config('notification-options.successfulPageBlade')); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
protected function failureResponse(NotificationNotFoundException $exception) |
59
|
|
|
{ |
60
|
|
|
return view(config('notification-options.unsuccessfulPageBlade'), ['message' => $exception->getMessage()]); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @return Builder|Model|object|null |
65
|
|
|
* @throws NotificationNotFoundException |
66
|
|
|
*/ |
67
|
|
|
protected function controlAttributes() |
68
|
|
|
{ |
69
|
|
|
$notificationOption = NotificationOption::query()->where('key', $this->notificationName)->first(); |
70
|
|
|
if (!$notificationOption) { |
71
|
|
|
throw new NotificationNotFoundException(); |
72
|
|
|
} |
73
|
|
|
if ($notificationOption->notifiable->email !== $this->email || $notificationOption->token !== $this->token) { |
74
|
|
|
throw new NotificationNotFoundException('Your data is wrong'); |
75
|
|
|
} |
76
|
|
|
return $notificationOption; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
protected function setAttributes(Request $request) |
80
|
|
|
{ |
81
|
|
|
$this->token = $request->get('token'); |
82
|
|
|
$this->email = $request->get('email'); |
83
|
|
|
$this->notificationName = $request->get('notification_name'); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.