Completed
Pull Request — master (#143)
by Robbie
01:49
created

GitHubMarkdownService::toHtml()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 13
nc 2
nop 1
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
     * GitHub API configuration
23
     * @var string
24
     */
25
    const API_BASE_URI        = 'https://api.github.com';
26
    const API_REQUEST_METHOD  = 'POST';
27
    const API_RENDER_ENDPOINT = '/markdown/raw';
28
29
    /**
30
     * Use GitHub's API to render markdown to HTML
31
     *
32
     * @param  string $markdown Markdown
33
     * @return string           HTML
34
     */
35
    public function toHtml($markdown)
36
    {
37
        try {
38
            /** @var Psr\Http\Message\ResponseInterface $response */
39
            $response = $this->getClient()
40
                ->request(
41
                    $this->getRequestMethod(),
42
                    $this->getEndpoint(),
43
                    array(
44
                        'headers' => $this->getHeaders(),
45
                        'body' => $markdown
46
                    )
47
                );
48
        } catch (ClientException $ex) {
49
            user_error($ex->getMessage());
50
            return '';
51
        }
52
53
        return (string) $response->getBody();
54
    }
55
56
    /**
57
     * Get an instance of a GuzzleHttp client
58
     * @return GuzzleHttp\Client
59
     */
60
    public function getClient()
61
    {
62
        if (is_null($this->client)) {
63
            $this->client = new Client(
64
                array(
65
                    'base_uri' => $this->getBaseUri()
66
                )
67
            );
68
        }
69
70
        return $this->client;
71
    }
72
73
    /**
74
     * Get the GitHub base URI
75
     * @return string
76
     */
77
    public function getBaseUri()
78
    {
79
        return self::API_BASE_URI;
80
    }
81
82
    /**
83
     * Get the HTTP request method to use for the request
84
     * @return string
85
     */
86
    public function getRequestMethod()
87
    {
88
        return self::API_REQUEST_METHOD;
89
    }
90
91
    /**
92
     * Get the markdown parse endpoint for GitHub's API
93
     * @return string
94
     */
95
    public function getEndpoint()
96
    {
97
        return self::API_RENDER_ENDPOINT;
98
    }
99
100
    /**
101
     * Get any custom headers to use for the request
102
     * @return array
103
     */
104
    public function getHeaders()
105
    {
106
        return array(
107
            'User-Agent'   => 'silverstripe/addons-site',
108
            'Content-Type' => 'text/x-markdown'
109
        );
110
    }
111
}
112