Completed
Pull Request — master (#79)
by Tim
02:50
created

storeViewHasBeenProcessed()   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 2
crap 2
1
<?php
2
3
/**
4
 * TechDivision\Import\Product\Observers\ProductAttributeObserver
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\Observers\AbstractAttributeObserver;
24
use TechDivision\Import\Product\Services\ProductBunchProcessorInterface;
25
use TechDivision\Import\Product\Utils\ColumnKeys;
26
27
/**
28
 * Observer that creates/updates the product's attributes.
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
34
 * @link      http://www.techdivision.com
35
 */
36
class ProductAttributeObserver extends AbstractAttributeObserver
37
{
38
39
    /**
40
     * The product bunch processor instance.
41
     *
42
     * @var \TechDivision\Import\Product\Services\ProductBunchProcessorInterface
43
     */
44
    protected $productBunchProcessor;
45
46
    /**
47
     * Initialize the observer with the passed product bunch processor instance.
48
     *
49
     * @param \TechDivision\Import\Product\Services\ProductBunchProcessorInterface $productBunchProcessor The product bunch processor instance
50
     */
51
    public function __construct(ProductBunchProcessorInterface $productBunchProcessor)
52
    {
53
        $this->productBunchProcessor = $productBunchProcessor;
54
    }
55
56
    /**
57
     * Return's the product bunch processor instance.
58
     *
59
     * @return \TechDivision\Import\Product\Services\ProductBunchProcessorInterface The product bunch processor instance
60
     */
61
    protected function getProductBunchProcessor()
62
    {
63
        return $this->productBunchProcessor;
64
    }
65
66
    /**
67
     * Return's the column name that contains the primary key.
68
     *
69
     * @return string the column name that contains the primary key
70
     */
71
    protected function getPrimaryKeyColumnName()
72
    {
73
        return ColumnKeys::SKU;
74
    }
75
76
    /**
77
     * Queries whether or not the passed PK and store view code has already been processed.
78
     *
79
     * @param string $pk            The PK to check been processed
80
     * @param string $storeViewCode The store view code to check been processed
81
     *
82
     * @return boolean TRUE if the PK and store view code has been processed, else FALSE
83
     */
84
    protected function storeViewHasBeenProcessed($pk, $storeViewCode)
85
    {
86
        return $this->getSubject()->storeViewHasBeenProcessed($pk, $storeViewCode);
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 storeViewHasBeenProcessed() 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...
87
    }
88
89
    /**
90
     * Persist's the passed varchar attribute.
91
     *
92
     * @param array $attribute The attribute to persist
93
     *
94
     * @return void
95
     */
96
    protected function persistVarcharAttribute($attribute)
97
    {
98
        $this->getProductBunchProcessor()->persistProductVarcharAttribute($attribute);
99
    }
100
101
    /**
102
     * Persist's the passed integer attribute.
103
     *
104
     * @param array $attribute The attribute to persist
105
     *
106
     * @return void
107
     */
108
    protected function persistIntAttribute($attribute)
109
    {
110
        $this->getProductBunchProcessor()->persistProductIntAttribute($attribute);
111
    }
112
113
    /**
114
     * Persist's the passed decimal attribute.
115
     *
116
     * @param array $attribute The attribute to persist
117
     *
118
     * @return void
119
     */
120
    protected function persistDecimalAttribute($attribute)
121
    {
122
        $this->getProductBunchProcessor()->persistProductDecimalAttribute($attribute);
123
    }
124
125
    /**
126
     * Persist's the passed datetime attribute.
127
     *
128
     * @param array $attribute The attribute to persist
129
     *
130
     * @return void
131
     */
132
    protected function persistDatetimeAttribute($attribute)
133
    {
134
        $this->getProductBunchProcessor()->persistProductDatetimeAttribute($attribute);
135
    }
136
137
    /**
138
     * Persist's the passed text attribute.
139
     *
140
     * @param array $attribute The attribute to persist
141
     *
142
     * @return void
143
     */
144
    protected function persistTextAttribute($attribute)
145
    {
146
        $this->getProductBunchProcessor()->persistProductTextAttribute($attribute);
147
    }
148
}
149