CssInline::getStyleBlocksFromMarkup()   A
last analyzed

Complexity

Conditions 6
Paths 2

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 6

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 6
eloc 12
nc 2
nop 1
dl 0
loc 22
ccs 11
cts 11
cp 1
crap 6
rs 9.2222
c 2
b 1
f 0
1
<?php
2
namespace Aoe\Asdis\Content\Scraper\Html;
3
4
use Aoe\Asdis\Content\Scraper\Css\Url;
5
use Aoe\Asdis\Content\Scraper\ScraperInterface;
6
7
/**
8
 * Scrapes assets from inline CSS.
9
 */
10
class CssInline implements ScraperInterface
11
{
12
    /**
13
     * @var \Aoe\Asdis\Content\Scraper\Css\Url
14
     */
15
    private $cssUrlScraper;
16
17
    /**
18
     * @param \Aoe\Asdis\Content\Scraper\Css\Url $cssUrlScraper
19
     */
20 1
    public function injectCssUrlScraper(Url $cssUrlScraper)
21
    {
22 1
        $this->cssUrlScraper = $cssUrlScraper;
23 1
    }
24
25
    /**
26
     * @param $content
27
     * @return \Aoe\Asdis\Domain\Model\Asset\Collection
28
     */
29 1
    public function scrape($content)
30
    {
31 1
        return $this->cssUrlScraper->scrape(implode(PHP_EOL, $this->getStyleBlocksFromMarkup($content)));
32
    }
33
34
    /**
35
     * Returns the inner content of all <style></style> blocks of the given markup as an array.
36
     *
37
     * @param string $content
38
     * @return array
39
     */
40 1
    private function getStyleBlocksFromMarkup($content)
41
    {
42 1
        $blocks  = [];
43 1
        $matches = [];
44
45 1
        preg_match_all(
46 1
            '~<style[^>]*>(.*?)</style>~is',
47
            $content,
48
            $matches,
49 1
            PREG_PATTERN_ORDER
50
        );
51
52 1
        if (is_array($matches) && sizeof($matches) > 1 && is_array($matches[1])) {
53 1
            foreach($matches[1] as $match) {
54
                // filter inline svg styles
55 1
                if (false === strpos($match, 'fill:url')) {
56 1
                    $blocks[] = $match;
57
                }
58
            }
59
        }
60
61 1
        return $blocks;
62
    }
63
}