UserCsvImportHelper::getUsersFromCsv()   B
last analyzed

Complexity

Conditions 10
Paths 98

Size

Total Lines 58
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 110

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 10
eloc 34
c 2
b 0
f 0
nc 98
nop 3
dl 0
loc 58
ccs 0
cts 33
cp 0
crap 110
rs 7.6666

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace App\Import;
4
5
use League\Csv\Exception;
6
use App\Entity\User;
7
use App\Entity\UserType;
8
use App\Repository\UserRepositoryInterface;
9
use League\Csv\Reader;
10
11
class UserCsvImportHelper {
12
13
    private const FirstnameHeader = 'Vorname';
14
    private const LastnameHeader = 'Nachname';
15
    private const IdHeader = 'ID';
16
    private const EmailAddressHeader = 'E-Mail';
17
    private const PasswordHeader = 'Passwort';
18
    private const GradeHeader = 'Klasse';
19
20
    public function __construct(private UserRepositoryInterface $userRepository)
21
    {
22
    }
23
24
    public function getHeaders(): array {
25
        return [
26
            self::EmailAddressHeader,
27
            self::PasswordHeader,
28
            self::FirstnameHeader,
29
            self::LastnameHeader,
30
            self::GradeHeader,
31
            self::IdHeader
32
        ];
33
    }
34
35
    public function getRequiredHeaders(): array {
36
        return [
37
            self::EmailAddressHeader
38
        ];
39
    }
40
41
    /**
42
     * Loads users from a given CSV file into a list of user objects
43
     *
44
     * @return User[]
45
     * @throws RecordInvalidException
46
     * @throws Exception
47
     */
48
    public function getUsersFromCsv(string $csv, string $delimiter, UserType $userType): array {
49
        $reader = Reader::createFromString($csv);
50
        $reader->setDelimiter($delimiter);
51
        $reader->setHeaderOffset(0);
52
53
        $users = [ ];
54
55
        foreach ($reader->getRecords() as $offset => $record) {
56
            $firstname = $record[self::FirstnameHeader] ?? null;
57
            $lastname = $record[self::LastnameHeader] ?? null;
58
            $id = $record[self::IdHeader] ?? null;
59
            $email = $record[self::EmailAddressHeader] ?? null;
60
            $password = $record[self::PasswordHeader] ?? null;
61
            $grade = $record[self::GradeHeader] ?? null;
62
63
            if(empty($email)) {
64
                throw new RecordInvalidException($offset, self::EmailAddressHeader);
65
            }
66
67
            $user = null;
68
69
            if($user === null) {
70
                $user = $this->userRepository->findOneByUsername($email);
71
            }
72
73
            if($user === null) {
74
                $user = new User();
75
                $user->setUsername($email);
76
                $user->setEmail($email);
77
78
                if(!empty($password)) {
79
                    $user->setPassword($password);
80
                    $user->setIsProvisioned(false);
81
                }
82
            }
83
84
            if(!empty($id)) {
85
                $user->setExternalId($id);
86
            }
87
88
            if(!empty($firstname)) {
89
                $user->setFirstname($firstname);
90
            }
91
92
            if(!empty($lastname)) {
93
                $user->setLastname($lastname);
94
            }
95
96
            if(!empty($grade)) {
97
                $user->setGrade($grade);
98
            }
99
100
            $user->setType($userType);
101
102
            $users[] = $user;
103
        }
104
105
        return $users;
106
    }
107
}