Completed
Push — master ( 5b54fc...16c96d )
by Bertrand
14:42
created

EditOrganization::edit()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 8
nc 2
nop 4
dl 0
loc 12
rs 10
c 0
b 0
f 0
1
<?php
2
3
4
namespace App\Src\UseCases\Domain\Organizations;
5
6
use App\Exceptions\Domain\OrganizationNotFound;
7
use App\Src\UseCases\Domain\Organizations\Model\Address;
8
use App\Src\UseCases\Domain\Ports\OrganizationRepository;
9
use Illuminate\Support\Facades\Validator;
10
11
class EditOrganization
12
{
13
    private $organizationRepository;
14
15
    public function __construct(OrganizationRepository $organizationRepository)
16
    {
17
        $this->organizationRepository = $organizationRepository;
18
    }
19
20
    public function edit(string $organizationId, string $name, array $picture, array $address)
21
    {
22
        $organization = $this->organizationRepository->get($organizationId);
23
        if(!isset($organization)){
24
            throw new OrganizationNotFound();
25
        }
26
        $this->validateData($name, $picture, $address);
27
28
        list($pathPicture, $ext) = $this->handlePicture($picture);
29
        $pictureToSave = ['path' => $pathPicture, 'ext' => $ext];
30
        $address = new Address($address['city'], $address['address1'], $address['address2'], $address['pc']);
31
        $organization->update($name, $pictureToSave, $address);
32
    }
33
34
    private function validateData(string $name, array $address, array $picture): array
35
    {
36
        $rules = [
37
            'name' => 'string|required|min:2|max:255',
38
            'city' => 'string|required|min:2|max:100',
39
            'pc' => 'string|required|min:2|max:6',
40
            'address1' => 'string|required|min:2|max:255',
41
            'address2' => 'min:2|max:255|nullable',
42
            'mine_type' => 'nullable|in:image/jpeg,image/png,image/jpg'
43
        ];
44
        $data = array_merge(['name' => $name], $address, $picture);
45
        $validator = Validator::make($data, $rules, [], ['name' => 'Nom']);
46
        $validator->validate();
47
        return $data;
48
    }
49
50
    private function handlePicture(array $picture): array
51
    {
52
        $pathPicture = isset($picture['path_picture']) ? $picture['path_picture'] : '';
53
        $ext = isset($picture['original_name']) && strpos($picture['original_name'], '.') ? explode('.', $picture['original_name'])[1] : 'jpg';
54
        return [$pathPicture, $ext];
55
    }
56
}
57