Completed
Pull Request — master (#2609)
by
unknown
70:41 queued 64:09
created

MediaTokenTransformer::reverseTransform()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 42

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 5.0554

Importance

Changes 0
Metric Value
dl 0
loc 42
ccs 20
cts 23
cp 0.8696
rs 8.9368
c 0
b 0
f 0
cc 5
nc 4
nop 1
crap 5.0554
1
<?php
2
3
namespace Kunstmaan\AdminBundle\Form;
4
5
use Symfony\Component\DomCrawler\Crawler;
6
use Symfony\Component\Form\DataTransformerInterface;
7
8
/**
9
 * Class MediaTokenTransformer.
10
 */
11
class MediaTokenTransformer implements DataTransformerInterface
12
{
13
    /**
14
     * @param mixed $content
15
     *
16
     * @return string
17
     */
18 1
    public function transform($content)
19
    {
20 1
        if (!trim($content)) {
21
            return '';
22
        }
23
24 1
        $crawler = new Crawler();
25 1
        $crawler->addHtmlContent($content);
26
27 1
        $crawler->filter('img,a')->each(
28
            function (Crawler $node) {
29 1
                $element = $node->getNode(0);
30 1
                $attribute = $element->nodeName === 'img' ? 'src' : 'href';
31 1
                if ($element->hasAttribute('data-' . $attribute)) {
0 ignored issues
show
Bug introduced by
The method hasAttribute() does not exist on DOMNode. Did you maybe mean hasAttributes()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
32 1
                    $attributeValue = $element->getAttribute('data-' . $attribute);
33 1
                    $element->setAttribute($attribute, $attributeValue);
34 1
                    $element->removeAttribute('data-' . $attribute);
35
                }
36 1
            }
37
        );
38
39
        try {
40 1
            return $crawler->html();
41
        } catch (\InvalidArgumentException $exception) {
42
            return $content;
43
        }
44
    }
45
46
    /**
47
     * @param mixed $content
48
     *
49
     * @return string
50
     */
51 1
    public function reverseTransform($content)
52
    {
53 1
        if (!trim($content)) {
54 1
            return '';
55
        }
56
57 1
        $crawler = new Crawler();
58 1
        $crawler->addHtmlContent($content);
59
60
        // Get all img and a tags and parse the token.
61 1
        $crawler->filter('img,a')->each(
62
            function (Crawler $node) {
63 1
                $element = $node->getNode(0);
64 1
                $attribute = $element->nodeName === 'img' ? 'src' : 'href';
65 1
                $attributeValue = $element->getAttribute($attribute);
66 1
                $parsed = parse_url($attributeValue, PHP_URL_QUERY);
67 1
                parse_str($parsed, $query);
68
69 1
                if (isset($query['token'])) {
70
                    $element->setAttribute($attribute, $query['token']);
71
                }
72 1
                $element->setAttribute('data-' . $attribute, $attributeValue);
73 1
            }
74
        );
75
76
        try {
77 1
            $html = $crawler->filter('body')->html();
78
79
            // URL-decode square brackets in img and a tags
80 1
            $html = preg_replace_callback(
81 1
                '/<(img|a)\s+[^>]*>/',
82
                function ($matches) {
83 1
                    return str_replace(['%5B', '%5D'], ['[', ']'], $matches[0]);
84 1
                },
85
                $html
86
            );
87
88 1
            return $html;
89
        } catch (\InvalidArgumentException $exception) {
90
            return $content;
91
        }
92
    }
93
}
94