Srcset::injectAssetFactory()   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 1
Bugs 0 Features 1
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 1
b 0
f 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
}