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

ImageParser   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 53
rs 10
c 0
b 0
f 0
wmc 6
lcom 1
cbo 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
B parse() 0 32 5
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