UserCreator   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 19
c 1
b 0
f 0
dl 0
loc 39
ccs 0
cts 25
cp 0
rs 10
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A updateOrCreate() 0 27 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace DMK\MKSamlAuth\Service;
6
7
use DMK\MKSamlAuth\EnricherRegistry;
8
use DMK\MKSamlAuth\Manager\FrontendUserManager;
9
use DMK\MKSamlAuth\Model\FrontendUser;
10
use TYPO3\CMS\Core\SingletonInterface;
11
12
final class UserCreator implements SingletonInterface
13
{
14
    private $enrichers;
15
16
    private $manager;
17
18
    public function __construct(EnricherRegistry $registry, FrontendUserManager $manager)
19
    {
20
        $this->enrichers = $registry;
21
        $this->manager = $manager;
22
    }
23
24
    public function updateOrCreate(array $attributes, array $record)
25
    {
26
        $userFolder = $record['user_folder'];
27
28
        if (!($attributes['uid'] ?? false)) {
29
            throw new \LogicException('The idp does not return any "uid". Please check the configuration in the idp settings.');
30
        }
31
32
        $user = $this->manager->getRepository()->findByUsername($attributes['uid'], $record['user_folder']);
33
34
        if (null === $user) {
35
            $user = new FrontendUser([
36
                'username' => $attributes['uid'],
37
                'pid' => $userFolder,
38
            ]);
39
        }
40
41
        unset($attributes['uid']);
42
43
        $this->enrichers->process($user, [
44
            'idp' => $record,
45
            'attributes' => $attributes,
46
        ]);
47
48
        $this->manager->save($user);
49
50
        return $user;
51
    }
52
}
53