Completed
Pull Request — master (#100)
by Tim
05:44
created

PreLoadEntityIdObserver::loadProduct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
/**
4
 * TechDivision\Import\Product\Observers\PreLoadEntityIdObserver
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\Services\ProductBunchProcessorInterface;
24
use TechDivision\Import\Product\Utils\ColumnKeys;
25
26
/**
27
 * Observer that pre-loads the entity ID of the product with the SKU found in the CSV file.
28
 *
29
 * @author    Tim Wagner <[email protected]>
30
 * @copyright 2016 TechDivision GmbH <[email protected]>
31
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
32
 * @link      https://github.com/techdivision/import-product
33
 * @link      http://www.techdivision.com
34
 */
35
class PreLoadEntityIdObserver extends AbstractProductImportObserver
36
{
37
38
    /**
39
     * The product bunch processor instance.
40
     *
41
     * @var \TechDivision\Import\Product\Services\ProductBunchProcessorInterface
42
     */
43
    protected $productBunchProcessor;
44
45
    /**
46
     * Initialize the observer with the passed product bunch processor instance.
47
     *
48
     * @param \TechDivision\Import\Product\Services\ProductBunchProcessorInterface $productBunchProcessor The product bunch processor instance
49
     */
50 3
    public function __construct(ProductBunchProcessorInterface $productBunchProcessor)
51
    {
52 3
        $this->productBunchProcessor = $productBunchProcessor;
53 3
    }
54
55
    /**
56
     * Return's the product bunch processor instance.
57
     *
58
     * @return \TechDivision\Import\Product\Services\ProductBunchProcessorInterface The product bunch processor instance
59
     */
60 2
    protected function getProductBunchProcessor()
61
    {
62 2
        return $this->productBunchProcessor;
63
    }
64
65
    /**
66
     * Process the observer's business logic.
67
     *
68
     * @return array The processed row
69
     * @throws \Exception Is thrown, if the product with the SKU can not be loaded
70
     */
71 3
    protected function process()
72
    {
73
74
        // query whether or not, we've found a new SKU => means we've found a new product
75 3
        if ($this->isLastSku($sku = $this->getValue(ColumnKeys::SKU))) {
76 1
            return;
77
        }
78
79
        // preserve the entity ID for the product with the passed SKU
80 2
        if ($product = $this->loadProduct($sku)) {
81 1
            $this->preLoadEntityId($product);
82 1
        } else {
83
            // initialize the error message
84 1
            $message = sprintf('Can\'t pre-load product with SKU %s', $sku);
85
            // load the subject
86 1
            $subject = $this->getSubject();
87
            // query whether or not debug mode has been enabled
88 1
            if ($subject->isDebugMode()) {
89
                $subject->getSystemLogger()->warning($subject->appendExceptionSuffix($message));
90
            } else {
91 1
                throw new \Exception($message);
92
            }
93
        }
94 1
    }
95
96
    /**
97
     * Pre-load the entity ID for the passed product.
98
     *
99
     * @param array $product The product to be pre-loaded
100
     *
101
     * @return void
102
     */
103 1
    protected function preLoadEntityId(array $product)
104
    {
105 1
        $this->getSubject()->preLoadEntityId($product);
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 preLoadEntityId() does only exist in the following implementations of said interface: 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...
106 1
    }
107
108
    /**
109
     * Load's and return's the product with the passed SKU.
110
     *
111
     * @param string $sku The SKU of the product to load
112
     *
113
     * @return array The product
114
     */
115 2
    protected function loadProduct($sku)
116
    {
117 2
        return $this->getProductBunchProcessor()->loadProduct($sku);
118
    }
119
}
120