Srcset   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Test Coverage

Coverage 84.21%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 20
dl 0
loc 46
ccs 16
cts 19
cp 0.8421
rs 10
c 1
b 0
f 1
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A scrape() 0 27 4
A injectAssetFactory() 0 3 1
1
<?php
2
namespace Aoe\Asdis\Content\Scraper\Html;
3
4
use Aoe\Asdis\Content\Scraper\Html\AbstractHtmlScraper;
5
use Aoe\Asdis\Content\Scraper\ScraperInterface;
6
use Aoe\Asdis\Domain\Model\Asset\Factory;
7
8
/**
9
 * Scrapes assets from all tags by using data-src attributes.
10
 */
11
class Srcset extends AbstractHtmlScraper implements ScraperInterface
12
{
13
    /**
14
     * @var \Aoe\Asdis\Domain\Model\Asset\Factory
15
     */
16
    private $assetFactory;
17
18
    /**
19
     * @param \Aoe\Asdis\Domain\Model\Asset\Factory $assetFactory
20
     */
21 1
    public function injectAssetFactory(Factory $assetFactory)
22
    {
23 1
        $this->assetFactory = $assetFactory;
24 1
    }
25
26
    /**
27
     * @param string $content
28
     * @return \Aoe\Asdis\Domain\Model\Asset\Collection
29
     */
30 1
    public function scrape($content)
31
    {
32 1
        $paths = [];
33 1
        $masks = [];
34 1
        $matches = [];
35 1
        preg_match_all(
36 1
            '/srcset=*\s?=\s?([\'"])(.*?)([\'"])/i',
37
            $content,
38
            $matches
39
        );
40
41 1
        foreach ($matches[2] as $mkey => $path) {
42 1
            if (strpos($path, ',') === false) {
43
                $paths[] = $path;
44
                $masks[] = $matches[1][$mkey];
45
                continue;
46
            }
47
48 1
            $expPaths = explode(',', $path);
49
50 1
            foreach ($expPaths as $singlePath) {
51 1
                $paths[] = trim(str_replace(' 2x','', $singlePath));
52 1
                $masks[] = '';
53
            }
54
        }
55
56 1
        return $this->assetFactory->createAssetsFromPaths($paths, $masks);
57
    }
58
}