Completed
Push — analysis-D2M1DL ( 825c2b )
by Arnaud
06:16 queued 11s
created

Parsedown::inlineImage()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 26
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 14
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 26
rs 9.7998
1
<?php
2
/*
3
 * Copyright (c) Arnaud Ligny <[email protected]>
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
9
namespace Cecil\Converter;
10
11
use Cecil\Assets\Image;
12
use Cecil\Builder;
13
use ParsedownExtra;
14
15
class Parsedown extends ParsedownExtra
16
{
17
    /**
18
     * @var Builder
19
     */
20
    private $builder;
21
22
    /**
23
     * {@inheritdoc}
24
     */
25
    public function __construct(Builder $builder = null)
26
    {
27
        parent::__construct();
28
        $this->builder = $builder;
29
    }
30
31
    /**
32
     * {@inheritdoc}
33
     */
34
    protected function inlineImage($excerpt)
35
    {
36
        $image = parent::inlineImage($excerpt);
37
38
        if (!isset($image)) {
39
            return null;
40
        }
41
42
        $query = parse_url($image['element']['attributes']['src'], PHP_URL_QUERY);
43
        if ($query === null) {
44
            return $image;
45
        }
46
        // clean URL
47
        $image['element']['attributes']['src'] = strtok($image['element']['attributes']['src'], '?');
48
49
        // has resize value
50
        parse_str($query, $result);
51
        if (array_key_exists('resize', $result)) {
52
            $size = (int) $result['resize'];
53
            $image['element']['attributes']['width'] = $size;
54
55
            $image['element']['attributes']['src'] = (new Image($this->builder))
56
                ->resize($image['element']['attributes']['src'], $size);
57
        }
58
59
        return $image;
60
    }
61
}
62