Completed
Push — master ( e5b919...026cf7 )
by Tim
05:11
created

loadProductMediaGalleryValueByValueIdAndStoreIdAndRowId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 3
crap 2
1
<?php
2
3
/**
4
 * TechDivision\Import\Product\Media\Ee\Subjects\EeMediaSubject
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
 * @copyright 2016 TechDivision GmbH <[email protected]>
16
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
17
 * @link      https://github.com/techdivision/import-product-media-ee
18
 * @link      http://www.techdivision.com
19
 */
20
21
namespace TechDivision\Import\Product\Media\Ee\Subjects;
22
23
use TechDivision\Import\Utils\RegistryKeys;
24
use TechDivision\Import\Product\Media\Subjects\MediaSubject;
25
26
/**
27
 * A subject that handles the process to import product media.
28
 *
29
 * @author    Tim Wagner <[email protected]>
30
 * @copyright 2016 TechDivision GmbH <[email protected]>
31
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
32
 * @link      https://github.com/techdivision/import-product-media-ee
33
 * @link      http://www.techdivision.com
34
 */
35
class EeMediaSubject extends MediaSubject
36
{
37
38
    /**
39
     * The mapping for the SKUs to the created entity IDs.
40
     *
41
     * @var array
42
     */
43
    protected $skuRowIdMapping = array();
44
45
    /**
46
     * Intializes the previously loaded global data for exactly one variants.
47
     *
48
     * @return void
49
     * @see \Importer\Csv\Actions\ProductImportAction::prepare()
50
     */
51 1
    public function setUp()
52
    {
53
54
        // invoke the parent method
55 1
        parent::setUp();
56
57
        // load the entity manager and the registry processor
58 1
        $registryProcessor = $this->getRegistryProcessor();
59
60
        // load the status of the actual import process
61 1
        $status = $registryProcessor->getAttribute($this->getSerial());
62
63
        // load the attribute set we've prepared intially
64 1
        $this->skuRowIdMapping = $status[RegistryKeys::SKU_ROW_ID_MAPPING];
65 1
    }
66
67
    /**
68
     * Load's the product media gallery value with the passed value/store/row ID.
69
     *
70
     * @param integer $valueId The value ID of the product media gallery value to load
71
     * @param string  $storeId The store ID of the product media gallery value to load
72
     * @param string  $rowId   The row ID of the parent product of the product media gallery value to load
73
     *
74
     * @return array The product media gallery value
75
     */
76
    public function loadProductMediaGalleryValueByValueIdAndStoreIdAndRowId($valueId, $storeId, $rowId)
77
    {
78
        return $this->getProductProcessor()->loadProductMediaGalleryValueByValueIdAndStoreIdAndRowId($valueId, $storeId, $rowId);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface TechDivision\Import\Prod...oductProcessorInterface as the method loadProductMediaGalleryV...eIdAndStoreIdAndRowId() does only exist in the following implementations of said interface: TechDivision\Import\Prod...EeProductMediaProcessor.

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...
79
    }
80
81
    /**
82
     * Load's the product media gallery with the passed value/entity ID.
83
     *
84
     * @param integer $valueId The value ID of the product media gallery value to entity to load
85
     * @param integer $rowId   The row ID of the product media gallery value to entity to load
86
     *
87
     * @return array The product media gallery
88
     */
89
    public function loadProductMediaGalleryValueToEntityByValueIdAndRowId($valueId, $rowId)
90
    {
91
        return $this->getProductProcessor()->loadProductMediaGalleryValueToEntityByValueIdAndRowId($valueId, $rowId);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface TechDivision\Import\Prod...oductProcessorInterface as the method loadProductMediaGalleryV...tityByValueIdAndRowId() does only exist in the following implementations of said interface: TechDivision\Import\Prod...EeProductMediaProcessor.

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...
92
    }
93
94
    /**
95
     * Return the row ID for the passed SKU.
96
     *
97
     * @param string $sku The SKU to return the row ID for
98
     *
99
     * @return integer The mapped row ID
100
     * @throws \Exception Is thrown if the SKU is not mapped yet
101
     */
102 1
    public function mapSkuToRowId($sku)
103
    {
104
105
        // query weather or not the SKU has been mapped
106 1
        if (isset($this->skuRowIdMapping[$sku])) {
107 1
            return $this->skuRowIdMapping[$sku];
108
        }
109
110
        // throw an exception if the SKU has not been mapped yet
111
        throw new \Exception(sprintf('Found not mapped SKU %s', $sku));
112
    }
113
}
114