Passed
Push — release_2_0 ( b4987f...d95ecd )
by Stefan
10:37
created

ExternalEduroamDBData   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 14
eloc 31
dl 0
loc 75
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A updateFreshness() 0 1 1
A __construct() 0 3 1
A splitNames() 0 18 5
B allServiceProviders() 0 21 7
1
<?php
2
3
/*
4
 * *****************************************************************************
5
 * Contributions to this work were made on behalf of the GÉANT project, a 
6
 * project that has received funding from the European Union’s Framework 
7
 * Programme 7 under Grant Agreements No. 238875 (GN3) and No. 605243 (GN3plus),
8
 * Horizon 2020 research and innovation programme under Grant Agreements No. 
9
 * 691567 (GN4-1) and No. 731122 (GN4-2).
10
 * On behalf of the aforementioned projects, GEANT Association is the sole owner
11
 * of the copyright in all material which was developed by a member of the GÉANT
12
 * project. GÉANT Vereniging (Association) is registered with the Chamber of 
13
 * Commerce in Amsterdam with registration number 40535155 and operates in the 
14
 * UK as a branch of GÉANT Vereniging.
15
 * 
16
 * Registered office: Hoekenrode 3, 1102BR Amsterdam, The Netherlands. 
17
 * UK branch address: City House, 126-130 Hills Road, Cambridge CB2 1PQ, UK
18
 *
19
 * License: see the web/copyright.inc.php file in the file structure or
20
 *          <base_url>/copyright.php after deploying the software
21
 */
22
23
/**
24
 * This file contains the AbstractProfile class. It contains common methods for
25
 * both RADIUS/EAP profiles and SilverBullet profiles
26
 *
27
 * @author Stefan Winter <[email protected]>
28
 * @author Tomasz Wolniewicz <[email protected]>
29
 *
30
 * @package Developer
31
 *
32
 */
33
34
namespace core;
35
36
use \Exception;
37
38
/**
39
 * This class interacts with the external DB to fetch operational data for
40
 * read-only purposes.
41
 * 
42
 * @author Stefan Winter <[email protected]>
43
 *
44
 * @license see LICENSE file in root directory
45
 *
46
 * @package Developer
47
 */
48
class ExternalEduroamDBData extends EntityWithDBProperties {
49
50
    private $counter = -1;
0 ignored issues
show
Coding Style Documentation introduced by
Missing member variable doc comment
Loading history...
51
52
    /**
53
     * constructor, gives us access to the DB handle we need for queries
54
     */
55
    public function __construct() {
56
        $this->databaseType = "EXTERNAL";
57
        parent::__construct();
58
    }
59
60
    /**
61
     * we don't write anything to the external DB, so no need to track update
62
     * timestamps
63
     * 
64
     * @return void
65
     */
66
    public function updateFreshness() {
67
        
68
    }
69
70
    /**
71
     * eduroam DB delivers a string with all name variants mangled in one. Pry
72
     * it apart.
73
     * 
74
     * @param string $nameRaw the string with all name variants coerced into one
75
     * @return array language/name pair
76
     */
77
    private function splitNames($nameRaw) {
78
        // the delimiter used by eduroam DB is ; but that is ALSO an allowed
79
        // character in payload, and in active use. We need to try and find out
80
        // which semicolon should NOT be considered a language delimiter...
81
        $variants = explode('#', $nameRaw);
82
        $submatches = [];
83
        $returnArray = [];
84
        foreach ($variants as $oneVariant) {
85
            if ($oneVariant == NULL) {
86
                return [];
87
            }
88
            if (!preg_match('/^(..)\:\ (.*)/', $oneVariant, $submatches) || !isset($submatches[2])) {
89
                $this->loggerInstance->debug(2, "[$nameRaw] We expect 'en: bla but found '$oneVariant'.");
90
                continue;
91
            }
92
            $returnArray[$submatches[1]] = $submatches[2];
93
        }
94
        return $returnArray;
95
    }
96
97
    /**
98
     * retrieves the list of all service providers from the eduroam database
99
     * 
100
     * @return integer number of providers
101
     */
102
    public function allServiceProviders() {
103
        if ($this->counter > -1) {
104
            return $this->counter;
105
        }
106
107
        $cachedNumber = @file_get_contents(ROOT . "/var/tmp/cachedSPNumber.serialised");
108
        if ($cachedNumber !== FALSE) {
109
            $numberData = unserialize($cachedNumber);
110
            $now = new \DateTime();
111
            $cacheDate = $numberData["timestamp"]; // this is a DateTime object
112
            $diff = $now->diff($cacheDate);
113
            if ($diff->y == 0 && $diff->m == 0 && $diff->d == 0) {
114
                $this->counter = $numberData["number"];
115
                return $this->counter;
116
            }
117
        } else { // data in cache is too old or doesn't exist. We really need to ask the database
118
            $query = $this->databaseHandle->exec("SELECT count(*) AS total FROM view_active_SP_location_eduroamdb");
119
            while ($iterator = mysqli_fetch_object(/** @scrutinizer ignore-type */ $query)) {
120
                $this->counter = $iterator->total;
121
            }
122
            file_put_contents(ROOT . "/var/tmp/cachedSPNumber.serialised", serialize(["number" => $this->counter, "timestamp" => new \DateTime()]));
123
        }
124
    }
125
126
}
127