ProductUpdateAttributeWriter::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
namespace Jh\DataImportMagento\Writer\Product;
3
4
use Ddeboer\DataImport\Exception\WriterException;
5
use Ddeboer\DataImport\Writer\AbstractWriter;
6
use Jh\DataImportMagento\Exception\MagentoSaveException;
7
8
/**
9
 * Class ProductUpdateAttributeWriter
10
 * @author Adam Paterson <[email protected]>
11
 * @package Jh\DataImportMagento\Writer\Product
12
 */
13
class ProductUpdateAttributeWriter extends AbstractWriter
14
{
15
    /**
16
     * @var \Mage_Catalog_Model_Product
17
     */
18
    protected $productModel;
19
20
    /**
21
     * @param \Mage_Catalog_Model_Product $productModel
22
     */
23
    public function __construct(\Mage_Catalog_Model_Product $productModel)
24
    {
25
        $this->productModel = $productModel;
26
    }
27
28
    /**
29
     * Write item if product exists
30
     *
31
     * @param array $item
32
     * @throws \Jh\DataImportMagento\Exception\MagentoSaveException
33
     */
34
    public function writeItem(array $item)
35
    {
36
        $productModel = clone $this->productModel;
37
        $sku          = $item['sku'];
38
        $product      = $productModel->loadByAttribute('sku', $sku);
39
40
        if (!$product) {
41
            throw new WriterException(sprintf('Product with SKU: %s does not exist in Magento', $sku));
42
        }
43
44
        $product->addData($item);
45
46
        try {
47
            $product->save();
48
        } catch (\Mage_Core_Exception $e) {
49
            $message = $e->getMessage();
50
            throw new MagentoSaveException($message);
51
        }
52
    }
53
}
54