Passed
Push — 14.x ( b3445b )
by Tim
04:11
created

RawEntityLoader::__construct()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 22
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 22
ccs 0
cts 16
cp 0
rs 9.9666
cc 4
nc 3
nop 3
crap 20
1
<?php
2
3
/**
4
 * TechDivision\Import\Attribute\Set\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-set
18
 * @link      http://www.techdivision.com
19
 */
20
21
namespace TechDivision\Import\Attribute\Set\Loaders;
22
23
use TechDivision\Import\Loaders\LoaderInterface;
24
use TechDivision\Import\Connection\ConnectionInterface;
25
use TechDivision\Import\Attribute\Set\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-set
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_SET, EntityTypeCodes::EAV_ATTRIBUTE_GROUP);
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),
0 ignored issues
show
Unused Code introduced by
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

88
                $columnMetadataLoader->/** @scrutinizer ignore-call */ 
89
                                       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...
Bug introduced by
$columnMetadataLoader->load($entityType) of type ArrayAccess is incompatible with the type array expected by parameter $input 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

88
                /** @scrutinizer ignore-type */ $columnMetadataLoader->load($entityType),
Loading history...
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) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
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);
0 ignored issues
show
Unused Code introduced by
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

139
        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...
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...
140
    }
141
}
142