Code Coverage |
||||||||||||||||
Lines |
Branches |
Paths |
Functions and Methods |
Classes and Traits |
||||||||||||
Total | |
100.00% |
32 / 32 |
|
100.00% |
19 / 19 |
|
36.84% |
7 / 19 |
|
100.00% |
4 / 4 |
CRAP | |
100.00% |
1 / 1 |
Event | |
100.00% |
32 / 32 |
|
100.00% |
19 / 19 |
|
36.84% |
7 / 19 |
|
100.00% |
4 / 4 |
41.48 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
7 / 7 |
|
100.00% |
5 / 5 |
|
75.00% |
3 / 4 |
|
100.00% |
1 / 1 |
3.14 | |||
fetch | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
formatIsValid | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
parse | |
100.00% |
20 / 20 |
|
100.00% |
12 / 12 |
|
15.38% |
2 / 13 |
|
100.00% |
1 / 1 |
27.81 |
1 | <?php |
2 | namespace Fdsn\Webservices\Event; |
3 | |
4 | use ArrayIterator; |
5 | use IteratorAggregate; |
6 | use Traversable; |
7 | |
8 | use Fdsn\Webservices\Webservice as Fdsnws_Webservice; |
9 | |
10 | use Fdsn\Webservices\Event\Structs\Author as DS_Author; |
11 | use Fdsn\Webservices\Event\Structs\DateTimeRange as DS_DateTimeRange; |
12 | use Fdsn\Webservices\Event\Structs\Depth as DS_Depth; |
13 | use Fdsn\Webservices\Event\Structs\DepthRange as DS_DepthRange; |
14 | use Fdsn\Webservices\Event\Structs\Epicenter as DS_Epicenter; |
15 | use Fdsn\Webservices\Event\Structs\EventType as DS_EventType; |
16 | use Fdsn\Webservices\Event\Structs\Id as DS_Id; |
17 | use Fdsn\Webservices\Event\Structs\LatLon as DS_LatLon; |
18 | use Fdsn\Webservices\Event\Structs\LatLonRange as DS_LatLonRange; |
19 | use Fdsn\Webservices\Event\Structs\Location as DS_Location; |
20 | use Fdsn\Webservices\Event\Structs\Magnitude as DS_Magnitude; |
21 | use Fdsn\Webservices\Event\Structs\MagnitudeRange as DS_MagnitudeRange; |
22 | use Fdsn\Webservices\Event\Structs\Quake as DS_Quake; |
23 | use Fdsn\Webservices\Event\Structs\Radius as DS_Radius; |
24 | use Fdsn\Webservices\Event\Structs\RadiusRange as DS_RadiusRange; |
25 | |
26 | /** |
27 | * PHP library to access FDSN Webservices and request Event (earthquake) information in text format |
28 | * @see https://www.fdsn.org/webservices/fdsnws-event-1.2.pdf FDSN official documentation |
29 | * |
30 | * @param string $format Accepted output kind format |
31 | * @param string $user Set your app name |
32 | * @param string $fdns_server Fdns webservice domain name (default: webservices.ms.ingv.it) |
33 | */ |
34 | class Event extends Fdsnws_Webservice implements IteratorAggregate { |
35 | private string $format = 'text'; |
36 | private string $user; |
37 | |
38 | protected string $url; |
39 | protected string $eventList; |
40 | protected int $counter = 0; |
41 | protected array $parameters = array(); |
42 | |
43 | function __construct( string $format, ?string $user, string $fdns_server = Fdsnws_Webservice::defaultFdsnServer){ |
44 | |
45 | if( $this->formatIsValid($format) ) |
46 | $this->format = $format; //otherwise it has default value |
47 | |
48 | $this->user = $user; |
49 | |
50 | $this->parameters['format'] = sprintf("format=%s", $this->format); |
51 | if ( ! empty($user) ) //not implemented/accepted on every service |
52 | $this->parameters['user'] = sprintf("user=%s", $this->user); |
53 | |
54 | |
55 | parent::__construct($fdns_server); |
56 | } |
57 | |
58 | /** |
59 | * Apply all filter and DO the request to the Official ONT Webservice |
60 | * @return int Return the number of elems found |
61 | */ |
62 | public function fetch():int{ |
63 | $this->url = sprintf('%s/query?%s', $this->webserviceFullPath, implode('&', $this->parameters)); |
64 | $response = $this->getFetch( $this->url ); |
65 | |
66 | $this->parse($response); |
67 | |
68 | return $this->counter; |
69 | } |
70 | |
71 | /** |
72 | * Check if format set is valid |
73 | * @return bool format is valid? |
74 | */ |
75 | protected function formatIsValid(string $format):bool{ |
76 | return preg_match('/text/', $format); |
77 | |
78 | //TODO: 2024-08-13: xml format is still not supported, but i'll fix |
79 | //return preg_match('/(text|xml)/', $format); |
80 | } |
81 | |
82 | // /** |
83 | // * Compose URL before do the request |
84 | // * |
85 | // * @return string composed URL |
86 | // */ |
87 | // protected function composeURL():string{ |
88 | // return sprintf('%s/query?%s', $this->webserviceFullPath, implode('&', $this->parameters)); |
89 | // } |
90 | |
91 | /** |
92 | * Loop over found elems found (in text format) and convert the to \Fdsn\DataStructure\Quake obj |
93 | */ |
94 | protected function parse(string $response):int { |
95 | date_default_timezone_set('UTC'); |
96 | |
97 | if( preg_match('/^Error/', $response) ) |
98 | throw new \RuntimeException( sprintf("Error fetching url: %s\nError reported is:\n%s", $this->url, $response) ); |
99 | |
100 | $events = preg_split('/\R/', $response); |
101 | |
102 | foreach($events as $details){ |
103 | if(empty($details) || '#' == $details[0]){ |
104 | //error_log('[INF] no data in this event line'); |
105 | continue; |
106 | } |
107 | |
108 | list($adsIdEvent, $utcTime, $lat, $lon, $depth, $author, $catalog, $contributor, $contributorID, $magPrefType, $magPrefValue, $magAuthor, $locationName, $eventType) = preg_split('/\|/', $details); |
109 | $this->arrayOfResults[] = new DS_Quake( |
110 | new DS_Id($adsIdEvent), |
111 | new \DateTime($utcTime), |
112 | new DS_Location($locationName), |
113 | new DS_Magnitude($magPrefType, $magPrefValue), |
114 | new DS_Epicenter(new DS_LatLon($lat, $lon), new DS_Depth($depth)), |
115 | new DS_Author($author), |
116 | new DS_EventType($eventType) |
117 | ); |
118 | } |
119 | |
120 | if( 1 == count($this->arrayOfResults) ) |
121 | $this->event = $this->arrayOfResults[0]; |
122 | |
123 | return $this->numRows(); |
124 | } |
125 | } |