1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Src\UseCases\Domain\Organizations; |
4
|
|
|
|
5
|
|
|
use App\Src\UseCases\Domain\Organizations\Model\Address; |
6
|
|
|
use App\Src\UseCases\Domain\Organizations\Model\Organization; |
7
|
|
|
use Illuminate\Support\Facades\Validator; |
8
|
|
|
use Ramsey\Uuid\Uuid; |
9
|
|
|
|
10
|
|
|
class CreateOrganization |
11
|
|
|
{ |
12
|
|
|
public function create(string $name, array $picture, array $address) |
13
|
|
|
{ |
14
|
|
|
$data = $this->validateData($name, $address, $picture); |
15
|
|
|
|
16
|
|
|
list($pathPicture, $ext) = $this->handlePicture($picture); |
17
|
|
|
|
18
|
|
|
$id = Uuid::uuid4()->toString(); |
19
|
|
|
$address = new Address($data['city'], $data['address1'], $data['address2'], $data['pc']); |
20
|
|
|
$organization = new Organization($id, $name, $pathPicture, $address); |
21
|
|
|
$organization->create($ext); |
22
|
|
|
|
23
|
|
|
return $id; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
private function validateData(string $name, array $address, array $picture): array |
27
|
|
|
{ |
28
|
|
|
$rules = [ |
29
|
|
|
'name' => 'string|required|min:2|max:255', |
30
|
|
|
'city' => 'string|required|min:2|max:100', |
31
|
|
|
'pc' => 'string|required|min:2|max:6', |
32
|
|
|
'address1' => 'string|required|min:2|max:255', |
33
|
|
|
'address2' => 'min:2|max:255|nullable', |
34
|
|
|
'mine_type' => 'nullable|in:image/jpeg,image/png,image/jpg' |
35
|
|
|
]; |
36
|
|
|
$data = array_merge(['name' => $name], $address, $picture); |
37
|
|
|
$validator = Validator::make($data, $rules, [], ['name' => 'Nom']); |
38
|
|
|
$validator->validate(); |
39
|
|
|
return $data; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param array $picture |
44
|
|
|
* @return array |
45
|
|
|
*/ |
46
|
|
|
private function handlePicture(array $picture): array |
47
|
|
|
{ |
48
|
|
|
$pathPicture = isset($picture['path_picture']) ? $picture['path_picture'] : ''; |
49
|
|
|
$ext = isset($picture['original_name']) && strpos($picture['original_name'], '.') ? explode('.', $picture['original_name'])[1] : 'jpg'; |
50
|
|
|
return [$pathPicture, $ext]; |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|