GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

Tree   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 124
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 95.74%

Importance

Changes 0
Metric Value
wmc 16
c 0
b 0
f 0
lcom 1
cbo 2
dl 0
loc 124
ccs 45
cts 47
cp 0.9574
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 3
A initializeDataTree() 0 5 1
A loadFeatures() 0 7 1
A evaluateFeatures() 0 6 1
A evaluateQuadrantData() 0 12 4
A isValidTimeZone() 0 4 1
B lookForTimeZone() 0 24 5
1
<?php
2
3
namespace GeoTimeZone\Quadrant;
4
5
use ErrorException;
6
use GeoTimeZone\Geometry\Utils;
7
8
class Tree extends Element
9
{
10
    const DATA_TREE_FILENAME = "index.json";
11
    const GEO_FEATURE_FILENAME = "geo.json";
12
    const NONE_TIMEZONE = "none";
13
    protected $dataTree = null;
14
    protected $dataDirectory;
15
    protected $utils;
16
    
17
    /**
18
     * Tree constructor.
19
     * @param $dataDirectory
20
     */
21 25
    public function __construct($dataDirectory=null)
22
    {
23 25
        if (isset($dataDirectory) && is_dir($dataDirectory)) {
24 25
            Element::__construct();
25 25
            $this->dataDirectory = $dataDirectory;
26 25
            $this->utils = new Utils();
27
        }else{
28
            new ErrorException('Invalid data directory: ' . $dataDirectory);
29
        }
30 25
    }
31
    
32
    /**
33
     * Data tree is loaded from json file
34
     */
35 25
    public function initializeDataTree()
36
    {
37 25
        $jsonData = file_get_contents($this->dataDirectory . self::DATA_TREE_FILENAME);
38 25
        $this->dataTree = json_decode($jsonData, true);
39 25
    }
40
    
41
    /**
42
     * Load json features data from a particular geo quadrant path
43
     * @param $quadrantPath
44
     * @return mixed
45
     */
46 7
    protected function loadFeatures($quadrantPath)
47
    {
48 7
        $filePath = $this->dataDirectory . implode('/', str_split($quadrantPath)) . DIRECTORY_SEPARATOR .
49 7
            self::GEO_FEATURE_FILENAME;
50 7
        $geoJson = json_decode(file_get_contents($filePath), true);
51 7
        return $geoJson;
52
    }
53
    
54
    /**
55
     * Check if a particular location (latitude, longitude)is IN a particular quadrant
56
     * @param $quadrantPath
57
     * @param $latitude
58
     * @param $longitude
59
     * @return string
60
     */
61 7
    protected function evaluateFeatures($quadrantPath, $latitude, $longitude)
62
    {
63 7
        $features = $this->loadFeatures($quadrantPath);
64 7
        $timeZone = $this->utils->isPointInQuadrantFeatures($features, $latitude, $longitude);
65 7
        return $timeZone;
66
    }
67
    
68
    /**
69
     * Get valid timezone
70
     * @param $zoneData
71
     * @param $quadrantPath
72
     * @param $latitude
73
     * @param $longitude
74
     * @return string
75
     * @throws ErrorException
76
     */
77 17
    protected function evaluateQuadrantData($zoneData, $quadrantPath, $latitude, $longitude)
78
    {
79 17
        $validTimezone = self::NONE_TIMEZONE;
80 17
        if (!isset($zoneData)) {
81
            throw new ErrorException('Unexpected data type');
82 17
        } elseif ($zoneData === "f") {
83 7
            $validTimezone = $this->evaluateFeatures($quadrantPath, $latitude, $longitude);
84 17
        } elseif (is_numeric($zoneData)) {
85 4
            $validTimezone = $this->dataTree['timezones'][$zoneData];
86
        }
87 17
        return $validTimezone;
88
    }
89
    
90
    /**
91
     * Check if timezone is valid
92
     * @param $timeZone
93
     * @return bool
94
     */
95 17
    protected function isValidTimeZone($timeZone)
96
    {
97 17
        return $timeZone != self::NONE_TIMEZONE;
98
    }
99
    
100
    /**
101
     * Main function for looking the timezone associated to a particular location (latitude, longitude)
102
     * @param $latitude
103
     * @param $longitude
104
     * @return string
105
     * @throws ErrorException
106
     */
107 17
    public function lookForTimeZone($latitude, $longitude)
108
    {
109 17
        $geoQuadrant = new Element();
110 17
        $timeZone = self::NONE_TIMEZONE;
111 17
        $quadrantPath = '';
112 17
        $quadrantTree = $this->dataTree['lookup'];
113
        
114 17
        while (!$this->isValidTimeZone($timeZone)) {
115 17
            $geoQuadrant->moveToNextQuadrant($latitude, $longitude);
116 17
            if (!isset($quadrantTree[$geoQuadrant->getLevel()])) {
117 6
                break;
118
            }
119 17
            $quadrantTree =  $quadrantTree[$geoQuadrant->getLevel()];
120 17
            $quadrantPath = $quadrantPath . $geoQuadrant->getLevel();
121 17
            $timeZone = $this->evaluateQuadrantData($quadrantTree, $quadrantPath, $latitude, $longitude);
122 17
            $geoQuadrant->updateMidCoordinates();
123
        }
124
        
125 17
        if ($timeZone == self::NONE_TIMEZONE || $timeZone == Utils::NOT_FOUND_IN_FEATURES) {
126 7
            throw new ErrorException("ERROR: TimeZone not found");
127
        }
128
        
129 10
        return $timeZone;
130
    }
131
}
132