Completed
Push — master ( e99017...2e2d17 )
by Tim
9s
created

VariantSubject::getProductProcessor()   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 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
/**
4
 * TechDivision\Import\Product\Variant\Subjects\VariantSubject
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-variant
18
 * @link      http://www.techdivision.com
19
 */
20
21
namespace TechDivision\Import\Product\Variant\Subjects;
22
23
use TechDivision\Import\Utils\RegistryKeys;
24
use TechDivision\Import\Product\Subjects\AbstractProductSubject;
25
use TechDivision\Import\Product\Variant\Services\ProductVariantProcessorInterface;
26
27
/**
28
 * A SLSB that handles the process to import product variants.
29
 *
30
 * @author    Tim Wagner <[email protected]>
31
 * @copyright 2016 TechDivision GmbH <[email protected]>
32
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
33
 * @link      https://github.com/techdivision/import-product-variant
34
 * @link      http://www.techdivision.com
35
 */
36
class VariantSubject extends AbstractProductSubject
37
{
38
39
    /**
40
     * The ID of the parent product to relate the variant with.
41
     *
42
     * @var integer
43
     */
44
    protected $parentId;
45
46
    /**
47
     * The mapping for the SKUs to the created entity IDs.
48
     *
49
     * @var array
50
     */
51
    protected $skuEntityIdMapping = array();
52
53
    /**
54
     * Intializes the previously loaded global data for exactly one variants.
55
     *
56
     * @return void
57
     * @see \Importer\Csv\Actions\ProductImportAction::prepare()
58
     */
59
    public function setUp()
60
    {
61
62
        // invoke parent method
63
        parent::setUp();
64
65
        // load the entity manager and the registry processor
66
        $registryProcessor = $this->getRegistryProcessor();
67
68
        // load the status of the actual import process
69
        $status = $registryProcessor->getAttribute($this->getSerial());
70
71
        // load the attribute set we've prepared intially
72
        $this->skuEntityIdMapping = $status[RegistryKeys::SKU_ENTITY_ID_MAPPING];
73
    }
74
75
    /**
76
     * Set's the ID of the parent product to relate the variant with.
77
     *
78
     * @param integer $parentId The ID of the parent product
79
     *
80
     * @return void
81
     */
82
    public function setParentId($parentId)
83
    {
84
        $this->parentId = $parentId;
85
    }
86
87
    /**
88
     * Return's the ID of the parent product to relate the variant with.
89
     *
90
     * @return integer The ID of the parent product
91
     */
92
    public function getParentId()
93
    {
94
        return $this->parentId;
95
    }
96
97
    /**
98
     * Return the entity ID for the passed SKU.
99
     *
100
     * @param string $sku The SKU to return the entity ID for
101
     *
102
     * @return integer The mapped entity ID
103
     * @throws \Exception Is thrown if the SKU is not mapped yet
104
     */
105
    public function mapSkuToEntityId($sku)
106
    {
107
108
        // query weather or not the SKU has been mapped
109
        if (isset($this->skuEntityIdMapping[$sku])) {
110
            return $this->skuEntityIdMapping[$sku];
111
        }
112
113
        // throw an exception if the SKU has not been mapped yet
114
        throw new \Exception(sprintf('Found not mapped SKU %s', $sku));
115
    }
116
117
    /**
118
     * Return's the store for the passed store code.
119
     *
120
     * @param string $storeCode The store code to return the store for
121
     *
122
     * @return array The requested store
123
     * @throws \Exception Is thrown, if the requested store is not available
124
     */
125
    public function getStoreByStoreCode($storeCode)
126
    {
127
128
        // query whether or not the store with the passed store code exists
129
        if (isset($this->stores[$storeCode])) {
130
            return $this->stores[$storeCode];
131
        }
132
133
        // throw an exception, if not
134
        throw new \Exception(sprintf('Found invalid store code %s', $storeCode));
135
    }
136
137
    /**
138
     * Return's the first EAV attribute for the passed option value and store ID.
139
     *
140
     * @param string $optionValue The option value of the EAV attributes
141
     * @param string $storeId     The store ID of the EAV attribues
142
     *
143
     * @return array The array with the EAV attribute
144
     */
145
    public function getEavAttributeByOptionValueAndStoreId($optionValue, $storeId)
146
    {
147
        return $this->getProductProcessor()->getEavAttributeByOptionValueAndStoreId($optionValue, $storeId);
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 getEavAttributeByOptionValueAndStoreId() does only exist in the following implementations of said interface: TechDivision\Import\Prod...ProductVariantProcessor.

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...
148
    }
149
150
    /**
151
     * Persist's the passed product relation data and return's the ID.
152
     *
153
     * @param array $productRelation The product relation data to persist
154
     *
155
     * @return void
156
     */
157
    public function persistProductRelation($productRelation)
158
    {
159
        return $this->getProductProcessor()->persistProductRelation($productRelation);
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 persistProductRelation() does only exist in the following implementations of said interface: TechDivision\Import\Prod...ProductVariantProcessor.

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...
160
    }
161
162
    /**
163
     * Persist's the passed product super link data and return's the ID.
164
     *
165
     * @param array $productSuperLink The product super link data to persist
166
     *
167
     * @return void
168
     */
169
    public function persistProductSuperLink($productSuperLink)
170
    {
171
        return $this->getProductProcessor()->persistProductSuperLink($productSuperLink);
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 persistProductSuperLink() does only exist in the following implementations of said interface: TechDivision\Import\Prod...ProductVariantProcessor.

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...
172
    }
173
174
    /**
175
     * Persist's the passed product super attribute data and return's the ID.
176
     *
177
     * @param array $productSuperAttribute The product super attribute data to persist
178
     *
179
     * @return string The ID of the persisted product super attribute entity
180
     */
181
    public function persistProductSuperAttribute($productSuperAttribute)
182
    {
183
        return $this->getProductProcessor()->persistProductSuperAttribute($productSuperAttribute);
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 persistProductSuperAttribute() does only exist in the following implementations of said interface: TechDivision\Import\Prod...ProductVariantProcessor.

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...
184
    }
185
186
    /**
187
     * Persist's the passed product super attribute label data and return's the ID.
188
     *
189
     * @param array $productSuperAttributeLabel The product super attribute label data to persist
190
     *
191
     * @return void
192
     */
193
    public function persistProductSuperAttributeLabel($productSuperAttributeLabel)
194
    {
195
        return $this->getProductProcessor()->persistProductSuperAttributeLabel($productSuperAttributeLabel);
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 persistProductSuperAttributeLabel() does only exist in the following implementations of said interface: TechDivision\Import\Prod...ProductVariantProcessor.

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...
196
    }
197
}
198