Completed
Pull Request — develop (#38)
by A.
02:23
created

UserService::getUser()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 15
rs 9.4286
cc 2
eloc 8
nc 2
nop 0
1
<?php
2
3
/**
4
 * Copyright 2015 SURFnet B.V.
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 *     http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
namespace OpenConext\ProfileBundle\Service;
20
21
use OpenConext\Profile\Entity\User;
22
use OpenConext\Profile\Api\AuthenticatedUserProvider as AuthenticatedUserProviderInterface;
23
use OpenConext\Profile\Repository\UserRepository;
24
use OpenConext\Profile\Value\EntityId;
25
use OpenConext\Profile\Value\Locale;
26
use OpenConext\ProfileBundle\Profile\Command\ChangeLocaleCommand;
27
28
final class UserService
29
{
30
    /**
31
     * @var SupportContactEmailService
32
     */
33
    private $supportContactEmailService;
34
35
    /**
36
     * @var UserRepository
37
     */
38
    private $userRepository;
39
40
    /**
41
     * @var AuthenticatedUserProviderInterface
42
     */
43
    private $authenticatedUserProvider;
44
45
    /**
46
     * @var LocaleService
47
     */
48
    private $localeService;
49
    /**
50
     * @var EntityId
51
     */
52
    private $engineBlockEntityId;
53
54
    public function __construct(
55
        SupportContactEmailService $supportContactEmailService,
56
        UserRepository $userRepository,
57
        AuthenticatedUserProviderInterface $authenticatedUserProvider,
58
        LocaleService $localeService,
59
        EntityId $engineBlockEntityId
60
    ) {
61
        $this->supportContactEmailService = $supportContactEmailService;
62
        $this->userRepository             = $userRepository;
63
        $this->authenticatedUserProvider  = $authenticatedUserProvider;
64
        $this->localeService              = $localeService;
65
        $this->engineBlockEntityId        = $engineBlockEntityId;
66
    }
67
68
    /**
69
     * @return User
70
     */
71
    public function getUser()
72
    {
73
        $user = $this->userRepository->findUser();
74
75
        if ($user) {
76
            return $user;
77
        }
78
79
        $user = new User($this->authenticatedUserProvider->getCurrentUser(), $this->localeService->getLocale());
80
        $user = $this->enrichUserWithSupportContactEmail($user);
81
82
        $this->userRepository->save($user);
83
84
        return $user;
85
    }
86
87
    /**
88
     * @param ChangeLocaleCommand $changeLocaleCommand
89
     */
90
    public function changeLocale(ChangeLocaleCommand $changeLocaleCommand)
91
    {
92
        $user = $this->getUser();
93
        $user->switchLocaleTo(new Locale($changeLocaleCommand->newLocale));
94
95
        $this->localeService->saveLocaleOf($user);
96
    }
97
98
    /**
99
     * @param User $user
100
     * @return User
101
     */
102
    private function enrichUserWithSupportContactEmail(User $user)
103
    {
104
        $entityIds = $this->authenticatedUserProvider->getCurrentUser()->getAuthenticatingAuthorities();
105
106
        if (!empty($entityIds)) {
107
            $authenticatingIdpEntityId = $this->getNearestAuthenticatingAuthorityEntityId($entityIds);
108
            $supportContactEmail       = $this->supportContactEmailService->findSupportContactEmail(
109
                $authenticatingIdpEntityId
110
            );
111
112
            if ($supportContactEmail !== null) {
113
                $user = $user->withSupportContactEmail($supportContactEmail);
114
            }
115
        }
116
117
        return $user;
118
    }
119
120
    /**
121
     * @param EntityId[] $entityIds
122
     * @return EntityId
123
     */
124
    private function getNearestAuthenticatingAuthorityEntityId(array $entityIds)
125
    {
126
        $lastEntityId = array_pop($entityIds);
127
128
        if (!$this->engineBlockEntityId->equals($lastEntityId)) {
0 ignored issues
show
Bug introduced by
It seems like $lastEntityId defined by array_pop($entityIds) on line 126 can be null; however, OpenConext\Profile\Value\EntityId::equals() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
129
            return $lastEntityId;
130
        }
131
132
        return array_pop($entityIds);
133
    }
134
}
135