1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* TechDivision\Import\Attribute\Loaders\RawEntityLoader |
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 2020 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-attribute |
18
|
|
|
* @link http://www.techdivision.com |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
namespace TechDivision\Import\Attribute\Loaders; |
22
|
|
|
|
23
|
|
|
use TechDivision\Import\Loaders\LoaderInterface; |
24
|
|
|
use TechDivision\Import\Connection\ConnectionInterface; |
25
|
|
|
use TechDivision\Import\Attribute\Utils\EntityTypeCodes; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Loader for raw entities. |
29
|
|
|
* |
30
|
|
|
* @author Tim Wagner <[email protected]> |
31
|
|
|
* @copyright 2020 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-attribute |
34
|
|
|
* @link http://www.techdivision.com |
35
|
|
|
*/ |
36
|
|
|
class RawEntityLoader implements LoaderInterface |
37
|
|
|
{ |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* The connection instance. |
41
|
|
|
* |
42
|
|
|
* @var \TechDivision\Import\Connection\ConnectionInterface |
43
|
|
|
*/ |
44
|
|
|
protected $connection; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* The column metadata loader instance. |
48
|
|
|
* |
49
|
|
|
* @var \TechDivision\Import\Loaders\LoaderInterface |
50
|
|
|
*/ |
51
|
|
|
protected $rawEntities = array(); |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* The loader instance for the raw EAV entities. |
55
|
|
|
* |
56
|
|
|
* @var \TechDivision\Import\Loaders\LoaderInterface |
57
|
|
|
*/ |
58
|
|
|
protected $eavRawEntityLoader; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* The array with the attribute specific entity types. |
62
|
|
|
* |
63
|
|
|
* @var array |
64
|
|
|
*/ |
65
|
|
|
protected $entityTypes = array(EntityTypeCodes::EAV_ATTRIBUTE_OPTION, EntityTypeCodes::CATALOG_EAV_ATTRIBUTE); |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Construct a new instance. |
69
|
|
|
* |
70
|
|
|
* @param \TechDivision\Import\Connection\ConnectionInterface $connection The DB connection instance used to load the table metadata |
71
|
|
|
* @param \TechDivision\Import\Loaders\LoaderInterface $columnMetadataLoader The column metadata loader instance |
72
|
|
|
* @param \TechDivision\Import\Loaders\LoaderInterface $eavRawEntityLoader The loader instance for the raw EAV entities |
73
|
|
|
*/ |
74
|
|
|
public function __construct( |
75
|
|
|
ConnectionInterface $connection, |
76
|
|
|
LoaderInterface $columnMetadataLoader, |
77
|
|
|
LoaderInterface $eavRawEntityLoader |
78
|
|
|
) { |
79
|
|
|
|
80
|
|
|
// set the connection and the raw EAV entity loader |
81
|
|
|
$this->connection = $connection; |
82
|
|
|
$this->eavRawEntityLoader = $eavRawEntityLoader; |
83
|
|
|
|
84
|
|
|
// iterate over the entity types and create the raw entities |
85
|
|
|
foreach ($this->entityTypes as $entityType) { |
86
|
|
|
// load the columns from the metadata |
87
|
|
|
$columns = array_filter( |
88
|
|
|
$columnMetadataLoader->load($entityType), |
|
|
|
|
89
|
|
|
function ($value) { |
90
|
|
|
return $value['Key'] !== 'PRI' && $value['Null'] === 'NO' ; |
91
|
|
|
} |
92
|
|
|
); |
93
|
|
|
// initialize the raw entities and their default values, if available |
94
|
|
|
foreach ($columns as $column) { |
95
|
|
|
$this->rawEntities[$entityType][$column['Field']] = $this->loadDefaultValue($column); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Return's the default value for the passed column. |
102
|
|
|
* |
103
|
|
|
* @param array $column The column to return the default value for |
104
|
|
|
* |
105
|
|
|
* @return string|null The default value for the passed column |
106
|
|
|
*/ |
107
|
|
|
protected function loadDefaultValue(array $column) |
108
|
|
|
{ |
109
|
|
|
|
110
|
|
|
// load the default value |
111
|
|
|
$default = $column['Default']; |
112
|
|
|
|
113
|
|
|
// if a default value has been found |
114
|
|
|
if ($default === null) { |
115
|
|
|
return; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
try { |
119
|
|
|
// try to load it resolve it by executing a select statement (assuming it is an MySQL expression) |
120
|
|
|
$row = $this->connection->query(sprintf('SELECT %s()', $default))->fetch(\PDO::FETCH_ASSOC); |
121
|
|
|
return reset($row); |
122
|
|
|
} catch (\PDOException $pdoe) { |
|
|
|
|
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
// return the default value |
126
|
|
|
return $default; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Loads and returns data. |
131
|
|
|
* |
132
|
|
|
* @param string|null $entityTypeCode The table name to return the list for |
133
|
|
|
* @param array $data An array with data that will be used to initialize the raw entity with |
134
|
|
|
* |
135
|
|
|
* @return \ArrayAccess The array with the raw data |
136
|
|
|
*/ |
137
|
|
|
public function load($entityTypeCode = null, array $data = array()) |
138
|
|
|
{ |
139
|
|
|
return isset($this->rawEntities[$entityTypeCode]) ? array_merge($this->rawEntities[$entityTypeCode], $data) : $this->eavRawEntityLoader->load($entityTypeCode, $data); |
|
|
|
|
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
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.