Completed
Pull Request — develop (#237)
by Michiel
06:36 queued 03:56
created

WhitelistController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
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 Rhumsaa\Uuid\Uuid;
23
use Surfnet\Stepup\Helper\JsonHelper;
24
use Surfnet\StepupMiddleware\ApiBundle\Identity\Service\WhitelistService;
25
use Surfnet\StepupMiddleware\CommandHandlingBundle\Command\Command;
26
use Surfnet\StepupMiddleware\CommandHandlingBundle\Exception\ForbiddenException;
27
use Surfnet\StepupMiddleware\CommandHandlingBundle\Identity\Command\AddToWhitelistCommand;
28
use Surfnet\StepupMiddleware\CommandHandlingBundle\Identity\Command\RemoveFromWhitelistCommand;
29
use Surfnet\StepupMiddleware\CommandHandlingBundle\Identity\Command\ReplaceWhitelistCommand;
30
use Surfnet\StepupMiddleware\CommandHandlingBundle\Pipeline\TransactionAwarePipeline;
31
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
32
use Symfony\Component\HttpFoundation\JsonResponse;
33
use Symfony\Component\HttpFoundation\Request;
34
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
35
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
36
37
class WhitelistController extends Controller
38
{
39
    /**
40
     * @return TransactionAwarePipeline
41
     */
42
    private $pipeline;
43
44
    /**
45
     * @var WhitelistService
46
     */
47
    private $whitelistService;
48
49
    public function __construct(TransactionAwarePipeline $pipeline, WhitelistService $whitelistService)
50
    {
51
        $this->pipeline = $pipeline;
52
        $this->whitelistService = $whitelistService;
53
    }
54
55 View Code Duplication
    public function replaceWhitelistAction(Request $request)
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...
56
    {
57
        $this->denyAccessUnlessGranted(['ROLE_MANAGEMENT']);
58
59
        $command               = new ReplaceWhitelistCommand();
60
        $command->UUID         = (string) Uuid::uuid4();
61
        $command->institutions = $this->getInstitutionsFromBody($request);
62
63
        return $this->handleCommand($request, $command);
64
    }
65
66 View Code Duplication
    public function addToWhitelistAction(Request $request)
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...
67
    {
68
        $this->denyAccessUnlessGranted(['ROLE_MANAGEMENT']);
69
70
        $command                        = new AddToWhitelistCommand();
71
        $command->UUID                  = (string) Uuid::uuid4();
72
        $command->institutionsToBeAdded = $this->getInstitutionsFromBody($request);
73
74
        return $this->handleCommand($request, $command);
75
    }
76
77 View Code Duplication
    public function removeFromWhitelistAction(Request $request)
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...
78
    {
79
        $this->denyAccessUnlessGranted(['ROLE_MANAGEMENT']);
80
81
        $command                          = new RemoveFromWhitelistCommand();
82
        $command->UUID                    = (string) Uuid::uuid4();
83
        $command->institutionsToBeRemoved = $this->getInstitutionsFromBody($request);
84
85
        return $this->handleCommand($request, $command);
86
    }
87
88
    public function showWhitelistAction()
89
    {
90
        $entries = $this->whitelistService->getAllEntries();
91
92
        return new JsonResponse(['institutions' => $entries->getValues()]);
93
    }
94
95
    /**
96
     * @param Request $request
97
     * @param Command $command
98
     * @return JsonResponse
99
     */
100
    private function handleCommand(Request $request, Command $command)
101
    {
102
        try {
103
            $this->pipeline->process($command);
104
        } catch (ForbiddenException $e) {
105
            throw new AccessDeniedHttpException(
106
                sprintf('Processing of command "%s" is forbidden for this client', $command),
107
                $e
108
            );
109
        }
110
111
        $serverName = $request->server->get('SERVER_NAME') ?: $request->server->get('SERVER_ADDR');
112
        $response   = new JsonResponse([
113
            'status'       => 'OK',
114
            'processed_by' => $serverName,
115
            'applied_at'   => (new DateTime())->format(DateTime::ISO8601),
116
        ]);
117
118
        return $response;
119
    }
120
121
    /**
122
     * @param Request $request
123
     * @return array
124
     */
125
    private function getInstitutionsFromBody(Request $request)
126
    {
127
        $decoded = JsonHelper::decode($request->getContent());
128
129
        if (!isset($decoded['institutions']) || !is_array($decoded['institutions'])) {
130
            throw new BadRequestHttpException(
131
                'Request must contain json object with property "institutions" containing an array of institutions'
132
            );
133
        }
134
135
        return $decoded['institutions'];
136
    }
137
}
138