ProductMagic360Observer::addArtefacts()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
/**
4
 * TechDivision\Import\Product\Magic360\Observers\ProductMagic360Observer
5
 *
6
 * NOTICE OF LICENSE
7
 *
8
 * This source file is subject to the Open Software License (OSL 3.0)
9
 * that is available through the world-wide-web at this URL:
10
 * http://opensource.org/licenses/osl-3.0.php
11
 *
12
 * PHP version 5
13
 *
14
 * @author    Tim Wagner <[email protected]>
15
 * @author    Bernhard Wick <[email protected]>
16
 * @copyright 2017 TechDivision GmbH <[email protected]>
17
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
18
 * @link      https://github.com/techdivision/import-product-magic360
19
 * @link      http://www.techdivision.com
20
 */
21
22
namespace TechDivision\Import\Product\Magic360\Observers;
23
24
use TechDivision\Import\Product\Magic360\Utils\ColumnKeys;
25
use TechDivision\Import\Product\Observers\AbstractProductImportObserver;
26
27
/**
28
 * Prepares the artefacts for the generic Magic360 gallery type import.
29
 *
30
 * @author    Tim Wagner <[email protected]>
31
 * @author    Bernhard Wick <[email protected]>
32
 * @copyright 2017 TechDivision GmbH <[email protected]>
33
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
34
 * @link      https://github.com/techdivision/import-product-magic360
35
 * @link      http://www.techdivision.com
36
 */
37
class ProductMagic360Observer extends AbstractProductImportObserver
38
{
39
40
    /**
41
     * The artefact type.
42
     *
43
     * @var string
44
     */
45
    const ARTEFACT_TYPE = 'magic360';
46
47
    /**
48
     * Process the observer's business logic.
49
     *
50
     * @return array The processed row
51
     */
52
    protected function process()
53
    {
54
        // we will only look for the image path if the row is flagged as having 360 images
55
        if ($this->hasValue(ColumnKeys::IS_360)) {
56
            $this->addArtefacts(
57
                array(
58
                    array(
59
                        ColumnKeys::SKU => $this->getValue(ColumnKeys::SKU),
60
                        ColumnKeys::IMAGES_PATH => $this->getValue(ColumnKeys::IMAGES_360)
61
                    )
62
                )
63
            );
64
        }
65
    }
66
67
    /**
68
     * Add the passed artefacts to the product with the
69
     * last entity ID.
70
     *
71
     * @param array $artefacts The product type artefacts
72
     *
73
     * @return void
74
     */
75
    protected function addArtefacts(array $artefacts)
76
    {
77
        $this->getSubject()->addArtefacts(ProductMagic360Observer::ARTEFACT_TYPE, $artefacts);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface TechDivision\Import\Subjects\SubjectInterface as the method addArtefacts() does only exist in the following implementations of said interface: TechDivision\Import\Plugins\ExportableSubjectImpl, TechDivision\Import\Product\Subjects\BunchSubject, TechDivision\Import\Subjects\ExportableTraitImpl.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
78
    }
79
}
80