Code Coverage |
||||||||||||||||
Lines |
Branches |
Paths |
Functions and Methods |
Classes and Traits |
||||||||||||
Total | |
88.24% |
30 / 34 |
|
80.00% |
12 / 15 |
|
54.55% |
6 / 11 |
|
40.00% |
2 / 5 |
CRAP | |
0.00% |
0 / 1 |
ThematicEffect | |
88.24% |
30 / 34 |
|
80.00% |
12 / 15 |
|
54.55% |
6 / 11 |
|
40.00% |
2 / 5 |
19.39 | |
0.00% |
0 / 1 |
__construct | |
88.89% |
8 / 9 |
|
88.89% |
8 / 9 |
|
50.00% |
3 / 6 |
|
0.00% |
0 / 1 |
8.12 | |||
getIterator | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
fetch | n/a |
0 / 0 |
n/a |
0 / 0 |
n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||||
fetchFromLocalPath | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
fetchFromUrl | |
90.91% |
20 / 22 |
|
66.67% |
2 / 3 |
|
50.00% |
1 / 2 |
|
0.00% |
0 / 1 |
2.50 | |||
isValidType | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | namespace Hsit\Webservice\Event; |
3 | |
4 | use ArrayIterator; |
5 | use IteratorAggregate; |
6 | use Traversable; |
7 | |
8 | use Fdsn\DataStructure\Id as DS_Id; |
9 | use Fdsn\DataStructure\LatLon as DS_LatLon; |
10 | |
11 | /** |
12 | * Fetch ThematicEffect data from HSIT portal |
13 | * |
14 | * @param Fdsn\DataStructure\Id $fdsnQuakeId Id quake to scan for data |
15 | * @param string $thematicEffect study (now only ????? are supported) |
16 | * @param string $localFileFullPath path to fetch local file (if exists AND is readable) |
17 | */ |
18 | abstract class ThematicEffect implements IteratorAggregate { |
19 | protected const templateUrl = 'https://e.hsit.it/%d/%d_%s.%s'; |
20 | |
21 | protected DS_Id $idQuake; |
22 | protected string $thematicEffectStudyName; |
23 | protected ?string $localFileFullPath = null; |
24 | |
25 | protected array $places = array(); |
26 | |
27 | public function __construct(DS_Id $fdsnQuakeId, string $thematicEffectStudyName, ?string $localFileFullPath = null){ |
28 | if( ! $this->isValidType(strtolower($thematicEffectStudyName)) ) |
29 | throw new \RuntimeException(sprintf("[%s] Type %s not valid", __METHOD__, $thematicEffectStudyName)); |
30 | |
31 | $this->idQuake = $fdsnQuakeId; |
32 | $this->thematicEffectStudyName = strtolower($thematicEffectStudyName); |
33 | |
34 | if( is_null($localFileFullPath) ) |
35 | return; |
36 | |
37 | if ( ! file_exists($localFileFullPath) || ! is_readable($localFileFullPath) ) |
38 | throw new \RuntimeException(sprintf("[%s] file %s not exists or is not readable", __METHOD__, $localFileFullPath)); |
39 | |
40 | $this->localFileFullPath = $localFileFullPath; |
41 | } |
42 | |
43 | /** |
44 | * get all places percentage |
45 | * |
46 | * @return array locality percentage (array of associative arrays[lat, lon, percentage, reports, placeName] ) |
47 | */ |
48 | public function getIterator():Traversable{ return new ArrayIterator($this->places); } |
49 | |
50 | /** |
51 | * fetch data from url (remote or local - if $localFileFullPath is set in __construct) |
52 | * |
53 | * @return int number of data found |
54 | */ |
55 | abstract public function fetch():int; |
56 | |
57 | /** |
58 | * fetch data from local path - if $localFileFullPath is set in __construct) |
59 | * |
60 | * @return array data from file, splitted in array of lines |
61 | */ |
62 | protected function fetchFromLocalPath():string { return file_get_contents( $this->localFileFullPath); } |
63 | |
64 | /** |
65 | * fetch data from remote url path - if $localFileFullPath is set NOT in __construct) |
66 | * |
67 | * @return array data from file, splitted in array of lines |
68 | */ |
69 | protected function fetchFromUrl():string{ |
70 | $callingClass = get_called_class(); |
71 | |
72 | $curlSession = curl_init(); |
73 | curl_setopt_array($curlSession, array( |
74 | CURLOPT_URL => sprintf(self::templateUrl, |
75 | $this->idQuake->value(), |
76 | $this->idQuake->value(), |
77 | $this->thematicEffectStudyName, |
78 | $callingClass::fileExtension), |
79 | CURLOPT_HEADER => false, |
80 | CURLOPT_CUSTOMREQUEST => 'GET', |
81 | CURLOPT_RETURNTRANSFER => true, |
82 | CURLOPT_FAILONERROR => true |
83 | ) |
84 | ); |
85 | $downloadedData = curl_exec($curlSession); |
86 | $this->curlErrNum = curl_errno($curlSession); |
87 | $this->curlErrInfo = curl_error($curlSession); |
88 | curl_close($curlSession); |
89 | |
90 | if(CURLE_OK != $this->curlErrNum){ |
91 | error_log(sprintf("[%s] [CurlErr: %d] %s", __METHOD__, $this->curlErrNum, $this->curlErrInfo)); |
92 | return -1; |
93 | } |
94 | |
95 | return $downloadedData; |
96 | } |
97 | |
98 | /** |
99 | * check if thematic effect study is supported |
100 | * |
101 | * @param string $thematicEffectStudyName name of the thematic effect study |
102 | * |
103 | * @return bool TRUE if is supported, FALSE otherwise |
104 | */ |
105 | private function isValidType(string $thematicEffectStudyName):bool{ return preg_match("(acousticeffect|animals|china|doors|fear|forniture|hangingobjects|liquids|locality|pictures|smallobjects)", $thematicEffectStudyName); } |
106 | |
107 | } |
108 | ?> |