Code Coverage |
||||||||||||||||
Lines |
Branches |
Paths |
Functions and Methods |
Classes and Traits |
||||||||||||
| Total | |
83.33% |
35 / 42 |
|
76.19% |
16 / 21 |
|
33.33% |
6 / 18 |
|
33.33% |
2 / 6 |
CRAP | |
0.00% |
0 / 1 |
| SingleInstance | |
83.33% |
35 / 42 |
|
76.19% |
16 / 21 |
|
33.33% |
6 / 18 |
|
33.33% |
2 / 6 |
54.67 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| curlErrorNumber | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| curlErrorInfo | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getIterator | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| fetch | |
88.89% |
32 / 36 |
|
85.71% |
12 / 14 |
|
25.00% |
3 / 12 |
|
0.00% |
0 / 1 |
21.19 | |||
| filterEmptyDataLine | |
100.00% |
1 / 1 |
|
100.00% |
3 / 3 |
|
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | namespace Dbmi\Webservice\Analysis; |
| 3 | |
| 4 | use ArrayIterator; |
| 5 | use IteratorAggregate; |
| 6 | use Traversable; |
| 7 | |
| 8 | use Dbmi\Webservice\Contributor\Struct as DS_Contributor; |
| 9 | use Dbmi\Webservice\Quake\IdStruct as DS_QuakeId; |
| 10 | |
| 11 | use Fdsn\Webservices\Event\Structs\LatLon as DS_LatLon; |
| 12 | |
| 13 | /** |
| 14 | * Get analysis |
| 15 | */ |
| 16 | class SingleInstance implements IteratorAggregate{ |
| 17 | private const templateUrlDataByContributor = 'https://emidius.mi.ingv.it/services/macroseismic/query?format=textmacro&includemdps=true&eventid=%s&contributor=%s'; |
| 18 | private const templateUrlDataByContributorAndMdpId = 'https://emidius.mi.ingv.it/services/macroseismic/query?format=textmacro&includemdps=true&eventid=%s&contributor=%s&mdpsetid=%s'; |
| 19 | |
| 20 | private DS_Contributor $contributor; |
| 21 | private DS_QuakeId $quakeId; |
| 22 | |
| 23 | private array $localities = array(); |
| 24 | |
| 25 | private int $curlErrNum = 0; |
| 26 | private string $curlErrInfo = ''; |
| 27 | |
| 28 | public function __construct(DS_Contributor $contributor, DS_QuakeId $quakeId){ |
| 29 | $this->contributor = $contributor; |
| 30 | $this->quakeId = $quakeId; |
| 31 | } |
| 32 | |
| 33 | public function curlErrorNumber(){ return $this->curlErrNum; } |
| 34 | public function curlErrorInfo(){ return $this->curlErrInfo; } |
| 35 | |
| 36 | /** |
| 37 | * get all localities intensity |
| 38 | * |
| 39 | * @return array locality intensity |
| 40 | */ |
| 41 | public function getIterator():Traversable{ return new ArrayIterator($this->localities); } |
| 42 | |
| 43 | /** |
| 44 | * find contributor based on quakeId |
| 45 | * |
| 46 | * @return int number of studies found |
| 47 | */ |
| 48 | public function fetch():int{ |
| 49 | $url = ( ! is_null ($this->contributor->mdpSetId()) ) |
| 50 | ? sprintf(self::templateUrlDataByContributorAndMdpId, $this->quakeId->id(), $this->contributor->name(), $this->contributor->mdpSetId() ) |
| 51 | : sprintf(self::templateUrlDataByContributor, $this->quakeId->id(), $this->contributor->name() ); |
| 52 | |
| 53 | $curlSession = curl_init(); |
| 54 | curl_setopt_array($curlSession, array( |
| 55 | CURLOPT_URL => $url, |
| 56 | CURLOPT_HEADER => false, |
| 57 | CURLOPT_CUSTOMREQUEST => 'GET', |
| 58 | CURLOPT_RETURNTRANSFER => true, |
| 59 | CURLOPT_FAILONERROR => true |
| 60 | ) |
| 61 | ); |
| 62 | $downloadedData = curl_exec($curlSession); |
| 63 | |
| 64 | $this->curlErrNum = curl_errno($curlSession); |
| 65 | $this->curlErrInfo = curl_error($curlSession); |
| 66 | curl_close($curlSession); |
| 67 | |
| 68 | if(CURLE_OK != $this->curlErrNum){ |
| 69 | error_log(sprintf("[%s] [CurlErr: %d] %s", __METHOD__, $this->curlErrNum, $this->curlErrInfo)); |
| 70 | return -1; |
| 71 | } |
| 72 | |
| 73 | $stringsData = preg_split("/\n/", $downloadedData); |
| 74 | $stringsArray = array_filter($stringsData, array($this, 'filterEmptyDataLine')); |
| 75 | |
| 76 | if(0 == count($stringsArray)){ |
| 77 | error_log(sprintf("[%s] No data\n'%s'", __METHOD__, $stringsData)); |
| 78 | return 0; |
| 79 | } |
| 80 | |
| 81 | foreach($stringsArray as $data){ |
| 82 | $dataArray = str_getcsv($data, '|', "\"", "\\"); |
| 83 | |
| 84 | try{ |
| 85 | $this->localities[] = new Locality( |
| 86 | $dataArray[8], |
| 87 | $dataArray[9], |
| 88 | new DS_LatLon($dataArray[10], $dataArray[11]), |
| 89 | new Intensity($dataArray[12]) |
| 90 | ); |
| 91 | } |
| 92 | catch(\TypeError $e){ |
| 93 | error_log(sprintf( "[%s] Skipping invalid data with error '%s'", __METHOD__, $e->getMessage() )); |
| 94 | continue; |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | return count($this->localities); |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * filter empty lines or comment lines (starting with #) |
| 103 | */ |
| 104 | private function filterEmptyDataLine($data){ |
| 105 | return !(preg_match('/^$/', $data) || preg_match("/^#/", $data)); |
| 106 | } |
| 107 | |
| 108 | } |
| 109 | |
| 110 | ?> |
| 111 |