GithubMarkdownAdapter::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
ccs 5
cts 5
cp 1
rs 9.4285
cc 1
eloc 4
nc 1
nop 3
crap 1
1
<?php
2
3
namespace MaglMarkdown\Adapter;
4
5
use MaglMarkdown\Adapter\Options\GithubMarkdownOptions;
6
use Zend\Http\Client;
7
use Zend\Http\Request;
8
9
/**
10
 * This is an implementation that uses github's markdown API to render Markdown
11
 *
12
 *
13
 * @see    https://developer.github.com/v3/
14
 * @see    https://developer.github.com/v3/markdown/
15
 * @author Matthias Glaub <[email protected]>
16
 */
17
class GithubMarkdownAdapter implements MarkdownAdapterInterface
18
{
19
20
    /**
21
     *
22
     * @var Client
23
     */
24
    private $httpClient;
25
26
    /**
27
     *
28
     * @var GithubMarkdownOptions
29
     */
30
    private $options;
31
32
    /**
33
     *
34
     * @var Request
35
     */
36
    private $request;
37
38 2
    public function __construct(Client $httpClient, Request $request, GithubMarkdownOptions $options)
39
    {
40 2
        $this->httpClient = $httpClient;
41 2
        $this->options = $options;
42 2
        $this->request = $request;
43 2
    }
44
45 1
    public function transformText($text)
46
    {
47
        $requestArray = array(
48 1
            'text' => $text,
49 1
            'mode' => $this->options->getMarkdownMode(),
50
        );
51
52 1
        $this->request->setUri($this->options->getMarkdownApiUri());
53 1
        $this->request->setMethod(Request::METHOD_POST);
54 1
        $this->request->setContent(json_encode($requestArray));
55 1
        if ($this->options->getAccessToken()) {
56 1
            $this->request->getHeaders()
57 1
                ->addHeaderLine('Authorization', 'token ' . $this->options->getAccessToken());
58
        }
59
60 1
        $response = $this->httpClient->send($this->request);
61
62 1
        return $response->getBody();
63
    }
64
}
65