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

GeoIPChecker::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
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