Completed
Push — 2.0 ( 359ae1...221cc7 )
by Rafał
51:27 queued 18:07
created

GeoIPChecker   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 1
dl 0
loc 33
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A isGranted() 0 22 5
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Superdesk Publisher Geo IP Component.
7
 *
8
 * Copyright 2019 Sourcefabric z.ú. and contributors.
9
 *
10
 * For the full copyright and license information, please see the
11
 * AUTHORS and LICENSE files distributed with this source code.
12
 *
13
 * @copyright 2019 Sourcefabric z.ú
14
 * @license http://www.superdesk.org/license
15
 */
16
17
namespace SWP\Component\GeoIP\Checker;
18
19
use GeoIp2\Exception\AddressNotFoundException;
20
use SWP\Component\GeoIP\Reader\ReaderInterface;
21
22
class GeoIPChecker
23
{
24
    /** @var ReaderInterface */
25
    private $reader;
26
27
    public function __construct(ReaderInterface $reader)
28
    {
29
        $this->reader = $reader;
30
    }
31
32
    public function isGranted(string $ipAddress, array $places): bool
33
    {
34
        try {
35
            foreach ($places as $place) {
36
                $state = $place->getState();
37
38
                if ($state === $this->reader->getState($ipAddress)) {
39
                    return false;
40
                }
41
42
                $country = $place->getCountry();
43
44
                if ($country === $this->reader->getCountry($ipAddress)) {
45
                    return false;
46
                }
47
            }
48
49
            return true;
50
        } catch (AddressNotFoundException $e) {
51
            return true;
52
        }
53
    }
54
}
55