ProductUpdateAttributeWriter   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 4
c 0
b 0
f 0
lcom 1
cbo 5
dl 0
loc 41
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A writeItem() 0 19 3
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