ProductUpdater::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 12
rs 9.4285
cc 1
eloc 9
nc 1
nop 4
1
<?php
2
3
namespace DefaultValue\Bundle\AkeneoInlineEditBundle\Updater;
4
5
use Pim\Bundle\CatalogBundle\Doctrine\ORM\Repository\ProductRepository;
6
use Akeneo\Component\StorageUtils\Updater\PropertySetterInterface;
7
use Pim\Bundle\CatalogBundle\Doctrine\Common\Saver\ProductSaver;
8
use DefaultValue\Bundle\AkeneoInlineEditBundle\Product\ProductAttributeHelper;
9
10
/**
11
 * Service which updates product attribute value
12
 */
13
class ProductUpdater
14
{
15
    const DEFAULT_PRODUCT_CURRENCY = 'USD';
16
17
    /**
18
     * @var ProductAttributeHelper
19
     */
20
    private $attributeHelper;
21
22
    /**
23
     * @var ProductRepository
24
     */
25
    private $productRepository;
26
27
    /**
28
     * @var PropertySetterInterface
29
     */
30
    private $productPropertySetter;
31
32
    /**
33
     * @var ProductSaver
34
     */
35
    private $productSaver;
36
37
    /**
38
     * @var
39
     */
40
    private $updateInfo;
41
42
    /**
43
     * @param ProductAttributeHelper  $attributeHelper
44
     * @param ProductRepository       $productRepository
45
     * @param PropertySetterInterface $productPropertySetter
46
     * @param ProductSaver            $productSaver
47
     */
48
    public function __construct(
49
        ProductAttributeHelper $attributeHelper,
50
        ProductRepository $productRepository,
51
        PropertySetterInterface $productPropertySetter,
52
        ProductSaver $productSaver
53
    )
54
    {
55
        $this->attributeHelper       = $attributeHelper;
56
        $this->productRepository     = $productRepository;
57
        $this->productPropertySetter = $productPropertySetter;
58
        $this->productSaver          = $productSaver;
59
    }
60
61
    /**
62
     * Update product attribute value
63
     *
64
     * @param $productId
65
     * @param $attribute
66
     * @param $attributeValue
67
     * @param $dataLocale
68
     * @param $scopeCode
69
     * @return bool
70
     */
71
    public function update($productId, $attribute, $attributeValue, $dataLocale, $scopeCode)
72
    {
73
        $attributeHelper = $this->attributeHelper;
74
        $localizableAttributes = $attributeHelper->getLocalizableAttributes();
75
        $scopableAttributes = $attributeHelper->getScopableAttributes();
76
77
        $locale = null;
78
        $scope = null;
79
        if (in_array($attribute, $localizableAttributes)) $locale = $dataLocale; // check if attribute localizable
80
        if (in_array($attribute, $scopableAttributes)) $scope = $scopeCode; // check if attribute scopable
81
82
        $attributeValue = $this->prepareAttributeValue($attribute, $attributeValue);
83
        $product = $this->productRepository->getFullProduct($productId);
84
85
        try {
86
            $this->productPropertySetter->setData( // set attribute value
87
                $product,
88
                $attribute,
89
                $attributeValue,
90
                [
91
                    'locale' => $locale,
92
                    'scope'  => $scope
93
                ]
94
            );
95
96
            $this->productSaver->save($product);
97
            $updateInfo = sprintf('Product "%s" %s', $attribute, 'attribute value is successfully changed');
98
            $this->setUpdateInfo($updateInfo);
99
        } catch (\Exception $e) {
100
            $updateInfo = sprintf('Product "%s" %s. %s', $attribute, 'attribute value wasn\'t changed', $e->getMessage());
101
            $this->setUpdateInfo($updateInfo);
102
            return false;
103
        }
104
105
        return true;
106
    }
107
108
    /**
109
     * @return mixed
110
     */
111
    public function getUpdateInfo()
112
    {
113
        return $this->updateInfo;
114
    }
115
116
    /**
117
     * @param mixed $updateInfo
118
     */
119
    public function setUpdateInfo($updateInfo)
120
    {
121
        $this->updateInfo = $updateInfo;
122
    }
123
124
    /**
125
     * @param $attribute
126
     * @param $attributeValue
127
     * @return array
128
     */
129
    protected function prepareAttributeValue($attribute, $attributeValue)
130
    {
131
        $priceAttributes = $this->attributeHelper->getPriceAttributes();
132
        if (in_array($attribute, $priceAttributes)) {
133
            $data = str_replace(" $", "", $attributeValue);
134
            $attributeValue = [[
135
                'data'      => $data,
136
                'currency'  => static::DEFAULT_PRODUCT_CURRENCY
137
            ]];
138
        }
139
140
        return $attributeValue;
141
    }
142
}
143