Code Coverage
 
Lines
Branches
Paths
Functions and Methods
Classes and Traits
Total
96.15% covered (success)
96.15%
25 / 26
66.67% covered (warning)
66.67%
14 / 21
46.67% covered (danger)
46.67%
7 / 15
14.29% covered (danger)
14.29%
1 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
Webservice
96.15% covered (success)
96.15%
25 / 26
66.67% covered (warning)
66.67%
14 / 21
46.67% covered (danger)
46.67%
7 / 15
85.71% covered (warning)
85.71%
6 / 7
43.73
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
5 / 5
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
 fetch
n/a
0 / 0
n/a
0 / 0
n/a
0 / 0
n/a
0 / 0
0
 numRows
100.00% covered (success)
100.00%
1 / 1
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
 version
100.00% covered (success)
100.00%
1 / 1
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
 getIterator
100.00% covered (success)
100.00%
1 / 1
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
 getFetch
92.86% covered (success)
92.86%
13 / 14
60.00% covered (warning)
60.00%
3 / 5
25.00% covered (danger)
25.00%
1 / 4
0.00% covered (danger)
0.00%
0 / 1
6.80
 fdsnSupportedVersion
100.00% covered (success)
100.00%
3 / 3
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
 isCurlCloseSupported
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2namespace Fdsn\Webservices;
3
4use ArrayIterator;
5use IteratorAggregate;
6use 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 */
13abstract 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    /**
35     * 
36     *
37     */
38    private const curlCloceLatestPhpVersion = 7;
39
40    protected string $fdsnServer;
41    protected string $webserviceFullPath;
42
43    protected array $arrayOfResults = array();
44    protected string $version = '';
45
46    function __construct( string $fdsn_server = self::defaultFdsnServer ){
47        if( self::debug ) printf("[%s] %d \n", __METHOD__, time()); 
48
49        $this->fdsnServer =  $fdsn_server;
50
51        $this->webserviceFullPath = sprintf(self::basePath,
52            $this->fdsnServer
53            );
54    }
55
56    /**
57     * Fetch data from selected FDSN server
58     * 
59     * @return int number of data found
60     */
61    abstract public function fetch():int;
62
63    
64    /**
65     * Return the number of elems found
66     * @return int         The number of elems found
67     */
68    public function numRows():int { if( self::debug ) printf("[%s] %d \n", __METHOD__, time()); return count($this->arrayOfResults); }
69
70
71    /**
72     * Get FDSN running version
73     * @return string FDSN running version (trimmed by newline)
74     */
75    public function version():string{ if( self::debug ) printf("[%s] %d \n", __METHOD__, time()); return $this->version; }
76
77    /**
78     * Iterate over catalogis found
79     * @return array     array of \Fdsn\DataStructure\Catalog obj
80     */
81    public function getIterator():Traversable { if( self::debug ) printf("[%s] %d \n", __METHOD__, time()); return new ArrayIterator($this->arrayOfResults); }
82
83    /**
84     * Fetch data from selected FDSN server
85     * 
86     * @return string downloaded string
87     */
88    protected function getFetch(string $url):string{
89        if( self::debug ) printf("[%s] %d \n", __METHOD__, time()); 
90
91        $curlSession = curl_init();
92        curl_setopt_array($curlSession, array(
93            CURLOPT_URL         => $url,
94            CURLOPT_HEADER         => false,
95            CURLOPT_CUSTOMREQUEST    => 'GET',
96            CURLOPT_RETURNTRANSFER    => 1,
97            CURLOPT_TIMEOUT        => 60
98            )
99        );
100        $response = trim(curl_exec($curlSession));
101        if ( $this->isCurlCloseSupported() )
102            curl_close($curlSession);
103
104        return $response;
105    }
106
107    /**
108     * Check if FDSN server version is supported
109     */
110    protected function fdsnSupportedVersion():bool{
111        if( self::debug ) printf("[%s] %d \n", __METHOD__, time()); 
112
113        $this->version = trim( $this->getFetch($this->webserviceFullPath . '/version') );
114
115                return preg_match('/' . self::fdsnSupportedVersion . '/', $this->version);
116
117    }
118
119    /**
120     * Check if curl_close is still supported
121     * @return bool true is supported, false otherwise
122     */
123    private function isCurlCloseSupported():bool{ return PHP_MAJOR_VERSION <= self::curlCloceLatestPhpVersion; }
124
125}

