Completed
Push — master ( 4142c5...662dc7 )
by Tim
12s
created

AbstractBooleanCallback::handle()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 50
Code Lines 24

Duplication

Lines 29
Ratio 58 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 29
loc 50
ccs 0
cts 39
cp 0
rs 9.3333
cc 3
eloc 24
nc 3
nop 2
crap 12
1
<?php
2
3
/**
4
 * TechDivision\Import\Callbacks\AbstractBooleanCallback
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-category
18
 * @link      http://www.techdivision.com
19
 */
20
21
namespace TechDivision\Import\Callbacks;
22
23
use TechDivision\Import\Utils\RegistryKeys;
24
25
/**
26
 * A callback implementation that converts the passed boolean value.
27
 *
28
 * @author    Tim Wagner <[email protected]>
29
 * @copyright 2016 TechDivision GmbH <[email protected]>
30
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
31
 * @link      https://github.com/techdivision/import-category
32
 * @link      http://www.techdivision.com
33
 */
34
abstract class AbstractBooleanCallback extends AbstractCallback
35
{
36
37
    /**
38
     * Array with the string => boolean mapping.
39
     *
40
     * @var array
41
     */
42
    protected $booleanValues = array(
43
        'true'  => 1,
44
        'yes'   => 1,
45
        '1'     => 1,
46
        'false' => 0,
47
        'no'    => 0,
48
        '0'     => 0
49
    );
50
51
    /**
52
     * Will be invoked by a observer it has been registered for.
53
     *
54
     * @param string $attributeCode  The code of the attribute the passed value is for
55
     * @param mixed  $attributeValue The value to handle
56
     *
57
     * @return mixed The modified value
58
     * @see \TechDivision\Import\Callbacks\CallbackInterface::handle()
59
     */
60
    public function handle($attributeCode, $attributeValue)
61
    {
62
63
        // query whether or not, the passed value can be mapped to a boolean representation
64
        if (isset($this->booleanValues[strtolower($attributeValue)])) {
65
            return (boolean) $this->booleanValues[strtolower($attributeValue)];
66
        }
67
68
        // query whether or not we're in debug mode
69 View Code Duplication
        if ($this->isDebugMode()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
70
            // log a warning and continue with the next value
71
            $this->getSystemLogger()->warning(
72
                $this->appendExceptionSuffix(
73
                    sprintf(
74
                        'Can\'t map option value "%s" for attribute %s to a boolean representation',
75
                        $attributeValue,
76
                        $attributeCode
77
                    )
78
                )
79
            );
80
81
            // add the missing option value to the registry
82
            $this->mergeAttributesRecursive(
83
                array(
84
                    RegistryKeys::MISSING_OPTION_VALUES => array(
85
                        $attributeCode => array(
86
                            $attributeValue => array(
87
                                $this->raiseCounter($attributeValue),
88
                                array($this->getUniqueIdentifier() => true)
89
                            )
90
                        )
91
                    )
92
                )
93
            );
94
95
            // return NULL, if NO value can be mapped to a boolean representation
96
            return;
97
        }
98
99
        // throw an exception if the attribute is not available
100
        throw new \Exception(
101
            $this->appendExceptionSuffix(
102
                sprintf(
103
                    'Can\'t map option value "%s" for attribute %s to a boolean representation',
104
                    $attributeValue,
105
                    $attributeCode
106
                )
107
            )
108
        );
109
    }
110
}
111