Completed
Push — master ( 5ed5e7...614fcd )
by Adriano
05:55
created

Client::getParameter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace FipeApi;
4
use FipeApi\Constants\FipeApiParameter;
5
6
/**
7
 * Class Client
8
 *
9
 * @package FipeApi
10
 */
11
class Client
12
{
13
14
    /**
15
     * URL padrão
16
     */
17
    const DEFAULT_URL = "http://fipeapi.appspot.com/api/1/";
18
19
    /**
20
     * Timeout padrão
21
     */
22
    const DEFAULT_TIMEOUT = 10;
23
24
    /**
25
     * Client constructor.
26
     *
27
     * @param array|null $params
28
     * @throws \Exception
29
     */
30
    public function __construct(array $params = null)
31
    {
32
33
        if(!is_null($params))
34
        {
35
            foreach ($params as $key => $param)
36
            {
37
                if(!in_array($key, array_keys($params)))
38
                    throw new \Exception(sprintf("Parâmetro %s inválido", $key));
39
            }
40
        }
41
42
        putenv(sprintf("%s=%s", FipeApiParameter::FIPE_API_HOST, Client::DEFAULT_URL));
43
        putenv(sprintf("%s=%s", FipeApiParameter::FIPE_API_TIMEOUT, Client::DEFAULT_TIMEOUT));
44
45
        if(!is_null($params) && is_array($params))
46
        {
47
            foreach ($params as $key => $value)
48
                putenv(sprintf("%s=%s",$key,$value));
49
        }
50
    }
51
52
    /**
53
     * @param FipeApiParameter $key
54
     * @param string $value
55
     */
56
    public function setParameter($key, $value)
57
    {
58
        putenv(sprintf("%s=%s",$key,$value));
59
    }
60
61
    /**
62
     * @param FipeApiParameter $key
63
     * @return string
64
     */
65
    public function getParameter($key)
66
    {
67
        return getenv($key);
68
    }
69
70
}