Skip to content

Commit abbb4c2

Browse files
committed
Refactoring Code:
remove strict mode integration create abstarct class to avoid duplicate code
1 parent 636f1aa commit abbb4c2

4 files changed

+121
-193
lines changed
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
<?php
2+
/**
3+
* TechDivision\Import\Attribute\Callbacks\AttributeRelationFrontendInputTypeToBackendTypeValidatorCallback
4+
*
5+
* PHP version 7
6+
*
7+
* @author MET <met@techdivision.com>
8+
* @copyright 2022 TechDivision GmbH <info@techdivision.com>
9+
* @license https://opensource.org/licenses/MIT
10+
* @link https://github.com/techdivision/import-attribute
11+
* @link http://www.techdivision.com
12+
*/
13+
namespace TechDivision\Import\Attribute\Callbacks;
14+
15+
use TechDivision\Import\Attribute\Utils\ColumnKeys;
16+
use TechDivision\Import\Callbacks\IndexedArrayValidatorCallback;
17+
use TechDivision\Import\Loaders\LoaderInterface;
18+
use TechDivision\Import\Subjects\SubjectInterface;
19+
20+
/**
21+
* A callback implementation that validates the a list of attribute set names.
22+
*
23+
* @author MET <met@techdivision.com>
24+
* @copyright 2022 TechDivision GmbH <info@techdivision.com>
25+
* @license https://opensource.org/licenses/MIT
26+
* @link https://github.com/techdivision/import-attribute
27+
* @link http://www.techdivision.com
28+
*/
29+
abstract class AbstractAttributeValidatorCallback extends IndexedArrayValidatorCallback
30+
{
31+
32+
/**
33+
* The array with the values of the main row.
34+
*
35+
* @var array
36+
*/
37+
private $mainRowValues = array();
38+
39+
/**
40+
* The main row value loader instance to load the validations with.
41+
*
42+
* @var \TechDivision\Import\Loaders\LoaderInterface
43+
*/
44+
private $mainRowValueLoader;
45+
46+
/**
47+
* Initializes the callback with the loader instance.
48+
*
49+
* @param \TechDivision\Import\Loaders\LoaderInterface $loader The loader instance to load the validations with
50+
* @param \TechDivision\Import\Loaders\LoaderInterface $mainRowValueLoader The loader instance to load the main row values for of the attribute
51+
* @param boolean $nullable The flag to decide whether or not the value can be empty
52+
* @param boolean $mainRowOnly The flag to decide whether or not the value has to be validated on the main row only
53+
*/
54+
public function __construct(LoaderInterface $loader, LoaderInterface $mainRowValueLoader, $nullable = false, $mainRowOnly = false)
55+
{
56+
57+
// pass the loader to the parent instance
58+
parent::__construct($loader);
59+
60+
// the loader for the main row values
61+
$this->mainRowValueLoader = $mainRowValueLoader;
62+
63+
// initialize the flags with the passed values
64+
$this->nullable = $nullable;
65+
$this->mainRowOnly = $mainRowOnly;
66+
}
67+
68+
/**
69+
* Will be invoked by the callback visitor when a factory has been defined to create the callback instance.
70+
*
71+
* @param \TechDivision\Import\Subjects\SubjectInterface $subject The subject instance
72+
*
73+
* @return \TechDivision\Import\Callbacks\CallbackInterface The callback instance
74+
*/
75+
public function createCallback(SubjectInterface $subject)
76+
{
77+
// load the main row values as fallback for a store view row
78+
$this->mainRowValues = $this->mainRowValueLoader->load();
79+
80+
// return the initialized instance
81+
return parent::createCallback($subject);
82+
}
83+
84+
/**
85+
* Resolve's the value with the passed colum name from the actual row. If the
86+
* column does not contain a value, the value from the main row, if available
87+
* will be returned.
88+
*
89+
* @param string $name The name of the column to return the value for
90+
*
91+
* @return mixed|null The value
92+
*/
93+
public function getValue($name)
94+
{
95+
// load the attribute code of the actual EAV attribute
96+
$attributeCode = $this->getSubject()->getValue(ColumnKeys::ATTRIBUTE_CODE);
97+
98+
// load the value of the main row as fallback, if available
99+
$mainRowValue = isset($this->mainRowValues[$attributeCode]) ? $this->mainRowValues[$attributeCode] : null;
100+
101+
// return the value of the passed colmn name
102+
return $this->getSubject()->getValue($name, $mainRowValue);
103+
}
104+
}

