Completed
Push — master ( a3ab00...b679e8 )
by Tim
12s
created

getProductBundleProcessor()   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\Bundle\Observers\BundleOptionValueObserver
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-bundle
18
 * @link      http://www.techdivision.com
19
 */
20
21
namespace TechDivision\Import\Product\Bundle\Observers;
22
23
use TechDivision\Import\Utils\StoreViewCodes;
24
use TechDivision\Import\Product\Bundle\Utils\ColumnKeys;
25
use TechDivision\Import\Product\Bundle\Utils\MemberNames;
26
use TechDivision\Import\Product\Observers\AbstractProductImportObserver;
27
use TechDivision\Import\Product\Bundle\Services\ProductBundleProcessorInterface;
28
29
/**
30
 * Oberserver that provides functionality for the bundle option value replace operation.
31
 *
32
 * @author    Tim Wagner <[email protected]>
33
 * @copyright 2016 TechDivision GmbH <[email protected]>
34
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
35
 * @link      https://github.com/techdivision/import-product-bundle
36
 * @link      http://www.techdivision.com
37
 */
38
class BundleOptionValueObserver extends AbstractProductImportObserver
39
{
40
41
    /**
42
     * The option value store mapping.
43
     *
44
     * @var array
45
     */
46
    protected $optionValueStoreMapping = array();
47
48
    /**
49
     * The product bundle processor instance.
50
     *
51
     * @var \TechDivision\Import\Product\Bundle\Services\ProductBundleProcessorInterface
52
     */
53
    protected $productBundleProcessor;
54
55
    /**
56
     * Initialize the observer with the passed product bundle processor instance.
57
     *
58
     * @param \TechDivision\Import\Product\Bundle\Services\ProductBundleProcessorInterface $productBundleProcessor The product bundle processor instance
59
     */
60
    public function __construct(ProductBundleProcessorInterface $productBundleProcessor)
61
    {
62
        $this->productBundleProcessor = $productBundleProcessor;
63
    }
64
65
    /**
66
     * Return's the product bundle processor instance.
67
     *
68
     * @return \TechDivision\Import\Product\Bundle\Services\ProductBundleProcessorInterface The product bundle processor instance
69
     */
70
    protected function getProductBundleProcessor()
71
    {
72
        return $this->productBundleProcessor;
73
    }
74
75
    /**
76
     * Process the observer's business logic.
77
     *
78
     * @return array The processed row
79
     */
80
    protected function process()
81
    {
82
83
        // prepare the store view code
84
        $this->prepareStoreViewCode($this->getRow());
0 ignored issues
show
Unused Code introduced by
The call to BundleOptionValueObserver::prepareStoreViewCode() has too many arguments starting with $this->getRow().

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
85
86
        // prepare the attributes
87
        $attr = $this->prepareAttributes();
88
89
        // load store/option ID
90
        $storeId = $attr[MemberNames::STORE_ID];
91
        $optionId = $attr[MemberNames::OPTION_ID];
92
93
        // if the store has already been mapped, return immediately
94
        if ($this->isMapped($optionId, $storeId)) {
95
            return;
96
        }
97
98
        // initialize and save the product bundle option value
99
        if ($bundleOption = $this->initializeBundleOptionValue($attr)) {
100
            $this->persistProductBundleOptionValue($bundleOption);
101
        }
102
103
        // add the store => option mapping
104
        $this->addOptionValueStoreMapping($optionId, $storeId);
105
    }
106
107
    /**
108
     * Prepare the attributes of the entity that has to be persisted.
109
     *
110
     * @return array The prepared attributes
111
     */
112
    protected function prepareAttributes()
113
    {
114
115
        // load the product bundle option name
116
        $name = $this->getValue(ColumnKeys::BUNDLE_VALUE_NAME);
117
118
        // load the actual option ID
119
        $optionId = $this->getLastOptionId();
120
121
        // load the store/website ID
122
        $store = $this->getStoreByStoreCode($this->getStoreViewCode(StoreViewCodes::ADMIN));
123
        $storeId = $store[MemberNames::STORE_ID];
124
125
        // return the prepared product
126
        return $this->initializeEntity(
127
            array(
128
                MemberNames::OPTION_ID => $optionId,
129
                MemberNames::STORE_ID  => $storeId,
130
                MemberNames::TITLE     => $name
131
            )
132
        );
133
    }
134
135
    /**
136
     * Initialize the bundle option value with the passed attributes and returns an instance.
137
     *
138
     * @param array $attr The bundle option value attributes
139
     *
140
     * @return array The initialized bundle option value
141
     */
142
    protected function initializeBundleOptionValue(array $attr)
143
    {
144
        return $attr;
145
    }
146
147
    /**
148
     * Add the store => option mapping.
149
     *
150
     * @param integer $optionId The option ID to map
151
     * @param integer $storeId  The store ID to map
152
     *
153
     * @return void
154
     */
155
    protected function addOptionValueStoreMapping($optionId, $storeId)
156
    {
157
        $this->optionValueStoreMapping[$optionId][] = $storeId;
158
    }
159
160
    /**
161
     * Query whether or not the passed option/store ID combination has already been mapped.
162
     *
163
     * @param integer $optionId The option ID to map
164
     * @param integer $storeId  The store ID to map
165
     *
166
     * @return boolean TRUE if the combination has already been mapped, else FALSE
167
     */
168
    protected function isMapped($optionId, $storeId)
169
    {
170
        return (isset($this->optionValueStoreMapping[$optionId]) && in_array($storeId, $this->optionValueStoreMapping[$optionId]));
171
    }
172
173
    /**
174
     * Return's the last created option ID.
175
     *
176
     * @return integer $optionId The last created option ID
177
     */
178
    protected function getLastOptionId()
179
    {
180
        return $this->getSubject()->getLastOptionId();
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 getLastOptionId() does only exist in the following implementations of said interface: TechDivision\Import\Prod...\Subjects\BundleSubject.

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...
181
    }
182
183
    /**
184
     * Return's the option ID for the passed name.
185
     *
186
     * @param string $name The name to return the option ID for
187
     *
188
     * @return integer The option ID for the passed name
189
     * @throws \Exception Is thrown, if no option ID for the passed name is available
190
     */
191
    protected function getOptionIdForName($name)
192
    {
193
        return $this->getSubject()->getOptionIdForName($name);
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 getOptionIdForName() does only exist in the following implementations of said interface: TechDivision\Import\Prod...\Subjects\BundleSubject.

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...
194
    }
195
196
    /**
197
     * Return's the store for the passed store code.
198
     *
199
     * @param string $storeCode The store code to return the store for
200
     *
201
     * @return array The requested store
202
     * @throws \Exception Is thrown, if the requested store is not available
203
     */
204
    protected function getStoreByStoreCode($storeCode)
205
    {
206
        return $this->getSubject()->getStoreByStoreCode($storeCode);
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 getStoreByStoreCode() does only exist in the following implementations of said interface: TechDivision\Import\Prod...\Subjects\BundleSubject.

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...
207
    }
208
209
    /**
210
     * Persist's the passed product bundle option value data.
211
     *
212
     * @param array $productBundleOptionValue The product bundle option value data to persist
213
     *
214
     * @return void
215
     */
216
    protected function persistProductBundleOptionValue($productBundleOptionValue)
217
    {
218
        $this->getProductBundleProcessor()->persistProductBundleOptionValue($productBundleOptionValue);
219
    }
220
}
221