Passed
Pull Request — master (#58)
by Alberto
02:16
created

IdentityHelper   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
eloc 11
c 2
b 0
f 1
dl 0
loc 37
rs 10
wmc 3

1 Method

Rating   Name   Duplication   Size   Complexity  
A __call() 0 13 3
1
<?php
2
declare(strict_types=1);
3
4
/**
5
 * BEdita, API-first content management framework
6
 * Copyright 2022 Atlas Srl, Chialab Srl
7
 *
8
 * This file is part of BEdita: you can redistribute it and/or modify
9
 * it under the terms of the GNU Lesser General Public License as published
10
 * by the Free Software Foundation, either version 3 of the License, or
11
 * (at your option) any later version.
12
 *
13
 * See LICENSE.LGPL or <http://gnu.org/licenses/lgpl-3.0.html> for more details.
14
 */
15
namespace BEdita\WebTools\View\Helper;
16
17
use Authentication\View\Helper\IdentityHelper as AuthenticationIdentityHelper;
18
19
/**
20
 * Extends IdentityHelper allowing to delegate methods to the identity.
21
 */
22
class IdentityHelper extends AuthenticationIdentityHelper
23
{
24
    /**
25
     * Configuration options
26
     *
27
     * - `identityAttribute` - The request attribute which holds the identity.
28
     * - `delagateMethods` - Methods delegated to identity.
29
     *
30
     * @var array
31
     */
32
    protected $_defaultConfig = [
33
        'identityAttribute' => 'identity',
34
        'delegateMethods' => [
35
            'hasRole',
36
        ],
37
    ];
38
39
    /**
40
     * Delegate methods to identity.
41
     *
42
     * @param string $method The method being invoked.
43
     * @param array $args The arguments for the method.
44
     * @return mixed
45
     */
46
    public function __call($method, $args)
47
    {
48
        if (!in_array($method, $this->getConfig('delegateMethods'))) {
0 ignored issues
show
Bug introduced by
It seems like $this->getConfig('delegateMethods') can also be of type null; however, parameter $haystack of in_array() does only seem to accept array, 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

48
        if (!in_array($method, /** @scrutinizer ignore-type */ $this->getConfig('delegateMethods'))) {
Loading history...
49
            throw new \BadMethodCallException("Cannot call `{$method}`. Make sure to add it to `delegateMethods`.");
50
        }
51
52
        if (!is_object($this->_identity)) {
53
            throw new \BadMethodCallException("Cannot call `{$method}` on stored identity since it is not an object.");
54
        }
55
56
        $call = [$this->_identity, $method];
57
58
        return $call(...$args);
59
    }
60
}
61