Completed
Pull Request — master (#142)
by Robbie
06:53 queued 04:57
created

GitHubMarkdownService::toHtml()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 12
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
        }
51
52
        return (string) $response->getBody();
53
    }
54
55
    /**
56
     * Get an instance of a GuzzleHttp client
57
     * @return GuzzleHttp\Client
58
     */
59
    public function getClient()
60
    {
61
        if (is_null($this->client)) {
62
            $this->client = new Client(
63
                array(
64
                    'base_uri' => $this->getBaseUri()
65
                )
66
            );
67
        }
68
69
        return $this->client;
70
    }
71
72
    /**
73
     * Get the GitHub base URI
74
     * @return string
75
     */
76
    public function getBaseUri()
77
    {
78
        return self::API_BASE_URI;
79
    }
80
81
    /**
82
     * Get the HTTP request method to use for the request
83
     * @return string
84
     */
85
    public function getRequestMethod()
86
    {
87
        return self::API_REQUEST_METHOD;
88
    }
89
90
    /**
91
     * Get the markdown parse endpoint for GitHub's API
92
     * @return string
93
     */
94
    public function getEndpoint()
95
    {
96
        return self::API_RENDER_ENDPOINT;
97
    }
98
99
    /**
100
     * Get any custom headers to use for the request
101
     * @return array
102
     */
103
    public function getHeaders()
104
    {
105
        return array(
106
            'User-Agent'   => 'silverstripe/addons-site',
107
            'Content-Type' => 'text/x-markdown'
108
        );
109
    }
110
}
111