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 MessagesController 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
|
5 |
|
public function __construct(Message $message) |
20
|
|
|
{ |
21
|
5 |
|
parent::__construct(); |
22
|
|
|
|
23
|
5 |
|
$this->message = $message; |
24
|
5 |
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Retrieves a list of all the messages for |
28
|
|
|
* the given document id. |
29
|
|
|
* |
30
|
|
|
* @param string $documentId |
31
|
|
|
* @return \Illuminate\Http\Response |
32
|
|
|
*/ |
33
|
1 |
|
public function index(string $documentId) |
34
|
|
|
{ |
35
|
1 |
|
return $this->message->all($documentId) |
36
|
1 |
|
->getBody() |
37
|
1 |
|
->getContents(); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Gets details for a specific message. |
42
|
|
|
* |
43
|
|
|
* @param string $messageId |
44
|
|
|
* @return \Illuminate\Http\Response |
45
|
|
|
*/ |
46
|
1 |
|
public function show(string $messageId) |
47
|
|
|
{ |
48
|
1 |
|
return $this->message->get($messageId) |
49
|
1 |
|
->getBody() |
50
|
1 |
|
->getContents(); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Sends a message to all signees of a document. |
55
|
|
|
* |
56
|
|
|
* @param Request $request |
57
|
|
|
* @return \Illuminate\Http\Response |
58
|
|
|
*/ |
59
|
1 |
View Code Duplication |
public function store(Request $request) |
|
|
|
|
60
|
|
|
{ |
61
|
1 |
|
$this->validate($request, [ |
62
|
1 |
|
'document_id' => 'required|string|size:36', |
63
|
|
|
'message' => 'required|string|max:255', |
64
|
|
|
'email' => 'required|email|max:255', |
65
|
|
|
'signature' => 'required|string', |
66
|
|
|
'signee_ref' => 'required|string|size:36', |
67
|
|
|
]); |
68
|
|
|
|
69
|
|
|
// this is used to only set the keys which have been sent in |
70
|
|
|
$useKeys = [ |
71
|
1 |
|
'document_id' => 'DocumentID', |
72
|
|
|
'message' => 'EmailMessage', |
73
|
|
|
'email' => 'RecipientEmailAddress', |
74
|
|
|
'signature' => 'SenderSignature', |
75
|
|
|
'signee_ref' => 'SigneeRef', |
76
|
|
|
]; |
77
|
|
|
|
78
|
|
|
// check which keys are available in the request |
79
|
1 |
|
$available = array_intersect(array_keys($useKeys), array_keys($request->all())); |
80
|
|
|
|
81
|
1 |
|
$body = []; |
82
|
|
|
|
83
|
|
|
// set the body up |
84
|
1 |
|
foreach ($available as $use) { |
85
|
1 |
|
$body[$useKeys[$use]] = $request->$use; |
86
|
|
|
} |
87
|
|
|
|
88
|
1 |
|
return $this->message->sendMessage($body) |
89
|
1 |
|
->getBody() |
90
|
1 |
|
->getContents(); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Sends a new message to the Signeeref if the first one failed. |
95
|
|
|
* |
96
|
|
|
* @param Request $request |
97
|
|
|
* @return \Illuminate\Http\Response |
98
|
|
|
*/ |
99
|
2 |
|
public function update(Request $request) |
100
|
|
|
{ |
101
|
2 |
|
$this->validate($request, [ |
102
|
2 |
|
'document_id' => 'required|string|size:36', |
103
|
|
|
'email' => 'nullable|email|max:255', |
104
|
|
|
'replace_email' => 'nullable|string', |
105
|
|
|
'signee_ref' => 'required|string|size:36', |
106
|
|
|
]); |
107
|
|
|
|
108
|
|
|
// this is used to only set the keys which have been sent in |
109
|
|
|
$useKeys = [ |
110
|
2 |
|
'document_id' => 'DocumentID', |
111
|
|
|
'email' => 'RecipientEmailAddress', |
112
|
|
|
'replace_email' => 'ReplaceEmail', |
113
|
|
|
'signee_ref' => 'SigneeRef', |
114
|
|
|
]; |
115
|
|
|
|
116
|
|
|
// check which keys are available in the request |
117
|
2 |
|
$available = array_intersect(array_keys($useKeys), array_keys($request->all())); |
118
|
|
|
|
119
|
2 |
|
$body = []; |
120
|
|
|
|
121
|
|
|
// set the body up |
122
|
2 |
|
foreach ($available as $use) { |
123
|
2 |
|
$body[$useKeys[$use]] = $request->$use; |
124
|
|
|
} |
125
|
|
|
|
126
|
2 |
|
return $this->message->sendNewMessage($body) |
127
|
2 |
|
->getBody() |
128
|
2 |
|
->getContents(); |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
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.