Completed
Push — master ( 6c75f8...8616ea )
by Bobby
01:47
created

MetarHTTPClient::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Ballen\Metar\Helpers;
4
5
use GuzzleHttp\Client as HttpClient;
6
use \Exception;
7
8
/**
9
 * Metar
10
 *
11
 * Metar is a PHP library for retrieveing weather reports (METAR infomation),
12
 * the library supports multiple 'METAR prodivers' including NOAA and VATSIM.
13
 *
14
 * @author Bobby Allen <[email protected]>
15
 * @license http://www.gnu.org/licenses/gpl-3.0.html
16
 * @link https://github.com/bobsta63/metar
17
 * @link http://www.bobbyallen.me
18
 *
19
 */
20
class MetarHTTPClient
21
{
22
23
    /**
24
     * Optional Guzzle Client configuration.
25
     * @var array
26
     */
27
    protected $guzzle_conf;
28
29
    /**
30
     * HTTP Client
31
     * @param array $config Optional Guzzle configuration.
32
     */
33
    public function __construct($config = [])
34
    {
35
        $this->guzzle_conf = $config;
36
    }
37
38
    /**
39
     * Make a HTTP request and retrieve the body.
40
     * @param string $url The URL to request
41
     * @return string
42
     * @throws Exception
43
     */
44
    public function getMetarAPIResponse($url)
45
    {
46
        $client = new HttpClient($this->guzzle_conf);
47
        $response = $client->get($url);
48
        if ($response->getStatusCode() != 200) {
49
            throw new Exception('An error occured when attempting to access the remote webservice, please try again shortly!');
50
        }
51
        return $response->getBody();
52
    }
53
}
54