Url::extractUrlPaths()   B
last analyzed

Complexity

Conditions 6
Paths 4

Size

Total Lines 31
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 6.1979

Importance

Changes 0
Metric Value
cc 6
eloc 20
nc 4
nop 1
dl 0
loc 31
ccs 14
cts 17
cp 0.8235
crap 6.1979
rs 8.9777
c 0
b 0
f 0
1
<?php
2
namespace Aoe\Asdis\Content\Scraper\Css;
3
4
use Aoe\Asdis\Content\Scraper\ScraperInterface;
5
use Aoe\Asdis\Domain\Model\Asset\Factory;
6
7
/**
8
 * Scrapes paths from "url()" in CSS.
9
 */
10
class Url implements ScraperInterface
11
{
12
    /**
13
     * @var \Aoe\Asdis\Domain\Model\Asset\Factory
14
     */
15
    private $assetFactory;
16
17
    /**
18
     * @param \Aoe\Asdis\Domain\Model\Asset\Factory $assetFactory
19
     */
20 3
    public function injectAssetFactory(Factory $assetFactory)
21
    {
22 3
        $this->assetFactory = $assetFactory;
23 3
    }
24
25
    /**
26
     * @param $content
27
     * @return \Aoe\Asdis\Domain\Model\Asset\Collection
28
     */
29 3
    public function scrape($content)
30
    {
31 3
        $urls = $this->extractUrlPaths($content);
32 3
        return $this->assetFactory->createAssetsFromPaths($urls['paths'], $urls['masks']);
33
    }
34
35
    /**
36
     * Extracts paths to resources in CSS code.
37
     * This means file references which are included in "url(...)".
38
     *
39
     * @param string $cssContent
40
     * @return array
41
     */
42 3
    private function extractUrlPaths($cssContent)
43
    {
44 3
        $paths   = [];
45 3
        $masks   = [];
46 3
        $matches = [];
47
48 3
        preg_match_all(
49 3
            '~url\((?!#)\s*([\'"]?)(/?(\.\./)?.*?)([\'"]?);?\s*\)~is',
50
            $cssContent,
51
            $matches,
52 3
            PREG_PATTERN_ORDER
53
        );
54
55 3
        if (false === (is_array($matches) && sizeof($matches) > 1 && is_array($matches[2]))) {
56
            return [
57
                'paths' => $paths,
58
                'masks' => $masks
59
            ];
60
        }
61
62 3
        foreach ($matches[2] as $mkey => $path) {
63 2
            if (strpos($path, ',') !== false) {
64
                continue;
65
            }
66 2
            $paths[] = $path;
67 2
            $masks[] = $matches[1][$mkey];
68
        }
69
70
        return [
71 3
            'paths' => $paths,
72 3
            'masks' => $masks
73
        ];
74
    }
75
}