Completed
Push — 15.x ( b45baa...4ca9b4 )
by Tim
02:13
created

NumberValidatorCallback   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 94
Duplicated Lines 18.09 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 3
dl 17
loc 94
ccs 0
cts 30
cp 0
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A createCallback() 0 9 1
A isNullable() 0 4 3
A isNumber() 0 4 2
A handle() 17 17 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\NumberValidatorCallback
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 2019 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-product
18
 * @link      http://www.techdivision.com
19
 */
20
21
namespace TechDivision\Import\Callbacks;
22
23
use TechDivision\Import\Subjects\SubjectInterface;
24
25
/**
26
 * A callback implementation that validates the value is a valid number.
27
 *
28
 * @author    Tim Wagner <[email protected]>
29
 * @copyright 2019 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-product
32
 * @link      http://www.techdivision.com
33
 */
34
class NumberValidatorCallback implements CallbackInterface, CallbackFactoryInterface
35
{
36
37
    /**
38
     * The flag to query whether or not the value can be empty.
39
     *
40
     * @var boolean
41
     */
42
    protected $nullable = false;
43
44
    /**
45
     * The default locale.
46
     *
47
     * @var string
48
     */
49
    protected $locale = 'en_US';
50
51
    /**
52
     * Initializes the callback with the loader instance.
53
     *
54
     * @param boolean $nullable The flag to decide whether or not the value can be empty
55
     */
56
    public function __construct($nullable = false)
57
    {
58
        $this->nullable = $nullable;
59
    }
60
61
    /**
62
     * Will be invoked by the callback visitor when a factory has been defined to create the callback instance.
63
     *
64
     * @param \TechDivision\Import\Subjects\SubjectInterface $subject The subject instance
65
     *
66
     * @return \TechDivision\Import\Callbacks\CallbackInterface The callback instance
67
     */
68
    public function createCallback(SubjectInterface $subject)
69
    {
70
71
        // load the locale from the subject's number converter configuration
72
        $this->locale = $subject->getConfiguration()->getNumberConverter()->getLocale();
73
74
        // return the initialized instance
75
        return $this;
76
    }
77
78
    /**
79
     * Query whether or not the passed value IS empty and empty values are allowed.
80
     *
81
     * @param string $attributeValue The attribute value to query for
82
     *
83
     * @return boolean TRUE if empty values are allowed and the passed value IS empty
84
     */
85
    protected function isNullable($attributeValue)
86
    {
87
        return $this->nullable && ($attributeValue === '' || $attributeValue === null);
88
    }
89
90
    /**
91
     * Query whether or not the passed value IS a valid number.
92
     *
93
     * @param string $attributeValue The attribute value to query for
94
     *
95
     * @return boolean TRUE if the passed value IS a valid number
96
     */
97
    protected function isNumber($attributeValue)
98
    {
99
        return is_numeric($attributeValue) || \NumberFormatter::create($this->locale, \NumberFormatter::DECIMAL)->parse($attributeValue);
100
    }
101
102
    /**
103
     * Will be invoked by the observer it has been registered for.
104
     *
105
     * @param string|null $attributeCode  The code of the attribute that has to be validated
106
     * @param string|null $attributeValue The attribute value to be validated
107
     *
108
     * @return mixed The modified value
109
     */
110 View Code Duplication
    public function handle($attributeCode = null, $attributeValue = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
111
    {
112
113
        // query whether or not we've found a value and it is a valid number
114
        if ($this->isNullable($attributeValue) || $this->isNumber($attributeValue)) {
115
            return;
116
        }
117
118
        // throw an exception if the value is NOT in the array
119
        throw new \InvalidArgumentException(
120
            sprintf(
121
                'Found invalid number "%s" for column "%s"',
122
                $attributeValue,
123
                $attributeCode
124
            )
125
        );
126
    }
127
}
128