RaSecondFactorController   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 28
dl 0
loc 60
rs 10
c 0
b 0
f 0
wmc 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A buildRaSecondFactorQuery() 0 25 2
A __construct() 0 4 1
A export() 0 9 1
A collection() 0 11 1
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\ApiBundle\Controller;
20
21
use Surfnet\Stepup\Identity\Value\IdentityId;
22
use Surfnet\Stepup\Identity\Value\RegistrationAuthorityRole;
0 ignored issues
show
Bug introduced by
The type Surfnet\Stepup\Identity\...gistrationAuthorityRole was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
23
use Surfnet\StepupMiddleware\ApiBundle\Authorization\Service\AuthorizationContextService;
24
use Surfnet\StepupMiddleware\ApiBundle\Controller\AbstractController;
25
use Surfnet\StepupMiddleware\ApiBundle\Identity\Query\RaSecondFactorQuery;
26
use Surfnet\StepupMiddleware\ApiBundle\Identity\Service\RaSecondFactorService;
27
use Surfnet\StepupMiddleware\ApiBundle\Response\JsonCollectionResponse;
28
use Symfony\Component\HttpFoundation\JsonResponse;
29
use Symfony\Component\HttpFoundation\Request;
30
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
31
32
final class RaSecondFactorController extends AbstractController
33
{
34
    public function __construct(
35
        private readonly RaSecondFactorService $raSecondFactorService,
36
        private readonly AuthorizationContextService $authorizationService,
37
    ) {
38
    }
39
40
    public function collection(Request $request): JsonCollectionResponse
41
    {
42
        $this->denyAccessUnlessGrantedOneOff(['ROLE_RA', 'ROLE_READ']);
43
44
        $query = $this->buildRaSecondFactorQuery($request);
45
46
        $paginator = $this->raSecondFactorService->search($query);
47
48
        $filters = $this->raSecondFactorService->getFilterOptions($query);
49
50
        return JsonCollectionResponse::fromPaginator($paginator, $filters);
51
    }
52
53
    public function export(Request $request): JsonResponse
54
    {
55
        $this->denyAccessUnlessGrantedOneOff(['ROLE_RA', 'ROLE_READ']);
56
57
        $query = $this->buildRaSecondFactorQuery($request);
58
59
        $results = $this->raSecondFactorService->searchUnpaginated($query);
60
61
        return new JsonResponse($results);
62
    }
63
64
    /**
65
     * @return RaSecondFactorQuery
66
     */
67
    private function buildRaSecondFactorQuery(Request $request): RaSecondFactorQuery
68
    {
69
        $actorIdString = $request->query->get('actorId');
70
        if (!is_string($actorIdString)) {
71
            throw new BadRequestHttpException(sprintf('Invalid actorId "%s"', $actorIdString));
72
        }
73
74
        $actorId = new IdentityId($actorIdString);
75
76
        $query = new RaSecondFactorQuery();
77
        $query->pageNumber = $request->query->getInt('p', 1);
78
        $query->name = $request->query->get('name');
79
        $query->type = $request->query->get('type');
80
        $query->secondFactorId = $request->query->get('secondFactorId');
81
        $query->email = $request->query->get('email');
82
        $query->institution = $request->query->get('institution');
83
        $query->status = $request->query->get('status');
84
        $query->orderBy = $request->query->get('orderBy');
85
        $query->orderDirection = $request->query->get('orderDirection');
86
        $query->authorizationContext = $this->authorizationService->buildInstitutionAuthorizationContext(
87
            $actorId,
88
            RegistrationAuthorityRole::ra(),
89
        );
90
91
        return $query;
92
    }
93
}
94