Issues (5)

src/Observers/ConvertDecimalsObserver.php (3 issues)

Labels
Severity
1
<?php
2
3
/**
4
 * TechDivision\Import\Converter\ConvertDecimalsObserver
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-converter
18
 * @link      http://www.techdivision.com
19
 */
20
21
namespace TechDivision\Import\Converter\Observers;
22
23
use TechDivision\Import\Utils\MemberNames;
24
use TechDivision\Import\Utils\BackendTypeKeys;
25
26
/**
27
 * Observer implementation that converts decimal attributes to the Magento
28
 * specific decimal format.
29
 *
30
 * @author    Tim Wagner <[email protected]>
31
 * @copyright 2016 TechDivision GmbH <[email protected]>
32
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
33
 * @link      https://github.com/techdivision/import-converter
34
 * @link      http://www.techdivision.com
35
 */
36
class ConvertDecimalsObserver extends AbstractConverterObserver
37
{
38
39
    /**
40
     * Process the observer's business logic.
41
     *
42
     * @return void
43
     */
44
    protected function process()
45
    {
46
47
        // convert all values columns that match an attribute with the backend
48
        // type 'decimal' from the specified source to the target format
49
        foreach ($this->getColumnNamesToConvert() as $columnName) {
50
            // convert the value, if the attribute code match
51
            if ($this->hasValue($columnName)) {
0 ignored issues
show
$columnName of type array is incompatible with the type string expected by parameter $name of TechDivision\Import\Obse...actObserver::hasValue(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

51
            if ($this->hasValue(/** @scrutinizer ignore-type */ $columnName)) {
Loading history...
52
                $this->setValue($columnName, $this->getNumberConverter()->convert($this->getValue($columnName, null, function ($value) {
0 ignored issues
show
$columnName of type array is incompatible with the type string expected by parameter $name of TechDivision\Import\Obse...actObserver::getValue(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

52
                $this->setValue($columnName, $this->getNumberConverter()->convert($this->getValue(/** @scrutinizer ignore-type */ $columnName, null, function ($value) {
Loading history...
$columnName of type array is incompatible with the type string expected by parameter $name of TechDivision\Import\Obse...actObserver::setValue(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

52
                $this->setValue(/** @scrutinizer ignore-type */ $columnName, $this->getNumberConverter()->convert($this->getValue($columnName, null, function ($value) {
Loading history...
53
                    return trim($value);
54
                })));
55
            }
56
        }
57
    }
58
59
    /**
60
     * Returns the column names that has to be converted.
61
     *
62
     * @return array[] The column names
63
     */
64
    protected function getColumnNamesToConvert()
65
    {
66
67
        // initialize the array with the column names
68
        $columnNames = array();
69
70
        // load the EAV attributes with the backend type decimal
71
        foreach ($this->getEavAttributesByBackendType(BackendTypeKeys::BACKEND_TYPE_DECIMAL) as $attribute) {
72
            $columnNames[] = $attribute[MemberNames::ATTRIBUTE_CODE];
73
        }
74
75
        // return the column names
76
        return $columnNames;
77
    }
78
79
    /**
80
     * Returns the attributes with the passed backend type.
81
     *
82
     * @param string $backendType The backend type to return the attributes for
83
     *
84
     * @return array The attributes with the matching backend type
85
     */
86
    protected function getEavAttributesByBackendType($backendType)
87
    {
88
        return array_filter($this->getSubject()->getAttributes(), function ($attribute) use ($backendType) {
89
            return $attribute[MemberNames::BACKEND_TYPE] === $backendType;
90
        });
91
    }
92
93
    /**
94
     * Returns the number converter to use.
95
     *
96
     * @return \TechDivision\Import\Subjects\I18n\NumberConverterInterface The number converter instance
97
     */
98
    protected function getNumberConverter()
99
    {
100
        return $this->getSubject()->getNumberConverter();
101
    }
102
}
103