Completed
Push — master ( 25a706...762587 )
by Tim
12s
created

MediaGalleryValueObserver::getStoreByStoreCode()   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 1
crap 2
1
<?php
2
3
/**
4
 * TechDivision\Import\Product\Media\Observers\MediaGalleryValueObserver
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
18
 * @link      http://www.techdivision.com
19
 */
20
21
namespace TechDivision\Import\Product\Media\Observers;
22
23
use TechDivision\Import\Utils\StoreViewCodes;
24
use TechDivision\Import\Product\Media\Utils\ColumnKeys;
25
use TechDivision\Import\Product\Media\Utils\MemberNames;
26
use TechDivision\Import\Product\Observers\AbstractProductImportObserver;
27
use TechDivision\Import\Product\Media\Services\ProductMediaProcessorInterface;
28
29
/**
30
 * Observer that creates/updates the product's media gallery value information.
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-media
36
 * @link      http://www.techdivision.com
37
 */
38
class MediaGalleryValueObserver extends AbstractProductImportObserver
39
{
40
41
    /**
42
     * The product media processor instance.
43
     *
44
     * @var \TechDivision\Import\Product\Media\Services\ProductMediaProcessorInterface
45
     */
46
    protected $productMediaProcessor;
47
48
    /**
49
     * Initialize the observer with the passed product media processor instance.
50
     *
51
     * @param \TechDivision\Import\Product\Media\Services\ProductMediaProcessorInterface $productMediaProcessor The product media processor instance
52
     */
53
    public function __construct(ProductMediaProcessorInterface $productMediaProcessor)
54
    {
55
        $this->productMediaProcessor = $productMediaProcessor;
56
    }
57
58
    /**
59
     * Return's the product media processor instance.
60
     *
61
     * @return \TechDivision\Import\Product\Media\Services\ProductMediaProcessorInterface The product media processor instance
62
     */
63
    protected function getProductMediaProcessor()
64
    {
65
        return $this->productMediaProcessor;
66
    }
67
68
    /**
69
     * Process the observer's business logic.
70
     *
71
     * @return array The processed row
72
     */
73
    protected function process()
74
    {
75
76
        // query whether or not, the image changed
77
        if ($this->isParentImage($imagePath = $this->getValue(ColumnKeys::IMAGE_PATH))) {
78
            return;
79
        }
80
81
        // initialize and persist the product media gallery value
82
        $productMediaGalleryValue = $this->initializeProductMediaGalleryValue($this->prepareAttributes());
83
        $this->persistProductMediaGalleryValue($productMediaGalleryValue);
84
85
        // temporarily persist the image name
86
        $this->setParentImage($imagePath);
87
    }
88
89
    /**
90
     * Prepare the product media gallery value that has to be persisted.
91
     *
92
     * @return array The prepared product media gallery value attributes
93
     */
94
    protected function prepareAttributes()
95
    {
96
97
        try {
98
            // try to load the product SKU and map it the entity ID
99
            $parentId= $this->getValue(ColumnKeys::IMAGE_PARENT_SKU, null, array($this, 'mapParentSku'));
100
        } catch (\Exception $e) {
101
            throw $this->wrapException(array(ColumnKeys::IMAGE_PARENT_SKU), $e);
0 ignored issues
show
Documentation introduced by
array(\TechDivision\Impo...Keys::IMAGE_PARENT_SKU) is of type array<integer,?>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
102
        }
103
104
        // load the store ID
105
        $storeId = $this->getRowStoreId(StoreViewCodes::ADMIN);
106
107
        // load the value ID and the position counter
108
        $valueId = $this->getParentValueId();
109
        $position = $this->raisePositionCounter();
110
111
        // load the image label
112
        $imageLabel = $this->getValue(ColumnKeys::IMAGE_LABEL);
113
114
        // prepare the media gallery value
115
        return $this->initializeEntity(
116
            array(
117
                MemberNames::VALUE_ID    => $valueId,
118
                MemberNames::STORE_ID    => $storeId,
119
                MemberNames::ENTITY_ID   => $parentId,
120
                MemberNames::LABEL       => $imageLabel,
121
                MemberNames::POSITION    => $position,
122
                MemberNames::DISABLED    => 0
123
            )
124
        );
125
    }
126
127
    /**
128
     * Initialize the product media gallery value with the passed attributes and returns an instance.
129
     *
130
     * @param array $attr The product media gallery value attributes
131
     *
132
     * @return array The initialized product media gallery value
133
     */
134
    protected function initializeProductMediaGalleryValue(array $attr)
135
    {
136
        return $attr;
137
    }
138
139
    /**
140
     * Return's the store ID of the actual row, or of the default store
141
     * if no store view code is set in the CSV file.
142
     *
143
     * @param string|null $default The default store view code to use, if no store view code is set in the CSV file
144
     *
145
     * @return integer The ID of the actual store
146
     * @throws \Exception Is thrown, if the store with the actual code is not available
147
     */
148
    protected function getRowStoreId($default = null)
149
    {
150
        return $this->getSubject()->getRowStoreId($default);
151
    }
152
153
    /**
154
     * Map's the passed SKU of the parent product to it's PK.
155
     *
156
     * @param string $parentSku The SKU of the parent product
157
     *
158
     * @return integer The primary key used to create relations
159
     */
160
    protected function mapParentSku($parentSku)
161
    {
162
        return $this->mapSkuToEntityId($parentSku);
163
    }
164
165
    /**
166
     * Return the entity ID for the passed SKU.
167
     *
168
     * @param string $sku The SKU to return the entity ID for
169
     *
170
     * @return integer The mapped entity ID
171
     * @throws \Exception Is thrown if the SKU is not mapped yet
172
     */
173
    protected function mapSkuToEntityId($sku)
174
    {
175
        return $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...a\Subjects\MediaSubject.

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...
176
    }
177
178
    /**
179
     * Set's the name of the created image.
180
     *
181
     * @param string $parentImage The name of the created image
182
     *
183
     * @return void
184
     */
185
    protected function setParentImage($parentImage)
186
    {
187
        $this->getSubject()->setParentImage($parentImage);
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 setParentImage() does only exist in the following implementations of said interface: TechDivision\Import\Prod...a\Subjects\MediaSubject.

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...
188
    }
189
190
    /**
191
     * Return's the name of the created image.
192
     *
193
     * @return string The name of the created image
194
     */
195
    protected function getParentImage()
196
    {
197
        return $this->getSubject()->getParentImage();
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 getParentImage() does only exist in the following implementations of said interface: TechDivision\Import\Prod...a\Subjects\MediaSubject.

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...
198
    }
199
200
    /**
201
     * Return's TRUE if the passed image is the parent one.
202
     *
203
     * @param string $image The imageD to check
204
     *
205
     * @return boolean TRUE if the passed image is the parent one
206
     */
207
    protected function isParentImage($image)
208
    {
209
        return $this->getParentImage() === $image;
210
    }
211
212
    /**
213
     * Return's the value ID of the created media gallery entry.
214
     *
215
     * @return integer The ID of the created media gallery entry
216
     */
217
    protected function getParentValueId()
218
    {
219
        return $this->getSubject()->getParentValueId();
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 getParentValueId() does only exist in the following implementations of said interface: TechDivision\Import\Prod...a\Subjects\MediaSubject.

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...
220
    }
221
222
    /**
223
     * Return's the store for the passed store code.
224
     *
225
     * @param string $storeCode The store code to return the store for
226
     *
227
     * @return array The requested store
228
     * @throws \Exception Is thrown, if the requested store is not available
229
     */
230
    protected function getStoreByStoreCode($storeCode)
231
    {
232
        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...a\Subjects\MediaSubject.

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...
233
    }
234
235
    /**
236
     * Returns the acutal value of the position counter and raise's it by one.
237
     *
238
     * @return integer The actual value of the position counter
239
     */
240
    protected function raisePositionCounter()
241
    {
242
        return $this->getSubject()->raisePositionCounter();
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 raisePositionCounter() does only exist in the following implementations of said interface: TechDivision\Import\Prod...a\Subjects\MediaSubject.

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...
243
    }
244
245
    /**
246
     * Persist's the passed product media gallery value data.
247
     *
248
     * @param array $productMediaGalleryValue The product media gallery value data to persist
249
     *
250
     * @return void
251
     */
252
    protected function persistProductMediaGalleryValue($productMediaGalleryValue)
253
    {
254
        $this->getProductMediaProcessor()->persistProductMediaGalleryValue($productMediaGalleryValue);
255
    }
256
}
257