Completed
Push — master ( 8a8ced...010f71 )
by Tim
10s
created

Magic360GalleryObserver   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 1
dl 0
loc 93
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getProductMagic360Processor() 0 4 1
B process() 0 31 3
A initializeMagic360Gallery() 0 4 1
A persistMagic360Gallery() 0 4 1
1
<?php
2
3
/**
4
 * TechDivision\Import\Product\Magic360\Observers\Magic360ImageObserver
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\Magic360\Utils\MemberNames;
26
use TechDivision\Import\Product\Magic360\Services\ProductMagic360ProcessorInterface;
27
use TechDivision\Import\Product\Observers\AbstractProductImportObserver;
28
29
/**
30
 * Adds the images from within artefacts to the Magic360 tables
31
 *
32
 * @author    Tim Wagner <[email protected]>
33
 * @author    Bernhard Wick <[email protected]>
34
 * @copyright 2017 TechDivision GmbH <[email protected]>
35
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
36
 * @link      https://github.com/techdivision/import-product-magic360
37
 * @link      http://www.techdivision.com
38
 */
39
class Magic360GalleryObserver extends AbstractProductImportObserver
40
{
41
42
    /**
43
     * The product magic360 processor instance.
44
     *
45
     * @var \TechDivision\Import\Product\Services\ProductBunchProcessorInterface
46
     */
47
    protected $productMagic360Processor;
48
49
    /**
50
     * Initialize the observer with the passed product magic360 processor instance.
51
     *
52
     * @param \TechDivision\Import\Product\Magic360\Services\ProductMagic360ProcessorInterface $productMagic360Processor The product magic360 processor instance
53
     */
54
    public function __construct(ProductMagic360ProcessorInterface $productMagic360Processor)
55
    {
56
        $this->productMagic360Processor = $productMagic360Processor;
0 ignored issues
show
Documentation Bug introduced by
It seems like $productMagic360Processor of type object<TechDivision\Impo...c360ProcessorInterface> is incompatible with the declared type object<TechDivision\Impo...unchProcessorInterface> of property $productMagic360Processor.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
57
    }
58
59
    /**
60
     * Return's the product magic360 processor instance.
61
     *
62
     * @return \TechDivision\Import\Product\Magic360\Services\ProductMagic360ProcessorInterface The product magic360 processor instance
63
     */
64
    protected function getProductMagic360Processor()
65
    {
66
        return $this->productMagic360Processor;
67
    }
68
69
    /**
70
     * Process the observer's business logic.
71
     *
72
     * @return void
73
     *
74
     * @throws \Exception Possible error in non-debug mode
75
     */
76
    protected function process()
77
    {
78
        // extract the parent/child ID as well as the link type code from the row
79
        $sku = $this->getValue(ColumnKeys::SKU);
80
81
        // load parent/child IDs and link type ID
82
        $productId = $this->getSubject()->mapSkuToEntityId($sku);
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 mapSkuToEntityId() does only exist in the following implementations of said interface: TechDivision\Import\Prod...ubjects\Magic360Subject.

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...
83
84
        $images360Path = DIRECTORY_SEPARATOR . ltrim($this->getValue(ColumnKeys::IMAGES_PATH), DIRECTORY_SEPARATOR);
85
        $mediaFilePath = $this->getSubject()->getMediaDir();
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 getMediaDir() does only exist in the following implementations of said interface: TechDivision\Import\Prod...ubjects\Magic360Subject.

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...
86
        // iterate the images (if any) and initialize an entity for each one
87
        $images = $this->getSubject()->getFilesystem()->listContents($mediaFilePath . $images360Path);
88
        if (count($images) > 0) {
89
            $entity = null;
0 ignored issues
show
Unused Code introduced by
$entity is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
90
            $entityId = null;
0 ignored issues
show
Unused Code introduced by
$entityId is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
91
            /**
92
             * @var integer $position
93
             * @var array $image
94
             */
95
            foreach ($images as $position => $image) {
96
                $entity = $this->initializeMagic360Gallery($this->initializeEntity(
97
                    array(
98
                        MemberNames::PRODUCT_ID => $productId,
99
                        MemberNames::POSITION => $position + 1,
100
                        MemberNames::FILE => $images360Path . $image['basename']
101
                    )
102
                ));
103
                $this->persistMagic360Gallery($entity);
104
            }
105
        }
106
    }
107
108
    /**
109
     * Initialize the magic360 gallery with the passed attributes and returns an instance.
110
     *
111
     * @param array $attr The gallery attributes
112
     *
113
     * @return array The initialized gallery
114
     */
115
    protected function initializeMagic360Gallery(array $attr)
116
    {
117
        return $attr;
118
    }
119
120
    /**
121
     * Persists the passed magic360 gallery data and returns the ID.
122
     *
123
     * @param array $magic360Gallery The gallery data to persist
124
     *
125
     * @return string The ID of the persisted entity
126
     */
127
    protected function persistMagic360Gallery($magic360Gallery)
128
    {
129
        return $this->getProductMagic360Processor()->persistMagic360Gallery($magic360Gallery);
0 ignored issues
show
Bug introduced by
The method persistMagic360Gallery() does not seem to exist on object<TechDivision\Impo...unchProcessorInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
130
    }
131
}
132