Cloudflare::setGuzzle()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
/**
3
 * @author Chris Hilsdon <[email protected]>
4
 */
5
namespace Cloudflare;
6
7
use Symfony\Component\Yaml\Yaml;
8
use GuzzleHttp\Client;
9
10
class Cloudflare
11
{
12
    protected $Endpoint = "https://api.cloudflare.com/client/v4/";
13
    protected $APIKey;
14
    protected $Email;
15
    protected $MakeRequests = true;
16
    protected $Guzzle;
17
18
    public function setGuzzle($Guzzle)
19
    {
20
        $this->Guzzle = $Guzzle;
21
    }
22
23
    public function getEndpoint()
24
    {
25
        return $this->Endpoint;
26
    }
27
28
    public function getAPIKey()
29
    {
30
        return $this->APIKey;
31
    }
32
33
    public function getEmail()
34
    {
35
        return $this->Email;
36
    }
37
38
    public function getMakeRequests()
39
    {
40
        return $this->MakeRequests;
41
    }
42
43
    public function getGuzzle()
44
    {
45
        //If Guzzle has not been setup yet
46
        if($this->Guzzle === null) {
47
            $Guzzle = new Client();
48
            $this->setGuzzle($Guzzle);
49
        }
50
51
        return $this->Guzzle;
52
    }
53
54
    public function diableRequests()
55
    {
56
        $this->MakeRequests = false;
57
    }
58
59
    public function enableRequests()
60
    {
61
        $this->MakeRequests = true;
62
    }
63
64
    public function __construct($options = null, $Guzzle = null)
65
    {
66
        if (is_null($options) || !is_array($options)) {
67
            $this->loadConfig();
68
        } else {
69
            $this->APIKey = $options['APIKey'];
70
            $this->Email = $options['Email'];
71
        }
72
73
        if (!is_null($Guzzle)) {
74
            $this->setGuzzle($Guzzle);
75
        }
76
77
    }
78
79
    private function loadConfig()
80
    {
81
        $config = 'Cloudflare.yml';
82
        $configDist = $config.'.dist';
83
        $dir = $_SERVER['DOCUMENT_ROOT'];
84
85
        if (@file_exists($dir . $config)) {
86
            $load = $config;
87
        } elseif (@file_exists($dir . $configDist)) {
88
            $load = $configDist;
89
        } else {
90
            die('Unable to load either "'.$config.'" or "'.$configDist.'"');
91
        }
92
93
        $config = Yaml::parse(file_get_contents($load));
94
95
        $this->APIKey = $config['APIKey'];
96
        $this->Email = $config['Email'];
97
    }
98
}
99