AnonymizedIPSecurityProvider   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
dl 0
loc 28
rs 10
c 1
b 0
f 0
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A __invoke() 0 17 3
1
<?php
2
/*
3
 * Copyright (C) 2020  Jan Böhmer
4
 *
5
 * This program is free software: you can redistribute it and/or modify
6
 * it under the terms of the GNU Affero General Public License as published
7
 * by the Free Software Foundation, either version 3 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU Affero General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU Affero General Public License
16
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
17
 */
18
19
namespace App\Audit;
20
21
use DH\Auditor\Security\SecurityProviderInterface;
22
use Symfony\Bundle\SecurityBundle\Security\FirewallMap;
23
use Symfony\Component\HttpFoundation\IpUtils;
24
use Symfony\Component\HttpFoundation\RequestStack;
25
26
class AnonymizedIPSecurityProvider implements SecurityProviderInterface
27
{
28
    private $requestStack;
29
    private $firewallMap;
30
31
    public function __construct(RequestStack $requestStack, FirewallMap $firewallMap)
32
    {
33
        $this->requestStack = $requestStack;
34
        $this->firewallMap = $firewallMap;
35
    }
36
37
    public function __invoke(): array
38
    {
39
        $clientIp = null;
40
        $firewallName = null;
41
42
        $request = $this->requestStack->getCurrentRequest();
43
        if (null !== $request) {
44
            $firewallConfig = $this->firewallMap->getFirewallConfig($request);
45
46
            $clientIp = $request->getClientIp();
47
            //Censor clientIP to be GPDR conform
48
            $clientIp = IpUtils::anonymize($clientIp);
0 ignored issues
show
Bug introduced by
It seems like $clientIp can also be of type null; however, parameter $ip of Symfony\Component\HttpFo...on\IpUtils::anonymize() 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

48
            $clientIp = IpUtils::anonymize(/** @scrutinizer ignore-type */ $clientIp);
Loading history...
49
50
            $firewallName = null === $firewallConfig ? null : $firewallConfig->getName();
51
        }
52
53
        return [$clientIp, $firewallName];
54
    }
55
}
56