Passed
Pull Request — master (#7302)
by Angel Fernando Quiroz
17:21 queued 08:19
created

ScimHelper   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Importance

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

6 Methods

Rating   Name   Duplication   Size   Complexity  
A saveExternalId() 0 6 1
A __construct() 0 5 1
A getExtraField() 0 5 1
A createToken() 0 6 2
A findUser() 0 15 3
A getExternalId() 0 9 1
1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
declare(strict_types=1);
6
7
namespace Chamilo\CoreBundle\Helpers;
8
9
use Chamilo\CoreBundle\Entity\ExtraField;
10
use Chamilo\CoreBundle\Entity\User;
11
use Chamilo\CoreBundle\Repository\ExtraFieldRepository;
12
use Chamilo\CoreBundle\Repository\ExtraFieldValuesRepository;
13
use Chamilo\CoreBundle\Repository\Node\UserRepository;
14
use Doctrine\ORM\NonUniqueResultException;
15
use Random\RandomException;
16
17
readonly class ScimHelper
18
{
19
    public const SCIM_FIELD = 'scim_external_id';
20
21
    public function __construct(
22
        private ExtraFieldRepository $extraFieldRepo,
23
        private ExtraFieldValuesRepository $extraFieldValuesRepo,
24
        private UserRepository $userRepo,
25
    ) {}
26
27
    public static function createToken(): string
28
    {
29
        try {
30
            return bin2hex(random_bytes(64));
31
        } catch (RandomException) {
32
            return '';
33
        }
34
    }
35
36
    private function getExtraField(): ExtraField
37
    {
38
        return $this->extraFieldRepo->findByVariable(
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->extraField...TYPE, self::SCIM_FIELD) could return the type null which is incompatible with the type-hinted return Chamilo\CoreBundle\Entity\ExtraField. Consider adding an additional type-check to rule them out.
Loading history...
39
            ExtraField::USER_FIELD_TYPE,
40
            self::SCIM_FIELD
41
        );
42
    }
43
44
    public function findUser(string $externalId): ?User
45
    {
46
        $field = $this->getExtraField();
47
48
        try {
49
            $fieldValue = $this->extraFieldValuesRepo->findByVariableAndValue($field, $externalId);
50
        } catch (NonUniqueResultException) {
51
            return null;
52
        }
53
54
        if (!$fieldValue) {
55
            return null;
56
        }
57
58
        return $this->userRepo->find($fieldValue->getItemId());
59
    }
60
61
    public function getExternalId(User $user): ?string
62
    {
63
        $fieldValue = $this->extraFieldValuesRepo->getValueByVariableAndItem(
64
            self::SCIM_FIELD,
65
            $user->getId(),
66
            ExtraField::USER_FIELD_TYPE
67
        );
68
69
        return $fieldValue?->getFieldValue();
70
    }
71
72
    public function saveExternalId(string $externalId, User $user): void
73
    {
74
        $this->extraFieldValuesRepo->updateItemData(
75
            $this->getExtraField(),
76
            $user,
77
            $externalId
78
        );
79
    }
80
}
81