Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
79.49% |
31 / 39 |
|
25.00% |
2 / 8 |
CRAP | |
0.00% |
0 / 1 |
| Hsit | |
79.49% |
31 / 39 |
|
25.00% |
2 / 8 |
16.94 | |
0.00% |
0 / 1 |
| __construct | |
80.00% |
4 / 5 |
|
0.00% |
0 / 1 |
3.07 | |||
| makeListOfAvailableResources | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| fetchIndex | |
75.00% |
3 / 4 |
|
0.00% |
0 / 1 |
2.06 | |||
| addFilterByStudy | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
6 | |||
| listResources | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| getIterator | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| fetchFromLocalPath | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| fetchFromUrl | |
88.89% |
16 / 18 |
|
0.00% |
0 / 1 |
2.01 | |||
| 1 | <?php |
| 2 | namespace Hsit\Webservice; |
| 3 | |
| 4 | use ArrayIterator; |
| 5 | use IteratorAggregate; |
| 6 | use Traversable; |
| 7 | |
| 8 | use Fdsn\Webservices\Event\Data\Id as DS_Id; |
| 9 | |
| 10 | /** |
| 11 | * Fetch HSIT data from portal |
| 12 | * |
| 13 | * @param Fdsn\Webservices\Event\Data\Id $fdsnQuakeId Id quake to scan for data |
| 14 | * @param string $macroseismicStudyName macroseismic scale study (now only mcs|ems are supported) |
| 15 | * @param bool $fetchMunicipality TRUE fetch municipality data, FALSE fetch locality data. Default is TRUE |
| 16 | * @param string $localFileFullPath path to fetch local file (if exists AND is readable) |
| 17 | */ |
| 18 | class Hsit implements IteratorAggregate { |
| 19 | public const templateUrl = 'https://e.hsit.it/%d/index.html.xml'; |
| 20 | |
| 21 | private DS_Id $idQuake; |
| 22 | private ?string $indexXmlLocalPath = null; |
| 23 | |
| 24 | private \DOMDocument $indexXml; |
| 25 | private array $availableIntensities = array(); |
| 26 | private array $availableThematicEffect = array(); |
| 27 | |
| 28 | private int $curlErrNum; |
| 29 | private string $curlErrInfo; |
| 30 | |
| 31 | public function __construct(DS_Id $fdsnQuakeId, ?string $indexXmlLocalPath = null){ |
| 32 | $this->idQuake = $fdsnQuakeId; |
| 33 | |
| 34 | if ( ! is_null ($indexXmlLocalPath) && file_exists($indexXmlLocalPath) ) |
| 35 | $this->indexXmlLocalPath = $indexXmlLocalPath; |
| 36 | |
| 37 | $this->fetchIndex(); |
| 38 | $this->makeListOfAvailableResources(); |
| 39 | } |
| 40 | |
| 41 | private function makeListOfAvailableResources():int{ |
| 42 | $elaboratedNode = $this->indexXml->getElementsByTagName('Intensity'); |
| 43 | for( $i = 0; $i < $elaboratedNode->length; $i++) |
| 44 | echo $elaboratedNode->item($i)->nodeValue . PHP_EOL; |
| 45 | |
| 46 | return $elaboratedNode->length; |
| 47 | |
| 48 | } |
| 49 | |
| 50 | private function fetchIndex():bool{ |
| 51 | $this->indexXml = new \DOMDocument(); |
| 52 | if ( is_null($this->indexXmlLocalPath) ) |
| 53 | return $this->indexXml->loadXml( $this->fetchFromUrl() ); |
| 54 | |
| 55 | return $this->indexXml->load( $this->fetchFromLocalPath() ); |
| 56 | } |
| 57 | |
| 58 | public function addFilterByStudy(string $studyName, bool $fetchMunicipality = true):void{ |
| 59 | if ( in_array($studyName, $this->validStudies ) ) |
| 60 | $this->studiesToFetch[] = $studyName; |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Show resources found |
| 65 | * |
| 66 | * @return int number of elements found |
| 67 | */ |
| 68 | public function listResources():int{ |
| 69 | $elaboratedNode = $this->indexXml->getElementsByTagName('DataURL'); |
| 70 | for( $i = 0; $i < $elaboratedNode->length; $i++) |
| 71 | echo $elaboratedNode->item($i)->nodeValue . PHP_EOL; |
| 72 | |
| 73 | return $elaboratedNode->length; |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * get all places intensity |
| 78 | * |
| 79 | * @return array locality intensity (array of associative arrays[lat, lon, intensity, reports, placeName] ) |
| 80 | */ |
| 81 | public function getIterator():Traversable{ return new ArrayIterator($this->intensityToFetch); } |
| 82 | |
| 83 | /** |
| 84 | * fetch data from local path - if $localFileFullPath is set in __construct) |
| 85 | * |
| 86 | * @return array data from file, splitted in array of lines |
| 87 | */ |
| 88 | protected function fetchFromLocalPath():string { return file_get_contents( $this->indexXmlLocalPath); } |
| 89 | |
| 90 | /** |
| 91 | * fetch data from remote url path - if $localFileFullPath is set NOT in __construct) |
| 92 | * |
| 93 | * @return array data from file, splitted in array of lines |
| 94 | */ |
| 95 | protected function fetchFromUrl():string{ |
| 96 | $callingClass = get_called_class(); |
| 97 | |
| 98 | $curlSession = curl_init(); |
| 99 | curl_setopt_array($curlSession, array( |
| 100 | CURLOPT_URL => sprintf(self::templateUrl, $this->idQuake->value() ), |
| 101 | CURLOPT_HEADER => false, |
| 102 | CURLOPT_CUSTOMREQUEST => 'GET', |
| 103 | CURLOPT_RETURNTRANSFER => true, |
| 104 | CURLOPT_FAILONERROR => true |
| 105 | ) |
| 106 | ); |
| 107 | $downloadedData = curl_exec($curlSession); |
| 108 | $this->curlErrNum = curl_errno($curlSession); |
| 109 | $this->curlErrInfo = curl_error($curlSession); |
| 110 | curl_close($curlSession); |
| 111 | |
| 112 | if(CURLE_OK != $this->curlErrNum){ |
| 113 | error_log(sprintf("[%s] [CurlErr: %d] %s", __METHOD__, $this->curlErrNum, $this->curlErrInfo)); |
| 114 | return -1; |
| 115 | } |
| 116 | |
| 117 | return $downloadedData; |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * check if macroseismic scale study is supported |
| 122 | * |
| 123 | * @param string $macroseismicStudyName name of the macroseismic scale study |
| 124 | * |
| 125 | * @return bool TRUE if is supported, FALSE otherwise |
| 126 | */ |
| 127 | // private function isValidType(string $macroseismicStudyName):bool{ return preg_match("(mcs|ems)", $macroseismicStudyName); } |
| 128 | |
| 129 | } |
| 130 | ?> |