src/Callbacks/AttributeOptionValuesFrontendInputTypeValidatorCallback.php

Lines changed: 1 addition & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,6 @@
1515
namespace TechDivision\Import\Attribute\Callbacks;
1616

1717
use TechDivision\Import\Attribute\Utils\ColumnKeys;
18-
use TechDivision\Import\Callbacks\IndexedArrayValidatorCallback;
19-
use TechDivision\Import\Loaders\LoaderInterface;
20-
use TechDivision\Import\Subjects\SubjectInterface;
2118

2219
/**
2320
* A callback implementation that validates the a list of attribute set names.
@@ -28,62 +25,9 @@
2825
* @link https://github.com/techdivision/import-attribute
2926
* @link http://www.techdivision.com
3027
*/
31-
class AttributeOptionValuesFrontendInputTypeValidatorCallback extends IndexedArrayValidatorCallback
28+
class AttributeOptionValuesFrontendInputTypeValidatorCallback extends AbstractAttributeValidatorCallback
3229
{
3330

34-
/**
35-
* The array with the values of the main row.
36-
*
37-
* @var array
38-
*/
39-
private $mainRowValues = array();
40-
41-
/**
42-
* The main row value loader instance to load the validations with.
43-
*
44-
* @var \TechDivision\Import\Loaders\LoaderInterface
45-
*/
46-
private $mainRowValueLoader;
47-
48-
/**
49-
* Initializes the callback with the loader instance.
50-
*
51-
* @param \TechDivision\Import\Loaders\LoaderInterface $loader The loader instance to load the validations with
52-
* @param \TechDivision\Import\Loaders\LoaderInterface $mainRowValueLoader The loader instance to load the main row values for of the attribute
53-
* @param boolean $nullable The flag to decide whether or not the value can be empty
54-
* @param boolean $mainRowOnly The flag to decide whether or not the value has to be validated on the main row only
55-
*/
56-
public function __construct(LoaderInterface $loader, LoaderInterface $mainRowValueLoader, $nullable = false, $mainRowOnly = false)
57-
{
58-
59-
// pass the loader to the parent instance
60-
parent::__construct($loader);
61-
62-
// the loader for the main row values
63-
$this->mainRowValueLoader = $mainRowValueLoader;
64-
65-
// initialize the flags with the passed values
66-
$this->nullable = $nullable;
67-
$this->mainRowOnly = $mainRowOnly;
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-
// load the main row values as fallback for a store view row
81-
$this->mainRowValues = $this->mainRowValueLoader->load();
82-
83-
// return the initialized instance
84-
return parent::createCallback($subject);
85-
}
86-
8731
/**
8832
* Will be invoked by the observer it has been registered for.
8933
*
@@ -95,7 +39,6 @@ public function createCallback(SubjectInterface $subject)
9539
*/
9640
public function handle($attributeCode = null, $attributeValue = null)
9741
{
98-
9942
// query whether or not the passed value
10043
// IS empty and empty values are allowed
10144
if ($this->isNullable($attributeValue)) {
@@ -134,26 +77,4 @@ public function handle($attributeCode = null, $attributeValue = null)
13477
)
13578
);
13679
}
137-
138-
/**
139-
* Resolve's the value with the passed colum name from the actual row. If the
140-
* column does not contain a value, the value from the main row, if available
141-
* will be returned.
142-
*
143-
* @param string $name The name of the column to return the value for
144-
*
145-
* @return mixed|null The value
146-
*/
147-
public function getValue($name)
148-
{
149-
150-
// load the attribute code of the actual EAV attribute
151-
$attributeCode = $this->getSubject()->getValue(ColumnKeys::ATTRIBUTE_CODE);
152-
153-
// load the value of the main row as fallback, if available
154-
$mainRowValue = isset($this->mainRowValues[$attributeCode]) ? $this->mainRowValues[$attributeCode] : null;
155-
156-
// return the value of the passed colmn name
157-
return $this->getSubject()->getValue($name, $mainRowValue);
158-
}
15980
}

