Completed
Push — master ( 29e5ba...ffb5c1 )
by Tim
06:10 queued 04:28
created

ProductUrlRewriteObserver::processImages()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 47
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 47
rs 8.6845
c 0
b 0
f 0
cc 4
eloc 25
nc 4
nop 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A ProductUrlRewriteObserver::getStoreViewCodesByWebsiteCode() 0 4 1
1
<?php
2
3
/**
4
 * TechDivision\Import\Product\UrlRewrite\Observers\ProductUrlRewriteObserver
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-url-rewrite
18
 * @link      http://www.techdivision.com
19
 */
20
21
namespace TechDivision\Import\Product\UrlRewrite\Observers;
22
23
use TechDivision\Import\Utils\StoreViewCodes;
24
use TechDivision\Import\Product\UrlRewrite\Utils\ColumnKeys;
25
use TechDivision\Import\Product\Observers\AbstractProductImportObserver;
26
27
/**
28
 * Observer that extracts the URL rewrite data to a specific CSV file.
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-url-rewrite
34
 * @link      http://www.techdivision.com
35
 */
36
class ProductUrlRewriteObserver extends AbstractProductImportObserver
37
{
38
39
    /**
40
     * The artefact type.
41
     *
42
     * @var string
43
     */
44
    const ARTEFACT_TYPE = 'url-rewrite';
45
46
    /**
47
     * The image artefacts that has to be exported.
48
     *
49
     * @var array
50
     */
51
    protected $artefacts = array();
52
53
    /**
54
     * Process the observer's business logic.
55
     *
56
     * @return array The processed row
57
     */
58
    protected function process()
59
    {
60
61
        // initialize the array for the artefacts and the store view codes
62
        $this->artefacts = array();
63
        $storeViewCodes = array();
64
65
        // load the SKU from the row
66
        $sku = $this->getValue(ColumnKeys::SKU);
67
68
        // prepare the store view code
69
        $this->getSubject()->prepareStoreViewCode();
70
71
        // try to load the store view code
72
        $storeViewCode = $this->getSubject()->getStoreViewCode(StoreViewCodes::ADMIN);
73
74
        // query whether or not we've a store view code
75
        if ($storeViewCode === StoreViewCodes::ADMIN) {
76
            // if not, load the websites the product is related with
77
            $websiteCodes = $this->getValue(ColumnKeys::PRODUCT_WEBSITES, array(), array($this, 'extract'));
78
79
            // load the store view codes of all websites
80
            foreach ($websiteCodes as $websiteCode) {
81
                $storeViewCodes = array_merge($storeViewCodes, $this->getStoreViewCodesByWebsiteCode($websiteCode));
82
            }
83
84
        } else {
85
            array_push($storeViewCodes, $storeViewCode);
86
        }
87
88
        // iterate over the available image fields
89
        foreach ($storeViewCodes as $storeViewCode) {
90
            // prepare the new base image
91
            $artefact = $this->newArtefact(
92
                array(
93
                    ColumnKeys::SKU                => $sku,
94
                    ColumnKeys::STORE_VIEW_CODE    => $storeViewCode
95
                ),
96
                array(
97
                    ColumnKeys::SKU                => ColumnKeys::SKU,
98
                    ColumnKeys::STORE_VIEW_CODE    => ColumnKeys::STORE_VIEW_CODE
99
                )
100
            );
101
102
            // append the base image to the artefacts
103
            $this->artefacts[] = $artefact;
104
        }
105
106
        // append the artefacts that has to be exported to the subject
107
        $this->addArtefacts($this->artefacts);
108
    }
109
110
    /**
111
     * Returns an array with the codes of the store views related with the passed website code.
112
     *
113
     * @param string $websiteCode The code of the website to return the store view codes for
114
     *
115
     * @return array The array with the matching store view codes
116
     */
117
    protected function getStoreViewCodesByWebsiteCode($websiteCode)
118
    {
119
        return $this->getSubject()->getStoreViewCodesByWebsiteCode($websiteCode);
0 ignored issues
show
Bug introduced by
The method getStoreViewCodesByWebsiteCode() does not exist on TechDivision\Import\Subjects\SubjectInterface. Did you maybe mean getStoreViewCode()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
120
    }
121
122
    /**
123
     * Create's and return's a new empty artefact entity.
124
     *
125
     * @param array $columns             The array with the column data
126
     * @param array $originalColumnNames The array with a mapping from the old to the new column names
127
     *
128
     * @return array The new artefact entity
129
     */
130
    protected function newArtefact(array $columns, array $originalColumnNames)
131
    {
132
        return $this->getSubject()->newArtefact($columns, $originalColumnNames);
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 newArtefact() does only exist in the following implementations of said interface: TechDivision\Import\Plugins\ExportableSubjectImpl, TechDivision\Import\Product\Subjects\BunchSubject, TechDivision\Import\Subjects\ExportableTraitImpl.

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...
133
    }
134
135
    /**
136
     * Add the passed product type artefacts to the product with the
137
     * last entity ID.
138
     *
139
     * @param array $artefacts The product type artefacts
140
     *
141
     * @return void
142
     * @uses \TechDivision\Import\Product\Media\Subjects\MediaSubject::getLastEntityId()
143
     */
144
    protected function addArtefacts(array $artefacts)
145
    {
146
        $this->getSubject()->addArtefacts(ProductUrlRewriteObserver::ARTEFACT_TYPE, $artefacts);
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 addArtefacts() does only exist in the following implementations of said interface: TechDivision\Import\Plugins\ExportableSubjectImpl, TechDivision\Import\Product\Subjects\BunchSubject, TechDivision\Import\Subjects\ExportableTraitImpl.

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...
147
    }
148
}
149