NotificationController   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 71
Duplicated Lines 36.62 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
dl 26
loc 71
rs 10
c 0
b 0
f 0
wmc 11
lcom 1
cbo 5

6 Methods

Rating   Name   Duplication   Size   Complexity  
A store() 13 13 2
A destroy() 13 13 2
A successResponse() 0 4 1
A failureResponse() 0 4 1
A controlAttributes() 0 11 4
A setAttributes() 0 6 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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