Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
90.91% covered (success)
90.91%
10 / 11
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
IPFactory
90.91% covered (success)
90.91%
10 / 11
50.00% covered (danger)
50.00%
1 / 2
7.04
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 create
90.00% covered (success)
90.00%
9 / 10
0.00% covered (danger)
0.00%
0 / 1
6.04
1<?php
2
3/**
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; under version 2
7 * of the License (non-upgradable).
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17 *
18 * Copyright (c) 2020 (original work) (update and modification) Open Assessment Technologies SA
19 *                    (under the project TAO-PRODUCT)
20 */
21
22declare(strict_types=1);
23
24namespace oat\tao\model\oauth\lockout;
25
26/**
27 * Lock based on IP
28 * @package oat\tao\model\oauth\lockout
29 */
30class IPFactory
31{
32    /** @var array */
33    private $flags;
34    /** @var string */
35    private $ip = '';
36
37    public function __construct(array $serverFlags = [])
38    {
39        $this->flags = $serverFlags;
40    }
41
42    /**
43     * @return string
44     */
45    public function create(): string
46    {
47        if (empty($this->ip) && $this->flags) {
48            foreach ($this->flags as $flag) {
49                if (!empty($_SERVER[$flag])) {
50                    $this->ip = $_SERVER[$flag];
51                    break;
52                }
53            }
54        }
55        if (empty($this->ip)) {
56            $this->ip = $_SERVER['HTTP_CLIENT_IP']
57                ?? $_SERVER['HTTP_X_FORWARDED_FOR']
58                ?? $_SERVER['REMOTE_ADDR'];
59        }
60        return $this->ip;
61    }
62}