RaService   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 78
Duplicated Lines 25.64 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 9
dl 20
loc 78
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A amendRegistrationAuthorityInformation() 20 20 2
A changeRegistrationAuthorityRole() 0 19 2
A retractRegistrationAuthority() 0 18 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
/**
4
 * Copyright 2014 SURFnet bv
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\StepupRa\RaBundle\Service;
20
21
use Psr\Log\LoggerInterface;
22
use Surfnet\StepupMiddlewareClientBundle\Identity\Command\AmendRegistrationAuthorityInformationCommand
23
    as AmendRegistrationAuthorityInformationApiCommand;
24
use Surfnet\StepupMiddlewareClientBundle\Identity\Command\AppointRoleCommand;
25
use Surfnet\StepupMiddlewareClientBundle\Identity\Command\RetractRegistrationAuthorityCommand as ApiRetractRegistrationAuthorityCommand;
26
use Surfnet\StepupRa\RaBundle\Command\AmendRegistrationAuthorityInformationCommand;
27
use Surfnet\StepupRa\RaBundle\Command\ChangeRaRoleCommand;
28
use Surfnet\StepupRa\RaBundle\Command\RetractRegistrationAuthorityCommand;
29
30
final class RaService
31
{
32
    /**
33
     * @var CommandService
34
     */
35
    private $commandService;
36
37
    /**
38
     * @var LoggerInterface
39
     */
40
    private $logger;
41
42
    public function __construct(CommandService $commandService, LoggerInterface $logger)
43
    {
44
        $this->commandService = $commandService;
45
        $this->logger = $logger;
46
    }
47
48 View Code Duplication
    public function amendRegistrationAuthorityInformation(AmendRegistrationAuthorityInformationCommand $command)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
49
    {
50
        $apiCommand = new AmendRegistrationAuthorityInformationApiCommand();
51
        $apiCommand->identityId = $command->identityId;
52
        $apiCommand->location = $command->location;
53
        $apiCommand->contactInformation = $command->contactInformation;
54
        $apiCommand->raInstitution = $command->institution;
55
56
        $result = $this->commandService->execute($apiCommand);
57
58
        if (!$result->isSuccessful()) {
59
            $this->logger->error(sprintf(
60
                "Amending of registration authority %s's information failed: '%s'",
61
                $apiCommand->identityId,
62
                implode("', '", $result->getErrors())
63
            ));
64
        }
65
66
        return $result->isSuccessful();
67
    }
68
69
    public function changeRegistrationAuthorityRole(ChangeRaRoleCommand $command)
70
    {
71
        $apiCommand             = new AppointRoleCommand();
72
        $apiCommand->identityId = $command->identityId;
73
        $apiCommand->role       = $command->role;
74
75
        $result = $this->commandService->execute($apiCommand);
76
77
        if (!$result->isSuccessful()) {
78
            $this->logger->error(sprintf(
79
                'Could not change Identity "%s" role to "%s": "%s"',
80
                $apiCommand->identityId,
81
                $apiCommand->role,
82
                implode("', '", $result->getErrors())
83
            ));
84
        }
85
86
        return $result->isSuccessful();
87
    }
88
89
    public function retractRegistrationAuthority(RetractRegistrationAuthorityCommand $command)
90
    {
91
        $apiCommand              = new ApiRetractRegistrationAuthorityCommand();
92
        $apiCommand->identityId  = $command->identityId;
93
        $apiCommand->institution = $command->institution;
94
95
        $result = $this->commandService->execute($apiCommand);
96
97
        if (!$result->isSuccessful()) {
98
            $this->logger->error(sprintf(
99
                'Could not retract registration authority for identity "%s": "%s"',
100
                $apiCommand->identityId,
101
                implode("', '", $result->getErrors())
102
            ));
103
        }
104
105
        return $result->isSuccessful();
106
    }
107
}
108