GitHubMarkdownService::getHeaders()   A
last analyzed

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\RequestException;
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
        $body = '';
45
        try {
46
            /** @var Psr\Http\Message\RequestInterface $request */
47
            $request = $this->getClient()
48
                ->request(
49
                    $this->getRequestMethod(),
50
                    $this->getEndpoint(),
51
                    [
52
                        'headers' => $this->getHeaders(),
53
                        'body' => $this->getPayload($markdown)
54
                    ]
55
                );
56
            $body = (string) $request->getBody();
57
        } catch (RequestException $ex) {
58
            user_error($ex->getMessage());
59
            return '';
60
        }
61
62
        return $body;
63
    }
64
65
    /**
66
     * Get the GitHub repository context
67
     * @return string
68
     */
69
    public function getContext()
70
    {
71
        return $this->context;
72
    }
73
74
    /**
75
     * Set the GitHub repository context
76
     * @param  string $context
77
     * @return $this
78
     */
79
    public function setContext($context)
80
    {
81
        $this->context = $context;
82
        return $this;
83
    }
84
85
    /**
86
     * Get an instance of a GuzzleHttp client
87
     * @return GuzzleHttp\Client
88
     */
89
    public function getClient()
90
    {
91
        if (is_null($this->client)) {
92
            $this->client = new Client(['base_uri' => $this->getBaseUri()]);
93
        }
94
95
        return $this->client;
96
    }
97
98
    /**
99
     * Get the GitHub base URI
100
     * @return string
101
     */
102
    public function getBaseUri()
103
    {
104
        return self::API_BASE_URI;
105
    }
106
107
    /**
108
     * Get the HTTP request method to use for the request
109
     * @return string
110
     */
111
    public function getRequestMethod()
112
    {
113
        return self::API_REQUEST_METHOD;
114
    }
115
116
    /**
117
     * Get the markdown parse endpoint for GitHub's API
118
     * @return string
119
     */
120
    public function getEndpoint()
121
    {
122
        $endpoint = self::API_RENDER_ENDPOINT;
123
        if (!$this->getContext()) {
124
            $endpoint .= '/raw';
125
        }
126
        if (defined('SS_GITHUB_CLIENT_ID') && defined('SS_GITHUB_CLIENT_SECRET')) {
127
            $endpoint .= sprintf('?client_id=%s&client_secret=%s', SS_GITHUB_CLIENT_ID, SS_GITHUB_CLIENT_SECRET);
128
        }
129
        return $endpoint;
130
    }
131
132
    /**
133
     * Get any custom headers to use for the request
134
     * @return array
135
     */
136
    public function getHeaders()
137
    {
138
        return array(
139
            'User-Agent'   => 'silverstripe/addons-site',
140
            'Content-Type' => 'text/x-markdown'
141
        );
142
    }
143
144
    /**
145
     * Get the payload for for the GitHub Markdown API endpoint, either in "GFM" mode if there is a repository
146
     * context, or in raw mode if not.
147
     *
148
     * @see https://developer.github.com/v3/markdown/
149
     * @param  string $markdown
150
     * @return string           JSON or markdown
151
     */
152
    public function getPayload($markdown)
153
    {
154
        if ($this->getContext()) {
155
            return Convert::raw2json(
156
                array(
157
                    'text'    => $markdown,
158
                    'mode'    => 'gfm',
159
                    'context' => $this->getContext()
160
                )
161
            );
162
        }
163
164
        return $markdown;
165
    }
166
}
167