Passed
Push — master ( 63c7d9...6a69b3 )
by Jon
01:58
created

FlatPlan::testConnection()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 9
nc 4
nop 0
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
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
    private $endpoint = 'http://172.23.0.1/api/';
15
16
    private $client;
17
    private $accessToken;
18
    private $appId;
19
    private $statuses = array('failed', 'deferred', 'published');
20
21
    public function __construct($accessToken, $appId)
22
    {
23
        $this->client      = new Guzzle([
24
            'base_uri' => $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
        ];
39
    }
40
41
    public function testConnection()
42
    {
43
        try {
44
            $response = $this->client->get(
45
                'test',
46
                $this->getHeaders()
47
            );
48
            if ($response->getStatusCode() === 200) {
49
                return true;
50
            }
51
            return false;
52
53
        } catch (\GuzzleHttp\Exception\BadResponseException $e) {
54
            return false;
55
        }
56
    }
57
    
58
    /**
59
     * retrieve a list of articles from FlatPlan
60
     *
61
     * @return \stdClass
62
     */
63
    public function getArticles($status = null, $page = 1, $num = 10)
64
    {
65
        try {
66
            $options = $this->getHeaders();
67
            $options['form_params'] = [
68
                'page'         => (int) $page,
69
                'itemsPerPage' => (int) $num
70
            ];
71
            if (!is_null($status) && in_array($this->statuses, $status)) {
72
                $options['form_params']['status'] = $status;
73
            }
74
            $response = $this->client->get(
75
                'article',
76
                $options
77
            );
78
            $articles = json_decode((string) $response->getBody());
79
            return $articles;
80
81
        } catch (\GuzzleHttp\Exception\BadResponseException $e) {
82
            echo "Unable to retrieve access token.";
83
        }
84
    }
85
86
        public function getArticleStatus($articleId)
87
    {
88
        try {
89
            $response = $this->client->get(
90
                'article/status/' . $articleId,
91
                $this->getHeaders()
92
            );
93
            $status   = json_decode((string) $response->getBody());
94
            return $status;
95
96
        } catch (\GuzzleHttp\Exception\BadResponseException $e) {
97
            echo "Unable to retrieve access token.";
98
        }
99
    }
100
101
    public function publish(Article $article)
102
    {
103
        $json    = $article->getJson();
104
        $options = $this->getHeaders();
105
        $options['form_params'] = ['articleJson' => $json];
106
        try {
107
            $response = $this->client->post(
108
                'article',
109
                $options
110
            );
111
112
            $idArray = json_decode((string) $response->getBody());
113
            return $idArray;
114
115
        } catch (\GuzzleHttp\Exception\BadResponseException $e) {
116
            echo "Unable to retrieve access token.";
117
        }
118
    }
119
}