MyConnectionsController::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.6333
c 0
b 0
f 0
cc 1
nc 1
nop 8

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
/**
4
 * Copyright 2017 SURFnet B.V.
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 OpenConext\ProfileBundle\Controller;
20
21
use OpenConext\Profile\Api\AuthenticatedUserProviderInterface;
22
use OpenConext\Profile\Value\EmailAddress;
23
use OpenConext\Profile\Value\EmailAddressSupport;
24
use OpenConext\ProfileBundle\Form\Type\ConfirmConnectionDeleteType;
25
use OpenConext\ProfileBundle\Security\Guard;
26
use OpenConext\ProfileBundle\Service\AttributeAggregationService;
27
use Psr\Log\LoggerInterface;
28
use Symfony\Component\Form\FormFactoryInterface;
29
use Symfony\Component\HttpFoundation\RedirectResponse;
30
use Symfony\Component\HttpFoundation\Request;
31
use Symfony\Component\HttpFoundation\Response;
32
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
33
use Symfony\Component\Templating\EngineInterface;
34
35
/**
36
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
37
 */
38
class MyConnectionsController
39
{
40
    /**
41
     * @var EngineInterface
42
     */
43
    private $templateEngine;
44
45
    /**
46
     * @var Guard
47
     */
48
    private $guard;
49
50
    /**
51
     * @var LoggerInterface
52
     */
53
    private $logger;
54
55
    /**
56
     * @var AttributeAggregationService
57
     */
58
    private $service;
59
60
    /**
61
     * @var AuthenticatedUserProviderInterface
62
     */
63
    private $userProvider;
64
65
    /**
66
     * @var EmailAddress
67
     */
68
    private $mailTo;
69
70
    /**
71
     * @var FormFactoryInterface
72
     */
73
    private $formFactory;
74
75
    /**
76
     * @var UrlGeneratorInterface
77
     */
78
    private $urlGenerator;
79
80
    /**
81
     * @param EngineInterface $templateEngine
82
     * @param Guard $guard
83
     * @param LoggerInterface $logger
84
     * @param AttributeAggregationService $service
85
     * @param AuthenticatedUserProviderInterface $userProvider
86
     * @param EmailAddressSupport $mailTo
87
     * @param FormFactoryInterface $formFactory
88
     * @param UrlGeneratorInterface $urlGenerator
89
     */
90
    public function __construct(
91
        EngineInterface $templateEngine,
92
        Guard $guard,
93
        LoggerInterface $logger,
94
        AttributeAggregationService $service,
95
        AuthenticatedUserProviderInterface $userProvider,
96
        EmailAddressSupport $mailTo,
97
        FormFactoryInterface $formFactory,
98
        UrlGeneratorInterface $urlGenerator
99
    ) {
100
        $this->templateEngine = $templateEngine;
101
        $this->guard = $guard;
102
        $this->logger = $logger;
103
        $this->service = $service;
104
        $this->userProvider = $userProvider;
105
        $this->mailTo = $mailTo;
0 ignored issues
show
Documentation Bug introduced by
$mailTo is of type object<OpenConext\Profil...ue\EmailAddressSupport>, but the property $mailTo was declared to be of type object<OpenConext\Profile\Value\EmailAddress>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
106
        $this->formFactory = $formFactory;
107
        $this->urlGenerator = $urlGenerator;
108
    }
109
110
    /**
111
     * @param Request $request
112
     *
113
     * @return Response
114
     */
115
    public function overviewAction(Request $request)
116
    {
117
        $this->guard->userIsLoggedIn();
118
        $this->logger->notice('Showing My Connections page');
119
120
        $user = $this->userProvider->getCurrentUser();
121
        $attributes = $this->service->findByUser($user);
122
123
        $activeConnections = [];
124
        $availableConnections = [];
125
126
        if ($attributes) {
127
            $activeConnections = $attributes->getActiveAttributes();
128
            $availableConnections = $attributes->getAvailableAttributes();
129
        }
130
131
        // For now only the ORCID connection form is created and added to the form.
132
        $confirmationForm = $this->formFactory->create(ConfirmConnectionDeleteType::class);
133
134
        $confirmationForm->handleRequest($request);
135
        if ($confirmationForm->isSubmitted() && $confirmationForm->isValid()) {
136
            $this->logger->notice('The authenticated user is disconnecting ORCID iD.');
137
            $this->service->disconnectAttributeFor($user, $attributes->getAttribute('ORCID'));
138
            return new RedirectResponse($this->urlGenerator->generate('profile.my_connections_overview'));
139
        }
140
141
        return new Response($this->templateEngine->render(
142
            'OpenConextProfileBundle:MyConnections:overview.html.twig',
143
            [
144
                'activeConnections' => $activeConnections,
145
                'availableConnections' => $availableConnections,
146
                'mailTo' => $this->mailTo->getEmailAddress(),
147
                'confirmForm' => $confirmationForm->createView(),
148
            ]
149
        ));
150
    }
151
}
152