Passed
Push — master ( 1974f7...769154 )
by Tomasz
04:46
created

UserLocation::__construct()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 16
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 14
nc 4
nop 0
dl 0
loc 16
rs 9.2
c 0
b 0
f 0
1
<?php
2
3
/* 
4
 * ******************************************************************************
5
 * Copyright 2011-2017 DANTE Ltd. and GÉANT on behalf of the GN3, GN3+, GN4-1 
6
 * and GN4-2 consortia
7
 * 
8
 *  License: see the web/copyright.php file in the file structure
9
 * ******************************************************************************
10
 */
11
12
/*
13
 * This product includes GeoLite data created by MaxMind, available from
14
 * http://www.maxmind.com
15
 */
16
17
namespace core;
18
19
use GeoIp2\Database\Reader;
20
use \Exception;
21
22
class UserLocation {
23
        /**
24
     * find out where the user is currently located
25
     * @return array
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
26
     */
27
    public function __construct() {
28
        $geoipVersion = CONFIG['GEOIP']['version'] ?? 0;
29
        switch ($geoipVersion) {
30
            case 0:
31
                $this->location = ['status' => 'error', 'error' => 'Geolocation not supported'];
32
                break;
33
            case 1:
34
                $this->location = $this->locateUser1();
35
                break;
36
            case 2:
37
                $this->location = $this->locateUser2();
38
                break;
39
            default:
40
                throw new Exception("This version of GeoIP is not known!");
41
        }
42
    }
43
    
44
    
45
    private function locateUser1() {
46 View Code Duplication
        if (CONFIG['GEOIP']['version'] != 1) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
47
            return ['status' => 'error', 'error' => 'Function for GEOIPv1 called, but config says this is not the version to use!'];
48
        }
49
        //$host = $_SERVER['REMOTE_ADDR'];
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
50
        $host = filter_input(INPUT_SERVER, 'REMOTE_ADDR', FILTER_VALIDATE_IP);
51
        $record = geoip_record_by_name($host);
52
        if ($record === FALSE) {
53
            return ['status' => 'error', 'error' => 'Problem getting the address'];
54
        }
55
        $result = ['status' => 'ok'];
56
        $result['country'] = $record['country_code'];
57
//  the two lines below are a dirty hack to take of the error in naming the UK federation
58
        if ($result['country'] == 'GB') {
59
            $result['country'] = 'UK';
60
        }
61
        $result['region'] = $record['region'];
62
        $result['geo'] = ['lat' => (float) $record['latitude'], 'lon' => (float) $record['longitude']];
63
        return($result);
64
    }
65
    
66
    /**
67
     * find out where the user is currently located, using GeoIP2
68
     * @return array
69
     */
70
    private function locateUser2() {
71 View Code Duplication
        if (CONFIG['GEOIP']['version'] != 2) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
72
            return ['status' => 'error', 'error' => 'Function for GEOIPv2 called, but config says this is not the version to use!'];
73
        }
74
        require_once CONFIG['GEOIP']['geoip2-path-to-autoloader'];
75
        $reader = new Reader(CONFIG['GEOIP']['geoip2-path-to-db']);
76
        $host = filter_input(INPUT_SERVER, 'REMOTE_ADDR', FILTER_VALIDATE_IP);
77
        try {
78
            $record = $reader->city($host);
79
        } catch (\Exception $e) {
80
            $result = ['status' => 'error', 'error' => 'Problem getting the address'];
81
            return($result);
82
        }
83
        $result = ['status' => 'ok'];
84
        $result['country'] = $record->country->isoCode;
85
//  the two lines below are a dirty hack to take of the error in naming the UK federation
86
        if ($result['country'] == 'GB') {
87
            $result['country'] = 'UK';
88
        }
89
        $result['region'] = $record->continent->name;
90
91
        $result['geo'] = ['lat' => (float) $record->location->latitude, 'lon' => (float) $record->location->longitude];
92
        return($result);
93
    }
94
    public $location;
95
}