Completed
Push — develop ( fac452...d8e37f )
by A.
06:03 queued 02:16
created

getNearestAuthenticatingAuthorityEntityId()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 14
rs 9.4286
cc 3
eloc 7
nc 3
nop 1
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
        $authenticatingIdpEntityId = $this->getNearestAuthenticatingAuthorityEntityId($entityIds);
107
108
        if ($authenticatingIdpEntityId === null) {
109
            return $user;
110
        }
111
112
        $supportContactEmail = $this->supportContactEmailService->findSupportContactEmailForIdp(
113
            new EntityId($authenticatingIdpEntityId)
114
        );
115
116
        if ($supportContactEmail === null) {
117
            return $user;
118
        }
119
120
        return $user->withSupportContactEmail($supportContactEmail);
121
    }
122
123
    /**
124
     * @param EntityId[] $entityIds
125
     * @return null|EntityId
126
     */
127
    private function getNearestAuthenticatingAuthorityEntityId(array $entityIds)
128
    {
129
        $lastEntityId = array_pop($entityIds);
130
131
        if ($lastEntityId === null) {
132
            return null;
133
        }
134
135
        if (!$this->engineBlockEntityId->equals($lastEntityId)) {
136
            return $lastEntityId;
137
        }
138
139
        return array_pop($entityIds);
140
    }
141
}
142