Completed
Pull Request — develop (#38)
by A.
04:16
created

UserService::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 13
rs 9.4286
cc 1
eloc 11
nc 1
nop 5
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
    /**
51
     * @var EntityId
52
     */
53
    private $engineBlockEntityId;
54
55
    public function __construct(
56
        SupportContactEmailService $supportContactEmailService,
57
        UserRepository $userRepository,
58
        AuthenticatedUserProviderInterface $authenticatedUserProvider,
59
        LocaleService $localeService,
60
        EntityId $engineBlockEntityId
61
    ) {
62
        $this->supportContactEmailService = $supportContactEmailService;
63
        $this->userRepository             = $userRepository;
64
        $this->authenticatedUserProvider  = $authenticatedUserProvider;
65
        $this->localeService              = $localeService;
66
        $this->engineBlockEntityId        = $engineBlockEntityId;
67
    }
68
69
    /**
70
     * @return User
71
     */
72
    public function getUser()
73
    {
74
        $user = $this->userRepository->findUser();
75
76
        if ($user) {
77
            return $user;
78
        }
79
80
        $user = new User($this->authenticatedUserProvider->getCurrentUser(), $this->localeService->getLocale());
81
        $user = $this->enrichUserWithSupportContactEmail($user);
82
83
        $this->userRepository->save($user);
84
85
        return $user;
86
    }
87
88
    /**
89
     * @param ChangeLocaleCommand $changeLocaleCommand
90
     */
91
    public function changeLocale(ChangeLocaleCommand $changeLocaleCommand)
92
    {
93
        $user = $this->getUser();
94
        $user->switchLocaleTo(new Locale($changeLocaleCommand->newLocale));
95
96
        $this->localeService->saveLocaleOf($user);
97
    }
98
99
    /**
100
     * @param User $user
101
     * @return User
102
     */
103
    private function enrichUserWithSupportContactEmail(User $user)
104
    {
105
        $entityIds = $this->authenticatedUserProvider->getCurrentUser()->getAuthenticatingAuthorities();
106
107
        if (!empty($entityIds)) {
108
            $authenticatingIdpEntityId = $this->getNearestAuthenticatingAuthorityEntityId($entityIds);
109
            $supportContactEmail       = $this->supportContactEmailService->findSupportContactEmailForIdp(
110
                new EntityId($authenticatingIdpEntityId)
111
            );
112
113
            if ($supportContactEmail !== null) {
114
                $user = $user->withSupportContactEmail($supportContactEmail);
115
            }
116
        }
117
118
        return $user;
119
    }
120
121
    /**
122
     * @param EntityId[] $entityIds
123
     * @return EntityId
124
     */
125
    private function getNearestAuthenticatingAuthorityEntityId(array $entityIds)
126
    {
127
        $lastEntityId = array_pop($entityIds);
128
129
        if (!$this->engineBlockEntityId->equals($lastEntityId)) {
0 ignored issues
show
Bug introduced by
It seems like $lastEntityId defined by array_pop($entityIds) on line 127 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...
130
            return $lastEntityId;
131
        }
132
133
        return array_pop($entityIds);
134
    }
135
}
136