Test Setup Failed
Push — master ( d34886...775927 )
by guillaume
01:14 queued 10s
created

checkIfUserAlreadyIn()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 5
c 1
b 0
f 0
nc 2
nop 3
dl 0
loc 8
rs 10
1
<?php
2
3
4
namespace App\Src\UseCases\Domain;
5
6
7
use App\Src\UseCases\Domain\Ports\UserRepository;
8
use App\Src\UseCases\Infra\Gateway\FileStorage;
9
use Illuminate\Support\Facades\Validator;
10
11
class PrepareInvitationUsersInOrganization
12
{
13
    private $userRepository;
14
15
    public function __construct(UserRepository $userRepository)
16
    {
17
        $this->userRepository = $userRepository;
18
    }
19
20
    public function prepare(string $organizationId, array $users, string $filePathUsers = null)
21
    {
22
        $usersLoop = $usersToProcess = [];
23
        $usersLoop = $this->getUsersToProcess($users, $usersLoop, $filePathUsers);
24
        $errors = $imported = 0;
25
26
        foreach($usersLoop as $userToInvite){
27
            $validator = $this->validateUserData($userToInvite);
28
            if($validator->fails()){
29
                $userToInvite['error'] = 'email.error.syntax';
30
                $usersToProcess['users'][] = $userToInvite;
31
                $errors++;
32
                continue;
33
            }
34
35
            list($userToInvite, $errors) = $this->checkIfUserAlreadyIn($organizationId, $userToInvite, $errors);
36
37
            $usersToProcess['users'][] = $userToInvite;
38
            if(!isset($userToInvite["error"])){
39
                $imported++;
40
            }
41
        }
42
        $usersToProcess['total'] = count($usersToProcess['users']);
43
        $usersToProcess['imported'] = $imported;
44
        $usersToProcess['error'] = $errors;
45
        return $usersToProcess;
46
    }
47
48
    private function valueUnique(array $users, array $usersLoop): array
49
    {
50
        foreach ($users as $key => $user) {
51
            $usersLoop[trim($user)] = ['email' => trim($user)];
52
        }
53
        return $usersLoop;
54
    }
55
56
    private function getUsersToProcess(array $users, array $usersLoop, string $filePathUsers = null): array
57
    {
58
        if (!empty($users) && $filePathUsers === null) {
59
            return $this->valueUnique($users, $usersLoop);
60
        }
61
        return app(FileStorage::class)->content($filePathUsers);
0 ignored issues
show
Bug introduced by
It seems like $filePathUsers can also be of type null; however, parameter $path of App\Src\UseCases\Infra\G...\FileStorage::content() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

61
        return app(FileStorage::class)->content(/** @scrutinizer ignore-type */ $filePathUsers);
Loading history...
62
    }
63
64
    private function validateUserData($userToInvite): \Illuminate\Contracts\Validation\Validator
65
    {
66
        $rules = [
67
            'email' => 'email|required|min:2|max:255',
68
            'firstname' => 'string|max:100',
69
            'lastname' => 'string|max:100',
70
        ];
71
        return Validator::make($userToInvite, $rules);
72
    }
73
74
    private function checkIfUserAlreadyIn(string $organizationId, $userToInvite, int $errors): array
75
    {
76
        $user = $this->userRepository->getByEmail($userToInvite['email']);
77
        if (isset($user) && $user->organizationId() === $organizationId) {
78
            $userToInvite["error"] = 'already_in';
79
            $errors++;
80
        }
81
        return [$userToInvite, $errors];
82
    }
83
}
84