Completed
Pull Request — master (#12)
by
unknown
01:57
created

CssInline   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 1
dl 0
loc 49
ccs 16
cts 16
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A injectCssUrlScraper() 0 4 1
A scrape() 0 4 1
A getStyleBlocksFromMarkup() 0 18 4
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 1
            $content,
48 1
            $matches,
49 1
            PREG_PATTERN_ORDER
50
        );
51
52 1
        if (is_array($matches) && sizeof($matches) > 1 && is_array($matches[1])) {
53 1
            $blocks = $matches[1];
54
        }
55
56 1
        return $blocks;
57
    }
58
}