1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace App\Src\UseCases\Domain\Users\Profile; |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
use App\Src\UseCases\Domain\Agricultural\Model\Context; |
8
|
|
|
use App\Src\UseCases\Domain\Ports\IdentityProvider; |
9
|
|
|
use App\Src\UseCases\Domain\Ports\UserRepository; |
10
|
|
|
use Illuminate\Support\Facades\Validator; |
11
|
|
|
|
12
|
|
|
class FillWikiUserProfile |
13
|
|
|
{ |
14
|
|
|
private $userRepository; |
15
|
|
|
private $identityProvider; |
16
|
|
|
|
17
|
|
|
public function __construct( |
18
|
|
|
UserRepository $userRepository, |
19
|
|
|
IdentityProvider $identityProvider |
20
|
|
|
) |
21
|
|
|
{ |
22
|
|
|
$this->userRepository = $userRepository; |
23
|
|
|
$this->identityProvider = $identityProvider; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public function fill(string $userId, string $role, string $firstname, string $lastname, string $email, string $postcode, array $farmingType = []) |
27
|
|
|
{ |
28
|
|
|
$errors = []; |
29
|
|
|
$user = $this->userRepository->getByEmail($email); |
30
|
|
|
if(isset($user) && $user->id() !== $userId){ |
31
|
|
|
$errors[] = ['validation.unique']; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
$this->validate($firstname, $lastname, $role, $email, $postcode, $errors); |
35
|
|
|
|
36
|
|
|
$user = $this->userRepository->getById($userId); |
37
|
|
|
$user->update($email, $firstname, $lastname, ""); |
38
|
|
|
$user->addRole($role); |
39
|
|
|
|
40
|
|
|
$exploitationId = $this->identityProvider->id(); |
41
|
|
|
$context = new Context($exploitationId, $postcode, $farmingType); |
42
|
|
|
$context->create($userId); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
private function validate(string $firstname, string $lastname, string $role, string $email, string $postcode, array $errors = []): void |
46
|
|
|
{ |
47
|
|
|
$rules = [ |
48
|
|
|
'firstname' => 'required', |
49
|
|
|
'lastname' => 'required', |
50
|
|
|
'role' => 'required', |
51
|
|
|
'email' => 'required|email', |
52
|
|
|
'postal_code' => ['required', 'regex:/^((0[1-9])|([1-8][0-9])|(9[0-8])|(2A)|(2B))[0-9]{3}$/'], |
53
|
|
|
]; |
54
|
|
|
|
55
|
|
|
$validator = Validator::make([ |
56
|
|
|
'firstname' => $firstname, |
57
|
|
|
'lastname' => $lastname, |
58
|
|
|
'role' => $role, |
59
|
|
|
'postal_code' => $postcode, |
60
|
|
|
'email' => $email |
61
|
|
|
], $rules); |
62
|
|
|
|
63
|
|
|
$validator->after(function () use ($validator, $errors) { |
64
|
|
|
foreach ($errors as $field => $error) { |
65
|
|
|
$validator->errors()->add($field, $error); |
66
|
|
|
} |
67
|
|
|
}); |
68
|
|
|
|
69
|
|
|
$validator->validate(); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|