Issues (23)

src/Loaders/RawEntityLoader.php (5 issues)

1
<?php
2
3
/**
4
 * TechDivision\Import\Attribute\Set\Loaders\RawEntityLoader
5
 *
6
 * PHP version 7
7
 *
8
 * @author    Tim Wagner <[email protected]>
9
 * @copyright 2020 TechDivision GmbH <[email protected]>
10
 * @license   https://opensource.org/licenses/MIT
11
 * @link      https://github.com/techdivision/import-attribute-set
12
 * @link      http://www.techdivision.com
13
 */
14
15
namespace TechDivision\Import\Attribute\Set\Loaders;
16
17
use TechDivision\Import\Loaders\LoaderInterface;
18
use TechDivision\Import\Dbal\Connection\ConnectionInterface;
19
use TechDivision\Import\Attribute\Set\Utils\EntityTypeCodes;
20
21
/**
22
 * Loader for raw entities.
23
 *
24
 * @author    Tim Wagner <[email protected]>
25
 * @copyright 2020 TechDivision GmbH <[email protected]>
26
 * @license   https://opensource.org/licenses/MIT
27
 * @link      https://github.com/techdivision/import-attribute-set
28
 * @link      http://www.techdivision.com
29
 */
30
class RawEntityLoader implements LoaderInterface
31
{
32
33
    /**
34
     * The connection instance.
35
     *
36
     * @var \TechDivision\Import\Dbal\Connection\ConnectionInterface
37
     */
38
    protected $connection;
39
40
    /**
41
     * The column metadata loader instance.
42
     *
43
     * @var \TechDivision\Import\Loaders\LoaderInterface
44
     */
45
    protected $rawEntities = array();
46
47
    /**
48
     * The loader instance for the raw EAV entities.
49
     *
50
     * @var \TechDivision\Import\Loaders\LoaderInterface
51
     */
52
    protected $eavRawEntityLoader;
53
54
    /**
55
     * The array with the attribute specific entity types.
56
     *
57
     * @var array
58
     */
59
    protected $entityTypes = array(EntityTypeCodes::EAV_ATTRIBUTE_SET, EntityTypeCodes::EAV_ATTRIBUTE_GROUP);
60
61
    /**
62
     * Construct a new instance.
63
     *
64
     * @param \TechDivision\Import\Dbal\Connection\ConnectionInterface $connection           The DB connection instance used to load the table metadata
65
     * @param \TechDivision\Import\Loaders\LoaderInterface             $columnMetadataLoader The column metadata loader instance
66
     * @param \TechDivision\Import\Loaders\LoaderInterface             $eavRawEntityLoader   The loader instance for the raw EAV entities
67
     */
68
    public function __construct(
69
        ConnectionInterface $connection,
70
        LoaderInterface $columnMetadataLoader,
71
        LoaderInterface $eavRawEntityLoader
72
    ) {
73
74
        // set the connection and the raw EAV entity loader
75
        $this->connection = $connection;
76
        $this->eavRawEntityLoader = $eavRawEntityLoader;
77
78
        // iterate over the entity types and create the raw entities
79
        foreach ($this->entityTypes as $entityType) {
80
            // load the columns from the metadata
81
            $columns = array_filter(
82
                $columnMetadataLoader->load($entityType),
0 ignored issues
show
The call to TechDivision\Import\Load...LoaderInterface::load() has too many arguments starting with $entityType. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

82
                $columnMetadataLoader->/** @scrutinizer ignore-call */ 
83
                                       load($entityType),

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. Please note the @ignore annotation hint above.

Loading history...
$columnMetadataLoader->load($entityType) of type ArrayAccess is incompatible with the type array expected by parameter $array of array_filter(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

82
                /** @scrutinizer ignore-type */ $columnMetadataLoader->load($entityType),
Loading history...
83
                function ($value) {
84
                    return $value['Key'] !== 'PRI' && $value['Null'] === 'NO' ;
85
                }
86
            );
87
            // initialize the raw entities and their default values, if available
88
            foreach ($columns as $column) {
89
                $this->rawEntities[$entityType][$column['Field']] = $this->loadDefaultValue($column);
90
            }
91
        }
92
    }
93
94
    /**
95
     * Return's the default value for the passed column.
96
     *
97
     * @param array $column The column to return the default value for
98
     *
99
     * @return string|null The default value for the passed column
100
     */
101
    protected function loadDefaultValue(array $column)
102
    {
103
104
        // load the default value
105
        $default = $column['Default'];
106
107
        // if a default value has been found
108
        if ($default === null) {
109
            // if the column don't allowed NULL and of type text or varchar, we have to return an empty string
110
            if (isset($column['Null'], $column['Type'])
111
                && $column['Null'] === "NO"
112
                && ($column['Type'] === 'text' || strpos($column['Type'], 'varchar') === 0)
113
            ) {
114
                return '';
115
            }
116
            return $default;
117
        }
118
119
        try {
120
            // try to load it resolve it by executing a select statement (assuming it is an MySQL expression)
121
            $row = $this->connection->query(sprintf('SELECT %s()', $default))->fetch(\PDO::FETCH_ASSOC);
122
            return reset($row);
123
        } catch (\PDOException $pdoe) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
124
        }
125
126
        // return the default value
127
        return $default;
128
    }
129
130
    /**
131
     * Loads and returns data.
132
     *
133
     * @param string|null $entityTypeCode The table name to return the list for
134
     * @param array       $data           An array with data that will be used to initialize the raw entity with
135
     *
136
     * @return \ArrayAccess The array with the raw data
137
     */
138
    public function load($entityTypeCode = null, array $data = array())
139
    {
140
        return isset($this->rawEntities[$entityTypeCode]) ? array_merge($this->rawEntities[$entityTypeCode], $data) : $this->eavRawEntityLoader->load($entityTypeCode, $data);
0 ignored issues
show
Bug Best Practice introduced by
The expression return IssetNode ? array...$entityTypeCode, $data) also could return the type array which is incompatible with the documented return type ArrayAccess.
Loading history...
The call to TechDivision\Import\Load...LoaderInterface::load() has too many arguments starting with $entityTypeCode. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

140
        return isset($this->rawEntities[$entityTypeCode]) ? array_merge($this->rawEntities[$entityTypeCode], $data) : $this->eavRawEntityLoader->/** @scrutinizer ignore-call */ load($entityTypeCode, $data);

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. Please note the @ignore annotation hint above.

Loading history...
141
    }
142
}
143