Completed
Push — pac-73--image-sort-order ( 2e30f2...5d17a0 )
by Marcus
01:54
created

MediaRolesLoader::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 0
cts 5
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
/**
4
 * TechDivision\Import\Product\Loaders\MediaRolesLoader
5
 *
6
 * @author    Marcus Döllerer <[email protected]>
7
 * @copyright 2020 TechDivision GmbH <[email protected]>
8
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
9
 * @link      https://github.com/techdivision/import
10
 * @link      https://www.techdivision.com
11
 */
12
13
namespace TechDivision\Import\Product\Loaders;
14
15
use TechDivision\Import\Loaders\LoaderInterface;
16
use TechDivision\Import\Product\Utils\ColumnKeys;
17
use TechDivision\Import\Services\ImportProcessorInterface;
18
19
/**
20
 * Loader for media roles.
21
 *
22
 * @author    Marcus Döllerer <[email protected]>
23
 * @copyright 2020 TechDivision GmbH <[email protected]>
24
 * @link      https://www.techdivision.com
25
 */
26
class MediaRolesLoader implements LoaderInterface
27
{
28
29
    /**
30
     * The media roles array (default: ['base', 'small', 'thumbnail', 'swatch']).
31
     *
32
     * @var array
33
     */
34
    protected $mediaRoles = array();
35
36
    /**
37
     * The import processor.
38
     *
39
     * @var ImportProcessorInterface
40
     */
41
    protected $importProcessor;
42
43
    /**
44
     * ImageMediaRolesLoader constructor
45
     *
46
     * @param ImportProcessorInterface $importProcessor The import processor.
47
     */
48
    public function __construct(ImportProcessorInterface $importProcessor)
49
    {
50
        $this->importProcessor = $importProcessor;
51
        $this->mediaRoles = $this->createMediaRoles();
52
    }
53
54
    /**
55
     * @return \ArrayAccess|void|array
56
     */
57
    public function load()
58
    {
59
        return $this->mediaRoles;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->mediaRoles; (array) is incompatible with the return type declared by the interface TechDivision\Import\Loaders\LoaderInterface::load of type ArrayAccess.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
60
    }
61
62
    /**
63
     * Creates media roles from available image types.
64
     *
65
     * @return array
66
     */
67
    public function createMediaRoles()
68
    {
69
70
        // initialize default values
71
        $mediaRoles = array();
72
73
        // derive media roles form image types
74
        foreach ($this->importProcessor->getImageTypes() as $imageColumnName => $imageLabelColumnName) {
75
            // create the role based prefix for the image columns
76
            $role = str_replace('_image', null, $imageColumnName);
77
78
            // initialize the values for the corresponding media role
79
            $mediaRoles[$role] = array(
80
                ColumnKeys::IMAGE_PATH        => $imageColumnName,
81
                ColumnKeys::IMAGE_LABEL       => $imageLabelColumnName,
82
                ColumnKeys::IMAGE_POSITION    => sprintf('%s_image_position', $role)
83
            );
84
        }
85
86
        return $mediaRoles;
87
    }
88
}
89