src/Callbacks/AttributeRelationFrontendInputTypeToBackendTypeValidatorCallback.php

Lines changed: 16 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* PHP version 7
77
*
88
* @author MET <met@techdivision.com>
9-
* @copyright 20212 TechDivision GmbH <info@techdivision.com>
9+
* @copyright 2022 TechDivision GmbH <info@techdivision.com>
1010
* @license https://opensource.org/licenses/MIT
1111
* @link https://github.com/techdivision/import-attribute
1212
* @link http://www.techdivision.com
@@ -15,76 +15,18 @@
1515
namespace TechDivision\Import\Attribute\Callbacks;
1616

1717
use TechDivision\Import\Attribute\Utils\ColumnKeys;
18-
use TechDivision\Import\Callbacks\IndexedArrayValidatorCallback;
19-
use TechDivision\Import\Loaders\LoaderInterface;
20-
use TechDivision\Import\Subjects\SubjectInterface;
21-
use TechDivision\Import\Utils\RegistryKeys;
2218

2319
/**
2420
* A callback implementation that validates the a list of attribute set names.
2521
*
26-
* @author Martin Eisenführer <m.eisenfuehrer@techdivision.com>
27-
* @copyright 2021 TechDivision GmbH <info@techdivision.com>
22+
* @author MET <met@techdivision.com>
23+
* @copyright 2022 TechDivision GmbH <info@techdivision.com>
2824
* @license https://opensource.org/licenses/MIT
2925
* @link https://github.com/techdivision/import-attribute
3026
* @link http://www.techdivision.com
3127
*/
32-
class AttributeRelationFrontendInputTypeToBackendTypeValidatorCallback extends IndexedArrayValidatorCallback
28+
class AttributeRelationFrontendInputTypeToBackendTypeValidatorCallback extends AbstractAttributeValidatorCallback
3329
{
34-
35-
/**
36-
* The array with the values of the main row.
37-
*
38-
* @var array
39-
*/
40-
private $mainRowValues = array();
41-
42-
/**
43-
* The main row value loader instance to load the validations with.
44-
*
45-
* @var \TechDivision\Import\Loaders\LoaderInterface
46-
*/
47-
private $mainRowValueLoader;
48-
49-
/**
50-
* Initializes the callback with the loader instance.
51-
*
52-
* @param \TechDivision\Import\Loaders\LoaderInterface $loader The loader instance to load the validations with
53-
* @param \TechDivision\Import\Loaders\LoaderInterface $mainRowValueLoader The loader instance to load the main row values for of the attribute
54-
* @param boolean $nullable The flag to decide whether or not the value can be empty
55-
* @param boolean $mainRowOnly The flag to decide whether or not the value has to be validated on the main row only
56-
*/
57-
public function __construct(LoaderInterface $loader, LoaderInterface $mainRowValueLoader, $nullable = false, $mainRowOnly = false)
58-
{
59-
60-
// pass the loader to the parent instance
61-
parent::__construct($loader);
62-
63-
// the loader for the main row values
64-
$this->mainRowValueLoader = $mainRowValueLoader;
65-
66-
// initialize the flags with the passed values
67-
$this->nullable = $nullable;
68-
$this->mainRowOnly = $mainRowOnly;
69-
}
70-
71-
/**
72-
* Will be invoked by the callback visitor when a factory has been defined to create the callback instance.
73-
*
74-
* @param \TechDivision\Import\Subjects\SubjectInterface $subject The subject instance
75-
*
76-
* @return \TechDivision\Import\Callbacks\CallbackInterface The callback instance
77-
*/
78-
public function createCallback(SubjectInterface $subject)
79-
{
80-
81-
// load the main row values as fallback for a store view row
82-
$this->mainRowValues = $this->mainRowValueLoader->load();
83-
84-
// return the initialized instance
85-
return parent::createCallback($subject);
86-
}
87-
8830
/**
8931
* Will be invoked by the observer it has been registered for.
9032
*
@@ -96,7 +38,6 @@ public function createCallback(SubjectInterface $subject)
9638
*/
9739
public function handle($attributeCode = null, $attributeValue = null)
9840
{
99-
10041
// query whether or not the passed value
10142
// IS empty and empty values are allowed
10243
if ($this->isNullable($attributeValue)) {
@@ -118,62 +59,25 @@ public function handle($attributeCode = null, $attributeValue = null)
11859
return;
11960
}
12061
$message = sprintf(
121-
'Found invalid backend input type "%s" for attribute with code "%s" for frontend Type "%s", must be one of "%s" as backend type',
62+
'Found invalid backend_type "%s" for attribute "%s" with frontend Type "%s", must be one of "%s" as backend_type',
12263
$backendType,
12364
$this->getSubject()->getValue(ColumnKeys::ATTRIBUTE_CODE),
124-
$this->getValue(ColumnKeys::FRONTEND_INPUT),
65+
$attributeCode,
12566
implode(',', $backendTypeValidation[ColumnKeys::BACKEND_TYPE])
12667
);
127-
if ($this->getSubject()->isStrictMode()) {
128-
// throw an exception if the value is NOT in the array
129-
throw new \InvalidArgumentException($message);
130-
}
131-
$this->getSubject()->getSystemLogger()->warning($message);
132-
$this->getSubject()->mergeStatus(
133-
array(
134-
RegistryKeys::NO_STRICT_VALIDATIONS => array(
135-
basename($this->getSubject()->getFilename()) => array(
136-
$this->getSubject()->getLineNumber() => array(
137-
ColumnKeys::BACKEND_TYPE => $message
138-
)
139-
)
140-
)
141-
)
142-
);
143-
return;
68+
69+
// throw an exception if the value is NOT in the array
70+
throw new \InvalidArgumentException($message);
14471
}
145-
} else {
146-
continue;
14772
}
148-
// throw an exception if the necessary configuration is NOT available
149-
throw new \InvalidArgumentException(
150-
sprintf(
151-
'Missing custom validation configuration "frontend_input" type within configuration for column "%s"',
152-
$this->getSubject()->getValue(ColumnKeys::ATTRIBUTE_CODE)
153-
)
154-
);
15573
}
156-
}
157-
158-
/**
159-
* Resolve's the value with the passed colum name from the actual row. If the
160-
* column does not contain a value, the value from the main row, if available
161-
* will be returned.
162-
*
163-
* @param string $name The name of the column to return the value for
164-
*
165-
* @return mixed|null The value
166-
*/
167-
public function getValue($name)
168-
{
169-
170-
// load the attribute code of the actual EAV attribute
171-
$attributeCode = $this->getSubject()->getValue(ColumnKeys::ATTRIBUTE_CODE);
172-
173-
// load the value of the main row as fallback, if available
174-
$mainRowValue = isset($this->mainRowValues[$attributeCode]) ? $this->mainRowValues[$attributeCode] : null;
17574

176-
// return the value of the passed colmn name
177-
return $this->getSubject()->getValue($name, $mainRowValue);
75+
// throw an exception if the necessary configuration is NOT available
76+
throw new \InvalidArgumentException(
77+
sprintf(
78+
'Missing custom validation configuration "backend_type" type within configuration for column "%s"',
79+
$this->getSubject()->getValue(ColumnKeys::ATTRIBUTE_CODE)
80+
)
81+
);
17882
}
17983
}

src/Loaders/AttributeValueLoader.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ class AttributeValueLoader implements LoaderInterface
5050
*/
5151
public function __construct(LoaderInterface $registryLoader, $columnName)
5252
{
53-
5453
// initialize the column name and the registry loader
5554
$this->columnName = $columnName;
5655
$this->registryLoader = $registryLoader;

0 commit comments

Comments
 (0)