Passed
Push — master ( fb51bb...c6ef6c )
by Tim
07:46
created

Admin::admin()   B

Complexity

Conditions 5
Paths 9

Size

Total Lines 80
Code Lines 40

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 40
c 1
b 0
f 0
dl 0
loc 80
rs 8.9688
cc 5
nc 9
nop 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\Module\consentsimpleadmin\Controller;
6
7
use Exception;
8
use SimpleSAML\Auth;
9
use SimpleSAML\Configuration;
10
use SimpleSAML\Logger;
11
use SimpleSAML\Module\consent\Auth\Process\Consent;
12
use SimpleSAML\Session;
13
use SimpleSAML\Metadata\MetaDataStorageHandler;
14
use SimpleSAML\Module\consent\Store;
15
use SimpleSAML\XHTML\Template;
16
use Symfony\Component\HttpFoundation\Request;
17
18
/**
19
 * Controller class for the consentsimpleadmin module.
20
 *
21
 * This class serves the different views available in the module.
22
 *
23
 * @package simplesamlphp/simplesamlphp-module-consentsimpleadmin
24
 */
25
class Admin
26
{
27
    /** @var \SimpleSAML\Configuration */
28
    protected Configuration $config;
29
30
    /** @var \SimpleSAML\Session */
31
    protected Session $session;
32
33
34
    /**
35
     * Controller constructor.
36
     *
37
     * It initializes the global configuration and session for the controllers implemented here.
38
     *
39
     * @param \SimpleSAML\Configuration $config The configuration to use by the controllers.
40
     * @param \SimpleSAML\Session $session The session to use by the controllers.
41
     *
42
     * @throws \Exception
43
     */
44
    public function __construct(
45
        Configuration $config,
46
        Session $session
47
    ) {
48
        $this->config = $config;
49
        $this->session = $session;
50
    }
51
52
53
54
    /**
55
     * @param \Symfony\Component\HttpFoundation\Request $request The current request.
56
     *
57
     * @return \SimpleSAML\XHTML\Template
58
     */
59
    public function admin(Request $request): Template
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

59
    public function admin(/** @scrutinizer ignore-unused */ Request $request): Template

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
60
    {
61
        $consentconfig = Configuration::getConfig('module_consentSimpleAdmin.php');
62
63
        $as = $consentconfig->getValue('auth');
64
        $as = new Auth\Simple($as);
0 ignored issues
show
Bug introduced by
It seems like $as can also be of type null; however, parameter $authSource of SimpleSAML\Auth\Simple::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

64
        $as = new Auth\Simple(/** @scrutinizer ignore-type */ $as);
Loading history...
65
        $as->requireAuth();
66
67
        // Get all attributes
68
        $attributes = $as->getAttributes();
69
70
        // Get user ID
71
        $userid_attributename = $consentconfig->getValue('userid', 'eduPersonPrincipalName');
72
73
        if (empty($attributes[$userid_attributename])) {
74
            throw new Exception(sprintf(
75
                'Could not generate useridentifier for storing consent. Attribute [%s] was not available.',
76
                $userid_attributename
77
            ));
78
        }
79
80
        $userid = $attributes[$userid_attributename][0];
81
82
        // Get metadata storage handler
83
        $metadata = MetaDataStorageHandler::getMetadataHandler();
84
85
        // Get IdP id and metadata
86
        $idp_entityid = $as->getAuthData('saml:sp:IdP');
87
        if ($idp_entityid !== null) {
88
            // From a remote idp (as bridge)
89
            $idp_metadata = $metadata->getMetaData($idp_entityid, 'saml20-idp-remote');
90
        } else {
91
            // from the local idp
92
            $idp_entityid = $metadata->getMetaDataCurrentEntityID('saml20-idp-hosted');
93
            $idp_metadata = $metadata->getMetaData($idp_entityid, 'saml20-idp-hosted');
94
        }
95
96
        Logger::debug('consentAdmin: IdP is [' . $idp_entityid . ']');
97
98
        $source = $idp_metadata['metadata-set'] . '|' . $idp_entityid;
99
100
        // Parse consent config
101
        $consent_storage = Store::parseStoreConfig($consentconfig->getValue('store'));
102
103
        // Calc correct user ID hash
104
        $hashed_user_id = Consent::getHashedUserID($userid, $source);
105
106
        // Check if button with withdraw all consent was clicked
107
        if (array_key_exists('withdraw', $_REQUEST)) {
108
            Logger::info(
109
                'consentAdmin: UserID [' . $hashed_user_id . '] has requested to withdraw all consents given...'
110
            );
111
112
            $consent_storage->deleteAllConsents($hashed_user_id);
113
        }
114
115
        // Get all consents for user
116
        $user_consent_list = $consent_storage->getConsents($hashed_user_id);
117
118
        $consentServices = [];
119
        foreach ($user_consent_list as $c) {
120
            $consentServices[$c[1]] = 1;
121
        }
122
123
        Logger::debug(
124
            'consentAdmin: no of consents [' . count($user_consent_list) . '] no of services [' . count($consentServices) . ']'
125
        );
126
127
        // Init template
128
        $t = new Template($this->config, 'consentSimpleAdmin:consentadmin.twig');
129
        $translator = $t->getTranslator();
130
131
        $t->data['consentServices'] = count($consentServices);
132
        $t->data['consents'] = count($user_consent_list);
133
        $t->data['granted'] = $translator->t('{consentSimpleAdmin:consentsimpleadmin:granted}', [
0 ignored issues
show
Bug introduced by
The method t() does not exist on SimpleSAML\Locale\Translate. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

133
        /** @scrutinizer ignore-call */ 
134
        $t->data['granted'] = $translator->t('{consentSimpleAdmin:consentsimpleadmin:granted}', [

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
134
            '%NO%' => (string)$this->data['consents'],
135
            '%OF%' => (string)$this->data['consentServices'],
136
        ]);
137
138
        return $t;
139
    }
140
}
141