Completed
Push — master ( 10f0b5...3dd64b )
by Tim
13s
created

ProductWebsiteObserver::persistProductWebsite()   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
rs 10
c 0
b 0
f 0
ccs 0
cts 0
cp 0
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
3
/**
4
 * TechDivision\Import\Product\Observers\ProductWebsiteObserver
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
18
 * @link      http://www.techdivision.com
19
 */
20
21
namespace TechDivision\Import\Product\Observers;
22
23
use TechDivision\Import\Product\Utils\ColumnKeys;
24
use TechDivision\Import\Product\Utils\MemberNames;
25
use TechDivision\Import\Product\Services\ProductBunchProcessorInterface;
26
use TechDivision\Import\Product\Observers\AbstractProductImportObserver;
27
28
/**
29
 * Observer that creates/updates the product's website relations.
30
 *
31
 * @author    Tim Wagner <[email protected]>
32
 * @copyright 2016 TechDivision GmbH <[email protected]>
33
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
34
 * @link      https://github.com/techdivision/import-product
35
 * @link      http://www.techdivision.com
36
 */
37
class ProductWebsiteObserver extends AbstractProductImportObserver
38
{
39
40
    /**
41
     * The product bunch processor instance.
42
     *
43
     * @var \TechDivision\Import\Product\Services\ProductBunchProcessorInterface
44
     */
45
    protected $productBunchProcessor;
46
47
    /**
48
     * Initialize the observer with the passed product bunch processor instance.
49
     *
50
     * @param \TechDivision\Import\Product\Services\ProductBunchProcessorInterface $productBunchProcessor The product bunch processor instance
51
     */
52
    public function __construct(ProductBunchProcessorInterface $productBunchProcessor)
53
    {
54
        $this->productBunchProcessor = $productBunchProcessor;
55
    }
56
57
    /**
58
     * Return's the product bunch processor instance.
59
     *
60
     * @return \TechDivision\Import\Services\ProductBunchProcessorInterface The product bunch processor instance
61
     */
62
    protected function getProductBunchProcessor()
63
    {
64
        return $this->productBunchProcessor;
65
    }
66
67
    /**
68
     * The actual website code that has to be processed.
69
     *
70
     * @var string
71
     */
72
    protected $code;
73 1
74
    /**
75
     * Set's the actual website code that has to be processed.
76
     *
77 1
     * @param string $code The website code
78
     *
79
     * @return void
80
     */
81
    protected function setCode($code)
82 1
    {
83 1
        $this->code = $code;
84
    }
85
86
    /**
87
     * Return's the webiste code that has to be processed.
88
     *
89
     * @return string The website code
90
     */
91
    protected function getCode()
92
    {
93
        return $this->code;
94
    }
95
96
    /**
97
     * Process the observer's business logic.
98
     *
99
     * @return array The processed row
100
     */
101
    protected function process()
102
    {
103
104
        // query whether or not, we've found a new SKU => means we've found a new product
105
        if ($this->hasBeenProcessed($this->getValue(ColumnKeys::SKU))) {
106
            return;
107
        }
108
109
        // query whether or not, product => website relations has been specified
110
        if (!$this->hasValue(ColumnKeys::PRODUCT_WEBSITES)) {
111
            return;
112
        }
113
114
        // append the product => website relations found
115
        $codes = $this->getValue(ColumnKeys::PRODUCT_WEBSITES, array(), array($this, 'explode'));
116
        foreach ($codes as $code) {
117
            // set the code of the website that has to be processed
118
            $this->setCode($code);
119
            // prepare the product website relation attributes
120
            $attr = $this->prepareAttributes();
121
122
            try {
123
                // create the product website relation
124
                $productWebsite = $this->initializeProductWebsite($attr);
125
                $this->persistProductWebsite($productWebsite);
126
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
127
            } catch (\RuntimeException $re) {
128
                $this->getSystemLogger()->debug($re->getMessage());
129
            }
130
        }
131
    }
132
133
    /**
134
     * Prepare the attributes of the entity that has to be persisted.
135
     *
136
     * @return array The prepared attributes
137
     */
138
    protected function prepareAttributes()
139
    {
140
141
        // load the ID of the product that has been created recently
142
        $lastEntityId = $this->getLastEntityId();
143
144
        // load the website ID to relate the product with
145
        $websiteId = $this->getStoreWebsiteIdByCode($this->getCode());
146
147
        // return the prepared product
148
        return $this->initializeEntity(
149
            array(
150
                MemberNames::PRODUCT_ID => $lastEntityId,
151
                MemberNames::WEBSITE_ID => $websiteId
152
            )
153
        );
154
    }
155
156
    /**
157
     * Initialize the product website with the passed attributes and returns an instance.
158
     *
159
     * @param array $attr The product website attributes
160
     *
161
     * @return array The initialized product website
162
     * @throws \RuntimeException Is thrown, if the attributes can not be initialized
163
     */
164
    protected function initializeProductWebsite(array $attr)
165
    {
166
        return $attr;
167
    }
168
169
    /**
170
     * Persist's the passed product website data and return's the ID.
171
     *
172
     * @param array $productWebsite The product website data to persist
173
     *
174
     * @return void
175
     */
176
    protected function persistProductWebsite($productWebsite)
177
    {
178
        $this->getProductBunchProcessor()->persistProductWebsite($productWebsite);
179
    }
180
181
    /**
182
     * Return's the store website for the passed code.
183
     *
184
     * @param string $code The code of the store website to return the ID for
185
     *
186
     * @return integer The store website ID
187
     */
188
    protected function getStoreWebsiteIdByCode($code)
189
    {
190
        return $this->getSubject()->getStoreWebsiteIdByCode($code);
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 getStoreWebsiteIdByCode() does only exist in the following implementations of said interface: TechDivision\Import\Prod...\AbstractProductSubject, TechDivision\Import\Product\Subjects\BunchSubject.

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...
191
    }
192
}
193