Passed
Push — master ( c89864...65eac8 )
by Jan
17:55 queued 12s
created

AnonymizedIPSecurityProvider::__invoke()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 9
nc 3
nop 0
dl 0
loc 17
rs 9.9666
c 1
b 0
f 0
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
22
use DH\Auditor\Security\SecurityProviderInterface;
23
use Symfony\Bundle\SecurityBundle\Security\FirewallMap;
24
use Symfony\Component\HttpFoundation\IpUtils;
25
use Symfony\Component\HttpFoundation\RequestStack;
26
27
class AnonymizedIPSecurityProvider implements SecurityProviderInterface
28
{
29
    private $requestStack;
30
    private $firewallMap;
31
32
    public function __construct(RequestStack $requestStack, FirewallMap $firewallMap)
33
    {
34
        $this->requestStack = $requestStack;
35
        $this->firewallMap = $firewallMap;
36
    }
37
38
    public function __invoke(): array
39
    {
40
        $clientIp = null;
41
        $firewallName = null;
42
43
        $request = $this->requestStack->getCurrentRequest();
44
        if (null !== $request) {
45
            $firewallConfig = $this->firewallMap->getFirewallConfig($request);
46
47
            $clientIp = $request->getClientIp();
48
            //Censor clientIP to be GPDR conform
49
            $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

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