UserService::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.8333
c 0
b 0
f 0
cc 1
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\Api\ApiUserInterface;
22
use OpenConext\Profile\Entity\User;
23
use OpenConext\Profile\Api\AuthenticatedUserProviderInterface;
24
use OpenConext\Profile\Repository\UserRepositoryInterface;
25
use OpenConext\Profile\Value\EntityId;
26
use OpenConext\Profile\Value\Locale;
27
use OpenConext\ProfileBundle\Profile\Command\ChangeLocaleCommand;
28
use OpenConext\UserLifecycleApiClientBundle\Http\JsonApiClient as UserLifecycleApiClient;
29
30
final class UserService
31
{
32
    /**
33
     * @var SupportContactEmailService
34
     */
35
    private $supportContactEmailService;
36
37
    /**
38
     * @var UserRepositoryInterface
39
     */
40
    private $userRepository;
41
42
    /**
43
     * @var AuthenticatedUserProviderInterface
44
     */
45
    private $authenticatedUserProvider;
46
47
    /**
48
     * @var LocaleService
49
     */
50
    private $localeService;
51
52
    /**
53
     * @var EntityId
54
     */
55
    private $engineBlockEntityId;
56
57
    /**
58
     * @var UserLifecycleApiClient
59
     */
60
    private $userLifecycleApiClient;
61
62
    public function __construct(
63
        SupportContactEmailService $supportContactEmailService,
64
        UserRepositoryInterface $userRepository,
65
        AuthenticatedUserProviderInterface $authenticatedUserProvider,
66
        LocaleService $localeService,
67
        EntityId $engineBlockEntityId
68
    ) {
69
        $this->supportContactEmailService = $supportContactEmailService;
70
        $this->userRepository             = $userRepository;
71
        $this->authenticatedUserProvider  = $authenticatedUserProvider;
72
        $this->localeService              = $localeService;
73
        $this->engineBlockEntityId        = $engineBlockEntityId;
74
    }
75
76
    /**
77
     * The user lifecycle client is optional.
78
     *
79
     * @param UserLifecycleApiClient $client
0 ignored issues
show
Documentation introduced by
Should the type for parameter $client not be null|UserLifecycleApiClient?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
80
     */
81
    public function setUserLifecycleApiClient(UserLifecycleApiClient $client = null)
82
    {
83
        $this->userLifecycleApiClient = $client;
84
    }
85
86
    /**
87
     * @return ApiUserInterface
88
     */
89
    public function getUser()
90
    {
91
        $user = $this->userRepository->findUser();
92
93
        if ($user) {
94
            return $user;
95
        }
96
97
        $user = new User($this->authenticatedUserProvider->getCurrentUser(), $this->localeService->getLocale());
98
        $user = $this->enrichUserWithSupportContactEmail($user);
99
100
        $this->userRepository->save($user);
101
102
        return $user;
103
    }
104
105
    /**
106
     * @param ChangeLocaleCommand $changeLocaleCommand
107
     */
108
    public function changeLocale(ChangeLocaleCommand $changeLocaleCommand)
109
    {
110
        $user = $this->getUser();
111
        $user->switchLocaleTo(new Locale($changeLocaleCommand->newLocale));
112
113
        $this->localeService->saveLocaleOf($user);
114
    }
115
116
    /**
117
     * @return array
118
     */
119
    public function getUserLifecycleData()
120
    {
121
        if (!$this->userLifecycleApiIsEnabled()) {
122
            return;
123
        }
124
125
        $user = $this->getUser();
126
127
        return $this->userLifecycleApiClient->read(
128
            sprintf('/api/deprovision/%s', $user->getId())
129
        );
130
    }
131
132
    /**
133
     * @return bool
134
     */
135
    public function userLifecycleApiIsEnabled()
136
    {
137
        return $this->userLifecycleApiClient !== null;
138
    }
139
140
    /**
141
     * @param ApiUserInterface $user
142
     * @return ApiUserInterface
143
     */
144
    private function enrichUserWithSupportContactEmail(ApiUserInterface $user)
145
    {
146
        $entityIds                 = $this->authenticatedUserProvider->getCurrentUser()->getAuthenticatingAuthorities();
147
        $authenticatingIdpEntityId = $this->getNearestAuthenticatingAuthorityEntityId($entityIds);
148
149
        if ($authenticatingIdpEntityId === null) {
150
            return $user;
151
        }
152
153
        $supportContactEmail = $this->supportContactEmailService->findSupportContactEmailForIdp(
154
            $authenticatingIdpEntityId
155
        );
156
157
        if ($supportContactEmail === null) {
158
            return $user;
159
        }
160
161
        return $user->withSupportContactEmail($supportContactEmail);
162
    }
163
164
    /**
165
     * @param EntityId[] $entityIds
166
     * @return null|EntityId
167
     */
168
    private function getNearestAuthenticatingAuthorityEntityId(array $entityIds)
169
    {
170
        $lastEntityId = array_pop($entityIds);
171
172
        if ($lastEntityId === null) {
173
            return null;
174
        }
175
176
        if (!$this->engineBlockEntityId->equals($lastEntityId)) {
177
            return $lastEntityId;
178
        }
179
180
        return array_pop($entityIds);
181
    }
182
}
183