Completed
Push — master ( 5467e4...183ea4 )
by Boy
05:06 queued 01:02
created

RightToBeForgottenController   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 81
Duplicated Lines 18.52 %

Coupling/Cohesion

Components 1
Dependencies 11

Importance

Changes 4
Bugs 0 Features 1
Metric Value
wmc 9
lcom 1
cbo 11
dl 15
loc 81
rs 10
c 4
b 0
f 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
A forgetIdentityAction() 0 23 3
A handleCommand() 15 15 2
B assertMayForget() 0 28 4

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\StepupMiddleware\ManagementBundle\Controller;
20
21
use DateTime;
22
use GuzzleHttp;
23
use Rhumsaa\Uuid\Uuid;
24
use Surfnet\Stepup\Identity\Value\Institution;
25
use Surfnet\Stepup\Identity\Value\NameId;
26
use Surfnet\StepupMiddleware\CommandHandlingBundle\Command\Command;
27
use Surfnet\StepupMiddleware\CommandHandlingBundle\Identity\Command\ForgetIdentityCommand;
28
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
29
use Symfony\Component\HttpFoundation\JsonResponse;
30
use Symfony\Component\HttpFoundation\Request;
31
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
32
use Symfony\Component\HttpKernel\Exception\ConflictHttpException;
33
34
class RightToBeForgottenController extends Controller
35
{
36
    public function forgetIdentityAction(Request $request)
37
    {
38
        $this->denyAccessUnlessGranted(['ROLE_MANAGEMENT']);
39
40
        $payload = GuzzleHttp\json_decode($request->getContent(), true);
0 ignored issues
show
Bug introduced by
It seems like $request->getContent() targeting Symfony\Component\HttpFo...n\Request::getContent() can also be of type resource; however, GuzzleHttp\json_decode() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
41
42
        if (!isset($payload['name_id'])) {
43
            throw new BadRequestHttpException('Please specify a NameID in the property "name_id"');
44
        }
45
46
        if (!isset($payload['institution'])) {
47
            throw new BadRequestHttpException('Please specify an institution in the property "institution"');
48
        }
49
50
        $this->assertMayForget(new NameId($payload['name_id']), new Institution($payload['institution']));
51
52
        $command = new ForgetIdentityCommand();
53
        $command->UUID        = (string) Uuid::uuid4();
54
        $command->nameId      = $payload['name_id'];
55
        $command->institution = $payload['institution'];
56
57
        return $this->handleCommand($request, $command);
58
    }
59
60
    /**
61
     * @param Request $request
62
     * @param Command $command
63
     * @return JsonResponse
64
     */
65 View Code Duplication
    private function handleCommand(Request $request, Command $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...
66
    {
67
        /** @var \Surfnet\StepupMiddleware\CommandHandlingBundle\Pipeline\Pipeline $pipeline */
68
        $pipeline = $this->get('pipeline');
69
        $pipeline->process($command);
70
71
        $serverName = $request->server->get('SERVER_NAME') ?: $request->server->get('SERVER_ADDR');
72
        $response   = new JsonResponse([
73
            'status'       => 'OK',
74
            'processed_by' => $serverName,
75
            'applied_at'   => (new DateTime())->format(DateTime::ISO8601)
76
        ]);
77
78
        return $response;
79
    }
80
81
    /**
82
     * @param NameId      $nameId
83
     * @param Institution $institution
84
     * @throws ConflictHttpException
85
     */
86
    private function assertMayForget(NameId $nameId, Institution $institution)
87
    {
88
        $identityService = $this->get('surfnet_stepup_middleware_api.service.identity');
89
        $credentials =
90
            $identityService->findRegistrationAuthorityCredentialsByNameIdAndInstitution($nameId, $institution);
91
92
        if ($credentials === null) {
93
            return;
94
        }
95
96
        if ($credentials->isSraa()) {
97
            throw new ConflictHttpException(
98
                'Identity is currently configured to act as an SRAA. ' .
99
                'Remove its NameID from the configuration and try again.'
100
            );
101
        }
102
103
        if ($credentials->isRaa()) {
104
            $role = 'RAA';
105
        } else {
106
            $role = 'RA';
107
        }
108
109
        throw new ConflictHttpException(sprintf(
110
            'Identity is currently accredited as an %s. Retract the accreditation and try again.',
111
            $role
112
        ));
113
    }
114
}
115