Completed
Push — master ( 7e2127...77c655 )
by Marcus
04:10
created

RawEntityLoader::load()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 0
cts 3
cp 0
rs 10
cc 2
nc 2
nop 2
crap 6
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;
0 ignored issues
show
Bug introduced by
The type TechDivision\Import\Loaders\LoaderInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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),
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
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