CssAttribute::injectCssUrlScraper()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
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 CssAttribute 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 2
    public function injectCssUrlScraper(Url $cssUrlScraper)
21
    {
22 2
        $this->cssUrlScraper = $cssUrlScraper;
23 2
    }
24
25
    /**
26
     * @param $content
27
     * @return \Aoe\Asdis\Domain\Model\Asset\Collection
28
     */
29 2
    public function scrape($content)
30
    {
31 2
        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 2
    private function getStyleBlocksFromMarkup($content)
41
    {
42 2
        $blocks = [];
43 2
        $matches = [];
44 2
        preg_match_all(
45 2
            '/style=.*(url\([^#].*\))/i',
46
            $content,
47
            $matches
48
        );
49 2
        if (is_array($matches) && sizeof($matches) > 1 && is_array($matches[1])) {
50 2
            $blocks = $matches[1];
51
        }
52 2
        return $blocks;
53
    }
54
}