Completed
Pull Request — master (#142)
by Robbie
01:48
created

GitHubMarkdownService::getHeaders()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
3
use GuzzleHttp\Client;
4
use GuzzleHttp\Exception\ClientException;
5
6
/**
7
 * A GitHub service for communicating with the GitHub API to render Markdown. Uses Guzzle as the
8
 * transport method.
9
 *
10
 * @package mysite
11
 */
12
class GitHubMarkdownService extends Object
13
{
14
    /**
15
     * The Guzzle client
16
     *
17
     * @var GuzzleHttp\Client
18
     */
19
    protected $client;
20
21
    /**
22
     * The GitHub repository context
23
     *
24
     * @var string
25
     */
26
    protected $context;
27
28
    /**
29
     * GitHub API configuration
30
     * @var string
31
     */
32
    const API_BASE_URI        = 'https://api.github.com';
33
    const API_REQUEST_METHOD  = 'POST';
34
    const API_RENDER_ENDPOINT = '/markdown';
35
36
    /**
37
     * Use GitHub's API to render markdown to HTML
38
     *
39
     * @param  string $markdown Markdown
40
     * @return string           HTML
41
     */
42
    public function toHtml($markdown)
43
    {
44
        try {
45
            /** @var Psr\Http\Message\ResponseInterface $response */
46
            $response = $this->getClient()
47
                ->request(
48
                    $this->getRequestMethod(),
49
                    $this->getEndpoint(),
50
                    array(
51
                        'headers' => $this->getHeaders(),
52
                        'body' => $this->getPayload($markdown)
53
                    )
54
                );
55
        } catch (ClientException $ex) {
56
            user_error($ex->getMessage());
57
        }
58
59
        return (string) $response->getBody();
60
    }
61
62
    /**
63
     * Get the GitHub repository context
64
     * @return string
65
     */
66
    public function getContext()
67
    {
68
        return $this->context;
69
    }
70
71
    /**
72
     * Set the GitHub repository context
73
     * @param  string $context
74
     * @return $this
75
     */
76
    public function setContext($context)
77
    {
78
        $this->context = $context;
79
        return $this;
80
    }
81
82
    /**
83
     * Get an instance of a GuzzleHttp client
84
     * @return GuzzleHttp\Client
85
     */
86
    public function getClient()
87
    {
88
        if (is_null($this->client)) {
89
            $this->client = new Client(
90
                array(
91
                    'base_uri' => $this->getBaseUri()
92
                )
93
            );
94
        }
95
96
        return $this->client;
97
    }
98
99
    /**
100
     * Get the GitHub base URI
101
     * @return string
102
     */
103
    public function getBaseUri()
104
    {
105
        return self::API_BASE_URI;
106
    }
107
108
    /**
109
     * Get the HTTP request method to use for the request
110
     * @return string
111
     */
112
    public function getRequestMethod()
113
    {
114
        return self::API_REQUEST_METHOD;
115
    }
116
117
    /**
118
     * Get the markdown parse endpoint for GitHub's API
119
     * @return string
120
     */
121
    public function getEndpoint()
122
    {
123
        return self::API_RENDER_ENDPOINT;
124
    }
125
126
    /**
127
     * Get any custom headers to use for the request
128
     * @return array
129
     */
130
    public function getHeaders()
131
    {
132
        return array(
133
            'User-Agent'   => 'silverstripe/addons-site',
134
            'Content-Type' => 'text/x-markdown'
135
        );
136
    }
137
138
    /**
139
     * Get the payload for GFM mode parsing in the markdown API
140
     *
141
     * @see https://developer.github.com/v3/markdown/
142
     * @param  string $markdown
143
     * @return string           JSON
144
     */
145
    public function getPayload($markdown)
146
    {
147
        return Convert::raw2json(
148
            array(
149
                'text'    => $markdown,
150
                'mode'    => 'gfm',
151
                'context' => $this->getContext()
152
            )
153
        );
154
    }
155
}
156