Branches

Below are the source code lines that represent each code branch as identified by Xdebug. Please note a branch is not necessarily coterminous with a line, a line may contain multiple branches and therefore show up more than once. Please also be aware that some branches may be implicit rather than explicit, e.g. an if statement always has an else as part of its logical flow even if you didn't write one.

Webservice->__construct
46    function __construct( string $fdsn_server = self::defaultFdsnServer ){
47        if( self::debug ) printf("[%s] %d \n", __METHOD__, time()); 
47        if( self::debug ) printf("[%s] %d \n", __METHOD__, time()); 
48
49        $this->fdsnServer =  $fdsn_server;
49        $this->fdsnServer =  $fdsn_server;
50
51        $this->webserviceFullPath = sprintf(self::basePath,
52            $this->fdsnServer
53            );
54    }
Webservice->fdsnSupportedVersion
111        if( self::debug ) printf("[%s] %d \n", __METHOD__, time()); 
111        if( self::debug ) printf("[%s] %d \n", __METHOD__, time()); 
112
113        $this->version = trim( $this->getFetch($this->webserviceFullPath . '/version') );
113        $this->version = trim( $this->getFetch($this->webserviceFullPath . '/version') );
114
115                return preg_match('/' . self::fdsnSupportedVersion . '/', $this->version);
116
117    }
Webservice->getFetch
88    protected function getFetch(string $url):string{
89        if( self::debug ) printf("[%s] %d \n", __METHOD__, time()); 
89        if( self::debug ) printf("[%s] %d \n", __METHOD__, time()); 
90
91        $curlSession = curl_init();
91        $curlSession = curl_init();
92        curl_setopt_array($curlSession, array(
93            CURLOPT_URL         => $url,
94            CURLOPT_HEADER         => false,
95            CURLOPT_CUSTOMREQUEST    => 'GET',
96            CURLOPT_RETURNTRANSFER    => 1,
97            CURLOPT_TIMEOUT        => 60
98            )
99        );
100        $response = trim(curl_exec($curlSession));
101        if ( $this->isCurlCloseSupported() )
102            curl_close($curlSession);
103
104        return $response;
104        return $response;
105    }
Webservice->getIterator
81    public function getIterator():Traversable { if( self::debug ) printf("[%s] %d \n", __METHOD__, time()); return new ArrayIterator($this->arrayOfResults); }
81    public function getIterator():Traversable { if( self::debug ) printf("[%s] %d \n", __METHOD__, time()); return new ArrayIterator($this->arrayOfResults); }
81    public function getIterator():Traversable { if( self::debug ) printf("[%s] %d \n", __METHOD__, time()); return new ArrayIterator($this->arrayOfResults); }
Webservice->isCurlCloseSupported
123    private function isCurlCloseSupported():bool{ return PHP_MAJOR_VERSION <= self::curlCloceLatestPhpVersion; }
Webservice->numRows
68    public function numRows():int { if( self::debug ) printf("[%s] %d \n", __METHOD__, time()); return count($this->arrayOfResults); }
68    public function numRows():int { if( self::debug ) printf("[%s] %d \n", __METHOD__, time()); return count($this->arrayOfResults); }
68    public function numRows():int { if( self::debug ) printf("[%s] %d \n", __METHOD__, time()); return count($this->arrayOfResults); }
Webservice->version
75    public function version():string{ if( self::debug ) printf("[%s] %d \n", __METHOD__, time()); return $this->version; }
75    public function version():string{ if( self::debug ) printf("[%s] %d \n", __METHOD__, time()); return $this->version; }
75    public function version():string{ if( self::debug ) printf("[%s] %d \n", __METHOD__, time()); return $this->version; }