Completed
Push — master ( 23587a...d53256 )
by Tim
8s
created

prepareStockStatusAttributes()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 18
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 18
ccs 8
cts 8
cp 1
rs 9.4285
cc 3
eloc 8
nc 2
nop 0
crap 3

1 Method

Rating   Name   Duplication   Size   Complexity  
A ProductInventoryObserver::initializeStockItem() 0 4 1
1
<?php
2
3
/**
4
 * TechDivision\Import\Product\Observers\ProductInventoryObserver
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\Observers\AttributeLoaderInterface;
26
use TechDivision\Import\Observers\DynamicAttributeObserverInterface;
27
use TechDivision\Import\Product\Services\ProductBunchProcessorInterface;
28
use TechDivision\Import\Utils\BackendTypeKeys;
29
30
/**
31
 * Observer that creates/updates the product's inventory.
32
 *
33
 * @author    Tim Wagner <[email protected]>
34
 * @copyright 2016 TechDivision GmbH <[email protected]>
35
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
36
 * @link      https://github.com/techdivision/import-product
37
 * @link      http://www.techdivision.com
38
 */
39
class ProductInventoryObserver extends AbstractProductImportObserver implements DynamicAttributeObserverInterface
40
{
41
42
    /**
43
     * The product bunch processor instance.
44
     *
45
     * @var \TechDivision\Import\Product\Services\ProductBunchProcessorInterface
46
     */
47
    protected $productBunchProcessor;
48
49
    /**
50
     * The attribute loader instance.
51
     *
52
     * @var \TechDivision\Import\Observers\AttributeLoaderInterface
53
     */
54
    protected $attributeLoader;
55
56
    /**
57
     * Initialize the observer with the passed product bunch processor instance.
58
     *
59
     * @param \TechDivision\Import\Product\Services\ProductBunchProcessorInterface $productBunchProcessor The product bunch processor instance
60
     * @param \TechDivision\Import\Observers\AttributeLoaderInterface              $attributeLoader       The attribute loader instance
61
     */
62 2
    public function __construct(
63
        ProductBunchProcessorInterface $productBunchProcessor,
64
        AttributeLoaderInterface $attributeLoader
65
    ) {
66 2
        $this->productBunchProcessor = $productBunchProcessor;
67 2
        $this->attributeLoader = $attributeLoader;
68 2
    }
69
70
    /**
71
     * Return's the product bunch processor instance.
72
     *
73
     * @return \TechDivision\Import\Product\Services\ProductBunchProcessorInterface The product bunch processor instance
74
     */
75 1
    protected function getProductBunchProcessor()
76
    {
77 1
        return $this->productBunchProcessor;
78
    }
79
80
    /**
81
     * Process the observer's business logic.
82
     *
83
     * @return array The processed row
84
     */
85 2
    protected function process()
86
    {
87
88
        // query whether or not, we've found a new SKU => means we've found a new product
89 2
        if ($this->hasBeenProcessed($this->getValue(ColumnKeys::SKU))) {
90 1
            return;
91
        }
92
93
        // prepare, initialize and persist the stock status/item
94 1
        $this->persistStockItem($this->initializeStockItem($this->prepareStockItemAttributes()));
95 1
    }
96
97
    /**
98
     * Prepare the basic attributes of the stock status/item entity that has to be persisted.
99
     *
100
     * @return array The prepared stock status/item attributes
101
     */
102 1
    protected function prepareAttributes()
103
    {
104
105
        // load the ID of the product that has been created recently
106 1
        $lastEntityId = $this->getSubject()->getLastEntityId();
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 getLastEntityId() does only exist in the following implementations of said interface: TechDivision\Import\Observers\EntitySubjectImpl, TechDivision\Import\Plugins\ExportableSubjectImpl, TechDivision\Import\Prod...\AbstractProductSubject, 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...
107
108
        // initialize the stock status data
109 1
        $websiteId =  $this->getValue(ColumnKeys::WEBSITE_ID, 0);
110
111
        // return the prepared stock status
112 1
        return $this->initializeEntity(
113
            array(
114 1
                MemberNames::PRODUCT_ID   => $lastEntityId,
115 1
                MemberNames::WEBSITE_ID   => $websiteId,
116 1
                MemberNames::STOCK_ID     => 1
117
            )
118
        );
119
    }
120
121
    /**
122
     * Prepare the stock item attributes of the entity that has to be persisted.
123
     *
124
     * @return array The prepared stock status item
125
     */
126 1
    protected function prepareStockItemAttributes()
127
    {
128 1
        return array_merge($this->prepareAttributes(), $this->attributeLoader->load($this, $this->getHeaderStockMappings()));
129
    }
130
131
    /**
132
     * Initialize the stock item with the passed attributes and returns an instance.
133
     *
134
     * @param array $attr The stock item attributes
135
     *
136
     * @return array The initialized stock item
137
     */
138 1
    protected function initializeStockItem(array $attr)
139
    {
140 1
        return $attr;
141
    }
142
143
    /**
144
     * Return's the appings for the table column => CSV column header.
145
     *
146
     * @return array The header stock mappings
147
     */
148 1
    protected function getHeaderStockMappings()
149
    {
150 1
        return $this->getSubject()->getHeaderStockMappings();
0 ignored issues
show
Bug introduced by
The method getHeaderStockMappings() does not exist on TechDivision\Import\Subjects\SubjectInterface. Did you maybe mean getHeader()?

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...
151
    }
152
153
    /**
154
     * Persist's the passed stock item data and return's the ID.
155
     *
156
     * @param array $stockItem The stock item data to persist
157
     *
158
     * @return void
159
     */
160 1
    protected function persistStockItem($stockItem)
161
    {
162 1
        $this->getProductBunchProcessor()->persistStockItem($stockItem);
163 1
    }
164
}
165