Completed
Push — master ( 78f4d4...47ceaa )
by Michiel
02:25 queued 51s
created

ProfileService   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 5
dl 0
loc 57
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A get() 0 16 2
A assertIsValid() 0 10 3
1
<?php
2
3
/**
4
 * Copyright 2019 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 Surfnet\StepupMiddlewareClientBundle\Identity\Service;
20
21
use Surfnet\StepupMiddlewareClient\Identity\Dto\ProfileSearchQuery;
22
use Surfnet\StepupMiddlewareClient\Identity\Service\ProfileService as LibraryProfileService;
23
use Surfnet\StepupMiddlewareClientBundle\Exception\InvalidResponseException;
24
use Surfnet\StepupMiddlewareClientBundle\Identity\Dto\Profile;
25
use Symfony\Component\Validator\Validator\ValidatorInterface;
26
27
class ProfileService
28
{
29
    /**
30
     * @var LibraryProfileService
31
     */
32
    private $service;
33
34
    /**
35
     * @var ValidatorInterface
36
     */
37
    private $validator;
38
39
    /**
40
     * @param LibraryProfileService $service
41
     * @param ValidatorInterface $validator
42
     */
43
    public function __construct(LibraryProfileService $service, ValidatorInterface $validator)
44
    {
45
        $this->service = $service;
46
        $this->validator = $validator;
47
    }
48
    /**
49
     * @param string $identityId
50
     * @return null|Profile
51
     */
52
    public function get($identityId)
53
    {
54
        $query = new ProfileSearchQuery($identityId, $identityId);
55
        $data = $this->service->get($query);
56
57
        if ($data === null) {
58
            return null;
59
        }
60
61
        $profile = Profile::fromData($data);
62
63
        $message = sprintf("Profile '%s' retrieved from the Middleware is invalid", $identityId);
64
        $this->assertIsValid($profile, $message);
65
66
        return $profile;
67
    }
68
69
    /**
70
     * @param object      $value
71
     * @param null|string $message
72
     */
73
    private function assertIsValid($value, $message = null)
74
    {
75
        $violations = $this->validator->validate($value);
76
77
        $message = $message ?: 'Invalid Response Received';
78
79
        if (count($violations) > 0) {
80
            throw InvalidResponseException::withViolations($message, $violations);
81
        }
82
    }
83
}
84