Test Failed
Push — master ( a58c94...1bcec0 )
by Brent
03:26
created

ImageParser::parse()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 32
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 21
nc 6
nop 1
dl 0
loc 32
rs 8.439
c 0
b 0
f 0
1
<?php
2
3
namespace Brendt\Stitcher\Parser;
4
5
use brendt\image\ResponsiveFactory;
6
use Brendt\Stitcher\Config;
7
8
/**
9
 * The ImageParser uses the ResponsiveFactory to create images from an entry (array of parsed data),
10
 * or a path to an image.
11
 *
12
 * @see \brendt\image\ResponsiveFactory::create()
13
 */
14
class ImageParser implements Parser {
15
16
    /**
17
     * @var ResponsiveFactory
18
     */
19
    protected $factory;
20
21
    /**
22
     * AbstractParser constructor.
23
     */
24
    public function __construct() {
25
        $this->factory = Config::getDependency('factory.image');
26
    }
27
28
    /**
29
     * @param $entry
30
     *
31
     * @return array
32
     */
33
    public function parse($entry) {
34
        $data = [];
35
        $defaults = [];
36
        $path = null;
37
38
        if (is_array($entry)) {
39
            if (array_key_exists('src', $entry)) {
40
                $path = $entry['src'];
41
                unset($entry['src']);
42
            }
43
44
            foreach ($entry as $field => $value) {
45
                $defaults[$field] = $value;
46
            }
47
        } else {
48
            $path = $entry;
49
        }
50
51
        if (!$path) {
52
            return $data;
53
        }
54
55
        $image = $this->factory->create($path);
56
57
        $data = [
58
                'src'    => $image->src(),
59
                'srcset' => $image->srcset(),
60
                'sizes'  => $image->sizes(),
61
            ] + $defaults;
62
63
        return $data;
64
    }
65
66
}
67