Code Duplication    Length = 85-95 lines in 2 locations

src/Observers/LastEntityIdObserver.php 1 location

@@ 36-130 (lines=95) @@
33
 * @link      https://github.com/techdivision/import-product
34
 * @link      http://www.techdivision.com
35
 */
36
class LastEntityIdObserver extends AbstractProductImportObserver
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
     * Process the observer's business logic.
68
     *
69
     * @return array The processed row
70
     * @throws \Exception Is thrown, if the product with the SKU can not be loaded
71
     */
72
    protected function process()
73
    {
74
75
        // query whether or not, we've found a new SKU => means we've found a new product
76
        if ($this->isLastSku($sku = $this->getValue(ColumnKeys::SKU))) {
77
            return;
78
        }
79
80
        // set the entity ID for the product with the passed SKU
81
        if ($product = $this->loadProduct($sku)) {
82
            $this->setIds($product);
83
        } else {
84
            // initialize the error message
85
            $message = sprintf('Can\'t load entity ID for product with SKU %s', $sku);
86
            // load the subject
87
            $subject = $this->getSubject();
88
            // query whether or not debug mode has been enabled
89
            if ($subject->isDebugMode()) {
90
                $subject->getSystemLogger()->warning($subject->appendExceptionSuffix($message));
91
            } else {
92
                throw new \Exception($message);
93
            }
94
        }
95
    }
96
97
    /**
98
     * Temporarily persist's the IDs of the passed product.
99
     *
100
     * @param array $product The product to temporarily persist the IDs for
101
     */
102
    protected function setIds(array $product)
103
    {
104
        $this->setLastEntityId($product[MemberNames::ENTITY_ID]);
105
    }
106
107
    /**
108
     * Set's the ID of the product that has been created recently.
109
     *
110
     * @param string $lastEntityId The entity ID
111
     *
112
     * @return void
113
     */
114
    protected function setLastEntityId($lastEntityId)
115
    {
116
        $this->getSubject()->setLastEntityId($lastEntityId);
117
    }
118
119
    /**
120
     * Load's and return's the product with the passed SKU.
121
     *
122
     * @param string $sku The SKU of the product to load
123
     *
124
     * @return array The product
125
     */
126
    protected function loadProduct($sku)
127
    {
128
        return $this->getProductBunchProcessor()->loadProduct($sku);
129
    }
130
}
131

src/Observers/PreLoadEntityIdObserver.php 1 location

@@ 35-119 (lines=85) @@
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
    public function __construct(ProductBunchProcessorInterface $productBunchProcessor)
51
    {
52
        $this->productBunchProcessor = $productBunchProcessor;
53
    }
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
    protected function getProductBunchProcessor()
61
    {
62
        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
    protected function process()
72
    {
73
74
        // query whether or not, we've found a new SKU => means we've found a new product
75
        if ($this->isLastSku($sku = $this->getValue(ColumnKeys::SKU))) {
76
            return;
77
        }
78
79
        // preserve the entity ID for the product with the passed SKU
80
        if ($product = $this->loadProduct($sku)) {
81
            $this->preLoadEntityId($product);
82
        } else {
83
            // initialize the error message
84
            $message = sprintf('Can\'t pre-load product with SKU %s', $sku);
85
            // load the subject
86
            $subject = $this->getSubject();
87
            // query whether or not debug mode has been enabled
88
            if ($subject->isDebugMode()) {
89
                $subject->getSystemLogger()->warning($subject->appendExceptionSuffix($message));
90
            } else {
91
                throw new \Exception($message);
92
            }
93
        }
94
    }
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
    protected function preLoadEntityId(array $product)
104
    {
105
        $this->getSubject()->preLoadEntityId($product);
106
    }
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
    protected function loadProduct($sku)
116
    {
117
        return $this->getProductBunchProcessor()->loadProduct($sku);
118
    }
119
}
120