Passed
Push — develop ( 2a82ce...8ae779 )
by Brent
04:44
created

Parsedown   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 82.35%

Importance

Changes 0
Metric Value
dl 0
loc 70
ccs 28
cts 34
cp 0.8235
rs 10
c 0
b 0
f 0
wmc 11
lcom 1
cbo 2

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A element() 0 12 4
A blockFencedCode() 0 10 2
A inlineImage() 0 18 2
A parseBlankLink() 0 7 1
A parseImageWithSrcset() 0 9 1
1
<?php
2
3
namespace Brendt\Stitcher\Lib;
4
5
use Brendt\Stitcher\Parser\ImageParser;
6
use \Parsedown as LibParsedown;
7
8
/**
9
 * This class adds two extensions to the Parsedown library:
10
 *
11
 *  - Code block classes.
12
 *  - External links with `target="_blank"`.
13
 *  - Images are parsed with with the image parser
14
 *
15
 * Class Parsedown
16
 * @package Brendt\Stitcher\Lib
17
 */
18
class Parsedown extends LibParsedown
19
{
20
    private $imageParser;
21
22 40
    public function __construct(ImageParser $imageParser)
23
    {
24 40
        $this->imageParser = $imageParser;
25 40
    }
26
27 34
    public function element(array $Element)
28
    {
29 34
        if (isset($Element['attributes']['href']) && strpos($Element['attributes']['href'], '*') === 0) {
30 2
            return $this->parseBlankLink($Element);
31
        }
32
33 33
        if (isset($Element['attributes']['srcset'])) {
34 2
            return $this->parseImageWithSrcset($Element);
35
        }
36
37 32
        return parent::element($Element);
38
    }
39
40
    protected function blockFencedCode($Line)
41
    {
42
        $block = parent::blockFencedCode($Line);
43
44
        if (isset($block['element']['text']['attributes']['class'])) {
45
            $block['element']['attributes']['class'] = $block['element']['text']['attributes']['class'];
46
        }
47
48
        return $block;
49
    }
50
51 1
    protected function inlineImage($Excerpt)
52
    {
53 1
        $Inline = parent::inlineImage($Excerpt);
54
55 1
        if (!isset($Inline['element']['attributes']['src'])) {
56
            return $Inline;
57
        }
58
59 1
        $src = $Inline['element']['attributes']['src'];
60
61 1
        $responsiveImage = $this->imageParser->parse($src);
62
63 1
        $Inline['element']['attributes']['srcset'] = $responsiveImage['srcset'] ?? null;
64 1
        $Inline['element']['attributes']['sizes'] = $responsiveImage['sizes'] ?? null;
65 1
        $Inline['element']['attributes']['alt'] = $Inline['element']['attributes']['alt'] ?? null;
66
67 1
        return $Inline;
68
    }
69
70 2
    private function parseBlankLink($Element) : string
71
    {
72 2
        $href = $Element['attributes']['href'];
73 2
        $href = substr($href, 1);
74
75 2
        return "<a href=\"$href\" target=\"_blank\" rel=\"noreferrer noopener\">{$Element['text']}</a>";
76
    }
77
78 2
    private function parseImageWithSrcset($Element) : string
79
    {
80 2
        $src = $Element['attributes']['src'];
81 2
        $srcset = $Element['attributes']['srcset'];
82 2
        $sizes = $Element['attributes']['sizes'];
83 2
        $alt = $Element['attributes']['alt'];
84
85 2
        return "<img src=\"{$src}\" srcset=\"{$srcset}\" sizes=\"{$sizes}\" alt=\"{$alt}\" />";
86
    }
87
}
88