EditUser   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 29
dl 0
loc 61
rs 10
c 1
b 0
f 0
wmc 12

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A edit() 0 10 2
A addErrorsUniquenessEmail() 0 10 4
A handlePicture() 0 5 4
A validateData() 0 15 1
1
<?php
2
3
4
namespace App\Src\UseCases\Domain\Users;
5
6
7
use App\Exceptions\Domain\UserNotFound;
8
use App\Src\UseCases\Domain\Ports\UserRepository;
9
use Illuminate\Support\Facades\Validator;
10
11
class EditUser
12
{
13
    private $userRepository;
14
15
    public function __construct(UserRepository $userRepository)
16
    {
17
        $this->userRepository = $userRepository;
18
    }
19
20
    public function edit(string $userId, string $email, string $firstname, string $lastname, array $picture)
21
    {
22
        $user = $this->userRepository->getById($userId);
23
        if(!isset($user)){
24
            throw new UserNotFound();
25
        }
26
        $this->validateData($userId, $firstname, $lastname, $email, $picture);
27
28
        list($pathPicture, $ext) = $this->handlePicture($picture);
29
        $user->update($email, $firstname, $lastname, $pathPicture, $ext);
30
    }
31
32
    private function validateData(string $userId, string $firstname, string $lastname, string $email, array $picture): array
33
    {
34
        $rules = [
35
            'firstname' => 'string|required|min:2|max:255',
36
            'lastname' => 'string|required|min:2|max:255',
37
            'email' => 'string|required|min:2|max:255|email',
38
            'mine_type' => 'nullable|in:image/jpeg,image/png,image/jpg'
39
        ];
40
        $data = array_merge(['firstname' => $firstname, 'lastname' => $lastname, 'email' => $email], $picture);
41
        $validator = Validator::make($data, $rules);
42
43
        $this->addErrorsUniquenessEmail($userId, $email, $validator);
44
45
        $validator->validate();
46
        return $data;
47
    }
48
49
    private function addErrorsUniquenessEmail(string $userId, string $email, \Illuminate\Contracts\Validation\Validator $validator): void
50
    {
51
        $errors = [];
52
        $user = $this->userRepository->getByEmail($email);
53
        if (isset($user) && $user->id() !== $userId) {
54
            $errors['email'] = __('validation.unique', ['attribute' => 'email']);
55
        }
56
        $validator->after(function () use ($validator, $errors) {
57
            foreach ($errors as $field => $error) {
58
                $validator->errors()->add($field, $error);
59
            }
60
        });
61
    }
62
63
    /**
64
     * @param array $picture
65
     * @return array
66
     */
67
    private function handlePicture(array $picture): array
68
    {
69
        $pathPicture = isset($picture['path_picture']) ? $picture['path_picture'] : '';
70
        $ext = isset($picture['original_name']) && strpos($picture['original_name'], '.') ? explode('.', $picture['original_name'])[1] : 'jpg';
71
        return [$pathPicture, $ext];
72
    }
73
}
74