Completed
Push — 15.x ( 4ca9b4...c439ce )
by Tim
02:43
created

AbstractValidatorCallback::isMainRow()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 0
cts 5
cp 0
rs 9.9666
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 6
1
<?php
2
3
/**
4
 * TechDivision\Import\Callbacks\AbstractValidatorCallback
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
18
 * @link      http://www.techdivision.com
19
 */
20
21
namespace TechDivision\Import\Callbacks;
22
23
use TechDivision\Import\Loaders\LoaderInterface;
24
use TechDivision\Import\Subjects\SubjectInterface;
25
use TechDivision\Import\Utils\ColumnKeys;
26
27
/**
28
 * Abstract callback implementation the validate the value for an specific attribute.
29
 *
30
 * @author    Tim Wagner <[email protected]>
31
 * @copyright 2019 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
34
 * @link      http://www.techdivision.com
35
 */
36
abstract class AbstractValidatorCallback implements CallbackInterface, CallbackFactoryInterface
37
{
38
39
    /**
40
     * The loader instance for the custom validations.
41
     *
42
     * @var \TechDivision\Import\Loaders\LoaderInterface
43
     */
44
    protected $loader;
45
46
    /**
47
     * The subject instance the serializer is bound to.
48
     *
49
     * @var \TechDivision\Import\Subjects\SubjectInterface
50
     */
51
    protected $subject;
52
53
    /**
54
     * The array that contains the allowed values found in the configuration.
55
     *
56
     * @var array
57
     */
58
    protected $validations = array();
59
60
    /**
61
     * Initializes the callback with the loader instance.
62
     *
63
     * @param \TechDivision\Import\Loaders\LoaderInterface $loader The loader for the validations
64
     */
65
    public function __construct(LoaderInterface $loader)
66
    {
67
        $this->loader = $loader;
68
    }
69
70
    /**
71
     * Will be invoked by the callback visitor when a factory has been defined to create the callback instance.
72
     *
73
     * @param \TechDivision\Import\Subjects\SubjectInterface $subject The subject instance
74
     *
75
     * @return \TechDivision\Import\Callbacks\CallbackInterface The callback instance
76
     */
77
    public function createCallback(SubjectInterface $subject)
78
    {
79
80
        // set the subject
81
        $this->setSubject($subject);
82
83
        // set the validations
84
        $this->setValidations($this->getLoader()->load($subject->getConfiguration()));
0 ignored issues
show
Unused Code introduced by
The call to LoaderInterface::load() has too many arguments starting with $subject->getConfiguration().

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
Documentation introduced by
$this->getLoader()->load...ct->getConfiguration()) is of type object<ArrayAccess>, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
85
86
        // return the initialized instance
87
        return $this;
88
    }
89
90
    /**
91
     * Set's the subject instance.
92
     *
93
     * @param \TechDivision\Import\Subjects\SubjectInterface $subject The subject instance
94
     *
95
     * @return void
96
     */
97
    protected function setSubject(SubjectInterface $subject)
98
    {
99
        $this->subject = $subject;
100
    }
101
102
    /**
103
     * Return's the subject instance.
104
     *
105
     * @return \TechDivision\Import\Subjects\SubjectInterface The subject instance
106
     */
107
    protected function getSubject()
108
    {
109
        return $this->subject;
110
    }
111
112
    /**
113
     * Return's the loader instance for the custom validations.
114
     *
115
     * @return \TechDivision\Import\Loaders\LoaderInterface The loader instance
116
     */
117
    protected function getLoader()
118
    {
119
        return $this->loader;
120
    }
121
122
    /**
123
     * Set's the validations.
124
     *
125
     * @param array $validations The available validations
126
     *
127
     * @return void
128
     */
129
    protected function setValidations(array $validations)
130
    {
131
        $this->validations = $validations;
132
    }
133
134
    /**
135
     * Return's the validations for the attribute with the passed code.
136
     *
137
     * @param string|null $attributeCode The code of the attribute to return the validations for
138
     *
139
     * @return array The allowed values for the attribute with the passed code
140
     */
141
    protected function getValidations($attributeCode = null)
142
    {
143
        return $this->validations;
144
    }
145
146
    /**
147
     * Whether or not the actual row is a main row, the method returns TRUE or FALSE.
148
     *
149
     * @return boolean TRUE if the row is a main row, FALSE otherwise
150
     */
151
    protected function isMainRow()
152
    {
153
154
        // load the store view code
155
        $storeViewCode = $this->getSubject()->getValue(ColumnKeys::STORE_VIEW_CODE);
156
157
        // query whether or not it is empty
158
        return ($storeViewCode !== null || $storeViewCode !== '');
159
    }
160
}
161