Code Coverage |
||||||||||||||||
Lines |
Branches |
Paths |
Functions and Methods |
Classes and Traits |
||||||||||||
| Total | |
100.00% |
24 / 24 |
|
66.67% |
12 / 18 |
|
50.00% |
6 / 12 |
|
0.00% |
0 / 6 |
CRAP | |
0.00% |
0 / 1 |
| Webservice | |
100.00% |
24 / 24 |
|
66.67% |
12 / 18 |
|
50.00% |
6 / 12 |
|
100.00% |
6 / 6 |
30.00 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
5 / 5 |
|
66.67% |
2 / 3 |
|
50.00% |
1 / 2 |
|
100.00% |
1 / 1 |
2.50 | |||
| fetch | n/a |
0 / 0 |
n/a |
0 / 0 |
n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||||
| numRows | |
100.00% |
1 / 1 |
|
66.67% |
2 / 3 |
|
50.00% |
1 / 2 |
|
100.00% |
1 / 1 |
2.50 | |||
| version | |
100.00% |
1 / 1 |
|
66.67% |
2 / 3 |
|
50.00% |
1 / 2 |
|
100.00% |
1 / 1 |
2.50 | |||
| getIterator | |
100.00% |
1 / 1 |
|
66.67% |
2 / 3 |
|
50.00% |
1 / 2 |
|
100.00% |
1 / 1 |
2.50 | |||
| getFetch | |
100.00% |
13 / 13 |
|
66.67% |
2 / 3 |
|
50.00% |
1 / 2 |
|
100.00% |
1 / 1 |
2.50 | |||
| fdsnSupportedVersion | |
100.00% |
3 / 3 |
|
66.67% |
2 / 3 |
|
50.00% |
1 / 2 |
|
100.00% |
1 / 1 |
2.50 | |||
| 1 | <?php |
| 2 | namespace Fdsn\Webservices; |
| 3 | |
| 4 | use ArrayIterator; |
| 5 | use IteratorAggregate; |
| 6 | use Traversable; |
| 7 | |
| 8 | /** |
| 9 | * PHP library to access FDSN Webservices and request Catalogs |
| 10 | * |
| 11 | * @param string $fdsn_server Fdns webservice domain name (default: webservices.ms.ingv.it) |
| 12 | */ |
| 13 | abstract class Webservice implements IteratorAggregate { |
| 14 | /** |
| 15 | * @var Base URL to compile with FDSN server url |
| 16 | */ |
| 17 | public const basePath = 'https://%s/fdsnws/event/1'; |
| 18 | |
| 19 | /** |
| 20 | * @var Default server url |
| 21 | */ |
| 22 | public const defaultFdsnServer = 'webservices.ms.ingv.it'; |
| 23 | |
| 24 | /** |
| 25 | * @var Max supported version |
| 26 | */ |
| 27 | public const fdsnSupportedVersion = '^1.'; |
| 28 | |
| 29 | /** |
| 30 | * @var Debug print access to each "[method] unixtimestamp" |
| 31 | */ |
| 32 | public const debug = false; |
| 33 | |
| 34 | protected string $fdsnServer; |
| 35 | protected string $webserviceFullPath; |
| 36 | |
| 37 | protected array $arrayOfResults = array(); |
| 38 | protected string $version = ''; |
| 39 | |
| 40 | function __construct( string $fdsn_server = self::defaultFdsnServer ){ |
| 41 | if( self::debug ) printf("[%s] %d \n", __METHOD__, time()); |
| 42 | |
| 43 | $this->fdsnServer = $fdsn_server; |
| 44 | |
| 45 | $this->webserviceFullPath = sprintf(self::basePath, |
| 46 | $this->fdsnServer |
| 47 | ); |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Fetch data from selected FDSN server |
| 52 | * |
| 53 | * @return int number of data found |
| 54 | */ |
| 55 | abstract public function fetch():int; |
| 56 | |
| 57 | |
| 58 | /** |
| 59 | * Return the number of elems found |
| 60 | * @return int The number of elems found |
| 61 | */ |
| 62 | public function numRows():int { if( self::debug ) printf("[%s] %d \n", __METHOD__, time()); return count($this->arrayOfResults); } |
| 63 | |
| 64 | |
| 65 | /** |
| 66 | * Get FDSN running version |
| 67 | * @return string FDSN running version (trimmed by newline) |
| 68 | */ |
| 69 | public function version():string{ if( self::debug ) printf("[%s] %d \n", __METHOD__, time()); return $this->version; } |
| 70 | |
| 71 | /** |
| 72 | * Iterate over catalogis found |
| 73 | * @return array array of \Fdsn\DataStructure\Catalog obj |
| 74 | */ |
| 75 | public function getIterator():Traversable { if( self::debug ) printf("[%s] %d \n", __METHOD__, time()); return new ArrayIterator($this->arrayOfResults); } |
| 76 | |
| 77 | /** |
| 78 | * Fetch data from selected FDSN server |
| 79 | * |
| 80 | * @return string downloaded string |
| 81 | */ |
| 82 | protected function getFetch(string $url):string{ |
| 83 | if( self::debug ) printf("[%s] %d \n", __METHOD__, time()); |
| 84 | |
| 85 | $curlSession = curl_init(); |
| 86 | curl_setopt_array($curlSession, array( |
| 87 | CURLOPT_URL => $url, |
| 88 | CURLOPT_HEADER => false, |
| 89 | CURLOPT_CUSTOMREQUEST => 'GET', |
| 90 | CURLOPT_RETURNTRANSFER => 1, |
| 91 | CURLOPT_TIMEOUT => 60 |
| 92 | ) |
| 93 | ); |
| 94 | $response = trim(curl_exec($curlSession)); |
| 95 | curl_close($curlSession); |
| 96 | |
| 97 | return $response; |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Check if FDSN server version is supported |
| 102 | */ |
| 103 | protected function fdsnSupportedVersion():bool{ |
| 104 | if( self::debug ) printf("[%s] %d \n", __METHOD__, time()); |
| 105 | |
| 106 | $this->version = trim( $this->getFetch($this->webserviceFullPath . '/version') ); |
| 107 | |
| 108 | return preg_match('/' . self::fdsnSupportedVersion . '/', $this->version); |
| 109 | |
| 110 | } |
| 111 | |
| 112 | } |