|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Sausin\Signere\Http\Controllers\Admin; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Http\Request; |
|
6
|
|
|
use Sausin\Signere\Receiver; |
|
7
|
|
|
use Illuminate\Support\Facades\Config; |
|
8
|
|
|
use Sausin\Signere\Http\Controllers\Controller; |
|
9
|
|
|
|
|
10
|
|
|
class ReceiverController extends Controller |
|
11
|
|
|
{ |
|
12
|
|
|
/** @var \Sausin\Signere\Receiver */ |
|
13
|
|
|
protected $receiver; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Create a new controller instance. |
|
17
|
|
|
* |
|
18
|
|
|
* @param \Sausin\Signere\Receiver $receiver |
|
19
|
|
|
*/ |
|
20
|
4 |
|
public function __construct(Receiver $receiver) |
|
21
|
|
|
{ |
|
22
|
4 |
|
parent::__construct(); |
|
23
|
|
|
|
|
24
|
4 |
|
$this->receiver = $receiver; |
|
25
|
4 |
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Returns all receivers. |
|
29
|
|
|
* |
|
30
|
|
|
* @return \Illuminate\Http\Response |
|
31
|
|
|
*/ |
|
32
|
1 |
|
public function index() |
|
33
|
|
|
{ |
|
34
|
1 |
|
return $this->receiver->get(Config::get('signere.id')) |
|
35
|
1 |
|
->getBody() |
|
36
|
1 |
|
->getContents(); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Returns the given receiver. |
|
41
|
|
|
* |
|
42
|
|
|
* @param string $receiverId |
|
43
|
|
|
* @return \Illuminate\Http\Response |
|
44
|
|
|
*/ |
|
45
|
1 |
|
public function show(string $receiverId) |
|
46
|
|
|
{ |
|
47
|
1 |
|
return $this->receiver->get(Config::get('signere.id'), $receiverId) |
|
48
|
1 |
|
->getBody() |
|
49
|
1 |
|
->getContents(); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Create a receiver. |
|
54
|
|
|
* |
|
55
|
|
|
* @param Request $request |
|
56
|
|
|
* @return \Illuminate\Http\Response |
|
57
|
|
|
*/ |
|
58
|
1 |
|
public function store(Request $request) |
|
59
|
|
|
{ |
|
60
|
1 |
|
$this->validate($request, [ |
|
61
|
1 |
|
'first_name' => 'required|string', |
|
62
|
|
|
'last_name' => 'required|string', |
|
63
|
|
|
'email' => 'sometimes|email|nullable', |
|
64
|
|
|
'company_name' => 'sometimes|string|nullable', |
|
65
|
|
|
'org_no' => 'sometimes|string|nullable', |
|
66
|
|
|
'phone_number' => 'sometimes|string|nullable', |
|
67
|
|
|
]); |
|
68
|
|
|
|
|
69
|
|
|
// this is used to only set the keys which have been sent in |
|
70
|
|
|
$useKeys = [ |
|
71
|
1 |
|
'first_name' => 'FirstName', |
|
72
|
|
|
'last_name' => 'LastName', |
|
73
|
|
|
'email' => 'Email', |
|
74
|
|
|
'company_name' => 'CompanyName', |
|
75
|
|
|
'org_no' => 'OrgNo', |
|
76
|
|
|
'phone_number' => 'Mobile', |
|
77
|
|
|
]; |
|
78
|
|
|
|
|
79
|
|
|
// check which keys are available in the request |
|
80
|
1 |
|
$available = array_intersect(array_keys($useKeys), array_keys($request->all())); |
|
81
|
|
|
|
|
82
|
1 |
|
$body = []; |
|
83
|
|
|
|
|
84
|
|
|
// set the body up |
|
85
|
1 |
|
foreach ($available as $use) { |
|
86
|
1 |
|
$body[$useKeys[$use]] = $request->$use; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
1 |
|
return $this->receiver->create($body) |
|
90
|
1 |
|
->getBody() |
|
91
|
1 |
|
->getContents(); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Deletes one or many receivers. |
|
96
|
|
|
* |
|
97
|
|
|
* @param Request $request |
|
98
|
|
|
* @return \Illuminate\Http\Response |
|
99
|
|
|
*/ |
|
100
|
1 |
|
public function destroy(Request $request) |
|
101
|
|
|
{ |
|
102
|
1 |
|
$this->validate($request, ['receiver_id' => 'sometimes|nullable|string']); |
|
103
|
|
|
|
|
104
|
|
|
// if a specific receiver is to be deleted |
|
105
|
1 |
|
if ($request->has('receiver_id')) { |
|
106
|
1 |
|
return $this->receiver->delete(Config::get('signere.id'), $request->receiver_id) |
|
107
|
1 |
|
->getBody() |
|
108
|
1 |
|
->getContents(); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
// if all receivers are to be deleted |
|
112
|
1 |
|
return $this->receiver->deleteAll(Config::get('signere.id')) |
|
113
|
1 |
|
->getBody() |
|
114
|
1 |
|
->getContents(); |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|