ConfigurationController::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 0
nc 1
nop 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
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
 */
0 ignored issues
show
Coding Style introduced by
Missing @link tag in file comment
Loading history...
18
19
namespace Surfnet\StepupMiddleware\ManagementBundle\Controller;
20
21
use DateTime;
22
use Ramsey\Uuid\Uuid;
23
use Surfnet\Stepup\Helper\JsonHelper;
24
use Surfnet\StepupMiddleware\ApiBundle\Exception\BadCommandRequestException;
25
use Surfnet\StepupMiddleware\CommandHandlingBundle\Command\AbstractCommand;
26
use Surfnet\StepupMiddleware\CommandHandlingBundle\Configuration\Command\UpdateConfigurationCommand;
27
use Surfnet\StepupMiddleware\CommandHandlingBundle\Pipeline\TransactionAwarePipeline;
28
use Surfnet\StepupMiddleware\ManagementBundle\Validator\Constraints\HasValidConfigurationStructure;
29
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
30
use Symfony\Component\HttpFoundation\JsonResponse;
31
use Symfony\Component\HttpFoundation\Request;
32
use Symfony\Component\Validator\Validator\ValidatorInterface;
33
34
class ConfigurationController extends AbstractController
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class ConfigurationController
Loading history...
35
{
36
    public function __construct(
37
        private readonly TransactionAwarePipeline $pipeline,
38
        private readonly ValidatorInterface $validator,
39
    ) {
40
    }
41
42
    public function update(Request $request): JsonResponse
43
    {
44
        $this->denyAccessUnlessGranted('ROLE_MANAGEMENT');
45
46
        $violations = $this->validator->validate($request->getContent(), new HasValidConfigurationStructure());
47
        if ($violations->count() > 0) {
48
            throw BadCommandRequestException::withViolations('Invalid configure institutions request', $violations);
49
        }
50
51
        $command = new UpdateConfigurationCommand();
52
        $command->configuration = $request->getContent();
53
        $command->UUID = (string)Uuid::uuid4();
54
55
        return $this->handleCommand($request, $command);
56
    }
57
58
    private function handleCommand(Request $request, AbstractCommand $command): JsonResponse
0 ignored issues
show
Coding Style introduced by
Private method name "ConfigurationController::handleCommand" must be prefixed with an underscore
Loading history...
59
    {
60
        $this->pipeline->process($command);
61
62
        $serverName = $request->server->get('SERVER_NAME') ?: $request->server->get('SERVER_ADDR');
63
64
        return new JsonResponse([
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
65
            'status' => 'OK',
66
            'processed_by' => $serverName,
67
            'applied_at' => (new DateTime())->format(DateTime::ISO8601),
68
        ]);
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
69
    }
70
}
71