Client::createArticle()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 7
rs 10
1
<?php
2
3
namespace TenderSEO;
4
5
class Client
6
{
7
    const baseUrl = 'https://www.tenderseo.com/api/';
8
9
    private $key;
10
    private $isTest = false;
11
12
    public function __construct($params = [])
13
    {
14
        if (isset($params['test'])) {
15
            $this->isTest = $params['test'];
16
        }
17
18
        if (isset($params['key'])) {
19
            $this->key = $params['key'];
20
        }
21
    }
22
23
    public function setApiKey($key)
24
    {
25
        $this->key = $key;
26
    }
27
28
    public function status()
29
    {
30
        return $this->_request('status');
31
    }
32
33
    public function signup($options)
34
    {
35
        return $this->_request('signup', $options, 'post');
36
    }
37
38
    public function createArticle($options)
39
    {
40
        if (!$this->key) {
41
            throw new \Exception('You forgot to provide api key');
42
        }
43
44
        return $this->_request('order/create', $options, 'post');
45
    }
46
47
    public function order($options)
48
    {
49
        if (!$this->key) {
50
            throw new \Exception('You forgot to provide api key');
51
        }
52
53
        return $this->_request('order', $options);
54
    }
55
56
    public function randomImage($options)
57
    {
58
        return $this->_request('image/random', $options);
59
    }
60
61
    public function review($options)
62
    {
63
        return $this->_request('review', $options);
64
    }
65
66
    public function randomArticle($options=null)
67
    {
68
        $url = 'article/random';
69
70
        if ($options && isset($options['language'])) {
71
            $url .= '?language='.$options['language'];
72
        }
73
74
        return $this->_request($url);
75
    }
76
77
    public function article($uuid)
78
    {
79
        if (!$this->key) {
80
            throw new \Exception('You forgot to provide api key');
81
        }
82
83
        $options = [
84
            'uuid' => $uuid,
85
        ];
86
87
        return $this->_request('article', $options);
88
    }
89
90
    public function articles($options = null)
91
    {
92
        if (!$this->key) {
93
            throw new \Exception('You forgot to provide api key');
94
        }
95
96
        $options = ($options == null) ? [] : $options;
97
98
        if (isset($options['from']) && $options['from'] instanceOf \DateTime) {
99
            $options['from'] = $options['from']->format('Y-m-d');
100
        }
101
102
        $url = 'articles';
103
104
        return $this->_request($url, $options);
105
    }
106
107
    private function _request($suffix, $options = [], $method = 'get')
108
    {
109
        if ($this->key) {
110
            $options['key'] = $this->key;
111
        }
112
113
        if ($this->isTest) {
114
            $url = 'http://localhost:8000/api/'.$suffix;
115
        } else {
116
            $url = self::baseUrl.$suffix;
117
        }
118
119
        try {
120
            $client = new \GuzzleHttp\Client();
121
            $response = $client->request($method, $url, [
122
                'query' => $options,
123
            ]);
124
125
        } catch (\GuzzleHttp\Exception\ClientException $e) {
126
            $res = new \StdClass();
127
            $res->error = true;
128
            $res->error_message = 'Unknown error, could not reach server, endpoint: '.$url;
129
130
            return $res;
131
132
        } catch (\GuzzleHttp\Exception\ServerException $e) {
133
            $res = new \StdClass();
134
            $res->error = true;
135
            $res->error_message = 'Unknown error, server reported a problem, check your parameters again, endpoint: '.$url;
136
137
            return $res;
138
        }
139
140
        return json_decode($response->getBody()->getContents());
141
    }
142
}
143