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