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