TaxClassValueConverter::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 2
eloc 5
nc 2
nop 0
1
<?php
2
3
namespace Jh\DataImportMagento\ValueConverter;
4
5
use Ddeboer\DataImport\Exception\UnexpectedTypeException;
6
use Ddeboer\DataImport\Exception\UnexpectedValueException;
7
use Ddeboer\DataImport\ValueConverter\ValueConverterInterface;
8
9
/**
10
 * Class TaxClassValueConverter
11
 * @package Jh\DataImportMagento\ValueConverter
12
 * @author Aydin Hassan <[email protected]>
13
 */
14
class TaxClassValueConverter implements ValueConverterInterface
15
{
16
17
    /**
18
     * @var array
19
     */
20
    private $taxClasses = [];
21
22
    /**
23
     * @var string
24
     */
25
    private $default = 'Taxable Goods';
26
27
    /**
28
     *  Get the Tax Classes
29
     */
30
    public function __construct()
31
    {
32
        $model = \Mage::getSingleton('tax/class_source_product');
33
        $productTaxClassOptions = $model->getAllOptions();
34
        foreach ($productTaxClassOptions as $option) {
35
            $this->taxClasses[$option['value']] = $option['label'];
36
        }
37
    }
38
39
    /**
40
     * @param string $input
41
     * @return string
42
     */
43
    public function convert($input)
44
    {
45
        if (empty($input)) {
46
            $input = $this->default;
47
        }
48
49
        if (!in_array($input, $this->taxClasses)) {
50
            throw new UnexpectedValueException(
51
                sprintf(
52
                    'Given Tax-Class: "%s" is not valid. Allowed values: "%s"',
53
                    $input,
54
                    implode('", "', $this->taxClasses)
55
                )
56
            );
57
        }
58
59
        return array_search($input, $this->taxClasses);
60
    }
61
}
62