ExternalMessagesController   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 61
Duplicated Lines 59.02 %

Coupling/Cohesion

Components 0
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 36
loc 61
ccs 15
cts 15
cp 1
rs 10
wmc 3
lcom 0
cbo 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
B __invoke() 36 36 2

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 Sausin\Signere\Http\Controllers\Admin;
4
5
use Sausin\Signere\Message;
6
use Illuminate\Http\Request;
7
use Sausin\Signere\Http\Controllers\Controller;
8
9
class ExternalMessagesController extends Controller
10
{
11
    /** @var \Sausin\Signere\Message */
12
    protected $message;
13
14
    /**
15
     * Create a new controller instance.
16
     *
17
     * @param  \Sausin\Signere\Message $message
18
     */
19 1
    public function __construct(Message $message)
20
    {
21 1
        parent::__construct();
22
23 1
        $this->message = $message;
24 1
    }
25
26
    /**
27
     * Sends a message to an external person
28
     * with a link/URL to view a document.
29
     *
30
     * @param  Request $request
31
     * @return \Illuminate\Http\Response
32
     */
33 1 View Code Duplication
    public function __invoke(Request $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...
34
    {
35 1
        $this->validate($request, [
36 1
            'document_id' => 'required|string|size:36',
37
            'message' => 'required|string|max:255',
38
            'email' => 'required|email|max:255',
39
            'phone_number' => [
40
                'required',
41
                'regex:/^\+47\d{8}$/i',
42
            ],
43
            'signature' => 'required|string',
44
        ]);
45
46
        // this is used to only set the keys which have been sent in
47
        $useKeys = [
48 1
            'document_id' => 'DocumentID',
49
            'message' => 'EmailMessage',
50
            'email' => 'RecipientEmailAddress',
51
            'phone_number' => 'MobileNumber',
52
            'signature' => 'SenderSignature',
53
        ];
54
55
        // check which keys are available in the request
56 1
        $available = array_intersect(array_keys($useKeys), array_keys($request->all()));
57
58 1
        $body = [];
59
60
        // set the body up
61 1
        foreach ($available as $use) {
62 1
            $body[$useKeys[$use]] = $request->$use;
63
        }
64
65 1
        return $this->message->sendExternalMessage($body)
66 1
                ->getBody()
67 1
                ->getContents();
68
    }
69
}
70