ExternalMessagesController::__invoke()   B
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 36
Code Lines 22

Duplication

Lines 36
Ratio 100 %

Code Coverage

Tests 11
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 22
nc 2
nop 1
dl 36
loc 36
ccs 11
cts 11
cp 1
crap 2
rs 8.8571
c 0
b 0
f 0
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