FlatPlan::testConnection()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 9
c 1
b 0
f 0
nc 4
nop 0
dl 0
loc 14
rs 9.9666
1
<?php namespace FlatPlan;
2
3
use GuzzleHttp\Client as Guzzle;
4
5
/**
6
*  This class is the central point to the SDK
7
*  All communication between the publisher system and the hub will happen through here
8
*
9
*  @author Jon Bowes <[email protected]>
10
*/
11
class FlatPlan {
12
13
    private $endpoint        = 'https://staging.flatplan.app/api/';
14
15
    private $client;
16
    private $accessToken;
17
    private $appId;
18
    private $statuses        = array('deleted', 'draft', 'published');
19
    private $allowedStatuses = array('delete', 'draft', 'publish');
20
21
    public function __construct($accessToken, $appId, $endpoint = null)
22
    {
23
        $this->client      = new Guzzle([
24
            'base_uri' => (!is_null($endpoint) ? $endpoint : $this->endpoint)
25
        ]);
26
        $this->accessToken = $accessToken;
27
        $this->appId       = $appId;
28
    }
29
30
    private function getHeaders()
31
    {
32
        return [
33
            'headers' => [
34
                'Authorization' => 'Bearer ' . $this->accessToken,
35
                'Accept'        => 'application/json',
36
                'X-Application' => $this->appId,
37
            ],
38
            'http_errors' => false
39
        ];
40
    }
41
42
    public function testConnection()
43
    {
44
        try {
45
            $response = $this->client->get(
46
                'test',
47
                $this->getHeaders()
48
            );
49
            if ($response->getStatusCode() === 200) {
50
                return true;
51
            }
52
            return false;
53
54
        } catch (\GuzzleHttp\Exception\BadResponseException $e) {
55
            return false;
56
        }
57
    }
58
    
59
    /**
60
     * retrieve a list of articles from FlatPlan
61
     *
62
     * @return \stdClass
63
     */
64
    public function getArticles($status = null, $page = 1, $num = 10)
65
    {
66
        try {
67
            $options = $this->getHeaders();
68
            $options['form_params'] = [
69
                'page'         => (int) $page,
70
                'itemsPerPage' => (int) $num
71
            ];
72
            if (!is_null($status) && in_array($this->statuses, $status)) {
73
                $options['form_params']['status'] = $status;
74
            }
75
            $response = $this->client->get(
76
                'article',
77
                $options
78
            );
79
            $articles = json_decode((string) $response->getBody());
80
            return $articles;
81
82
        } catch (\GuzzleHttp\Exception\BadResponseException $e) {
83
            echo $e->getMessage();
84
        }
85
    }
86
87
    public function getArticleStatus($articleId)
88
    {
89
        try {
90
            $response = $this->client->get(
91
                'article/status/' . $articleId,
92
                $this->getHeaders()
93
            );
94
            $status   = json_decode((string) $response->getBody());
95
            return $status;
96
97
        } catch (\GuzzleHttp\Exception\BadResponseException $e) {
98
            echo $e->getMessage();
99
        }
100
    }
101
102
    public function publish(Article $article)
103
    {
104
        $json    = $article->getJson();
105
        $options = $this->getHeaders();
106
        $options['form_params'] = ['articleJson' => $json];
107
        try {
108
            $response = $this->client->post(
109
                'article',
110
                $options
111
            );
112
113
            $idArray = json_decode((string) $response->getBody());
114
            return $idArray;
115
116
        } catch (\GuzzleHttp\Exception\BadResponseException $e) {
117
            echo $e->getMessage();
118
        }
119
    }
120
121
    public function update($articleId, $status)
122
    {
123
        if (!in_array($status, $this->allowedStatuses)) {
124
            throw new \ErrorException('Invalid status passed');
125
        }
126
        $options = $this->getHeaders();
127
        try {
128
            $response = $this->client->get(
129
                'article/' . $articleId . '/' . $status,
130
                $options
131
            );
132
133
            $response = json_decode((string) $response->getBody());
134
            return $response;
135
136
        } catch (\GuzzleHttp\Exception\BadResponseException $e) {
137
            echo $e->getMessage();
138
        }
139
    }
140
}