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

AbstractBooleanCallback   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 77
Duplicated Lines 37.66 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
c 1
b 0
f 0
lcom 1
cbo 2
dl 29
loc 77
ccs 0
cts 39
cp 0
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A handle() 29 50 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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