Completed
Push — 19.x ( 24d584 )
by Tim
02:01
created

AbstractAttributeSubject::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 0
cts 7
cp 0
rs 9.7666
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
/**
4
 * TechDivision\Import\Attribute\Subjects\AbstractAttributeSubject
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 2016 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\Subjects;
22
23
use TechDivision\Import\Utils\RegistryKeys;
24
use TechDivision\Import\Attribute\Utils\MemberNames;
25
use TechDivision\Import\Subjects\AbstractSubject;
26
use TechDivision\Import\Subjects\EntitySubjectInterface;
27
use TechDivision\Import\Subjects\CleanUpColumnsSubjectInterface;
28
use TechDivision\Import\Attribute\Utils\ConfigurationKeys;
29
30
/**
31
 * The abstract product subject implementation that provides basic attribute
32
 * handling business logic.
33
 *
34
 * @author    Tim Wagner <[email protected]>
35
 * @copyright 2016 TechDivision GmbH <[email protected]>
36
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
37
 * @link      https://github.com/techdivision/import-attribute
38
 * @link      http://www.techdivision.com
39
 */
40
abstract class AbstractAttributeSubject extends AbstractSubject implements AttributeSubjectInterface, EntitySubjectInterface, CleanUpColumnsSubjectInterface
41
{
42
43
    /**
44
     * The array with the available entity types.
45
     *
46
     * @var array
47
     */
48
    protected $entityTypes = array();
49
50
    /**
51
     * The default entity type code.
52
     *
53
     * @var string
54
     */
55
    protected $defaultEntityTypeCode;
56
57
    /**
58
     * Intializes the previously loaded global data for exactly one bunch.
59
     *
60
     * @param string $serial The serial of the actual import
61
     *
62
     * @return void
63
     */
64
    public function setUp($serial)
65
    {
66
67
        // load the status of the actual import
68
        $status = $this->getRegistryProcessor()->getAttribute(RegistryKeys::STATUS);
69
70
        // load the global data we've prepared initially
71
        $this->entityTypes = $status[RegistryKeys::GLOBAL_DATA][RegistryKeys::ENTITY_TYPES];
72
73
        // initialize the default entity type code with the value from the configuration
74
        $this->defaultEntityTypeCode = $this->getEntityTypeCode();
75
76
        // prepare the callbacks
77
        parent::setUp($serial);
78
    }
79
80
    /**
81
     * Return's the header mappings for the actual entity.
82
     *
83
     * @return array The header mappings
84
     */
85
    public function getHeaderMappings()
86
    {
87
        return $this->headerMappings;
88
    }
89
90
    /**
91
     * Returns the default entity type code.
92
     *
93
     * @return string The default entity type code
94
     */
95
    public function getDefaultEntityTypeCode()
96
    {
97
        return $this->defaultEntityTypeCode;
98
    }
99
100
    /**
101
     * Return's the entity type for the passed code, of if no entity type code has
102
     * been passed, the default one from the configuration will be used.
103
     *
104
     * @param string|null $entityTypeCode The entity type code
105
     *
106
     * @return array The requested entity type
107
     * @throws \Exception Is thrown, if the entity type with the passed code is not available
108
     */
109
    public function getEntityType($entityTypeCode = null)
110
    {
111
112
        // set the default entity type code, if non has been passed
113
        if ($entityTypeCode === null) {
114
            $entityTypeCode = $this->getDefaultEntityTypeCode();
115
        }
116
117
        // query whether or not, the entity type with the passed code is available
118
        if (isset($this->entityTypes[$entityTypeCode])) {
119
            return $this->entityTypes[$entityTypeCode];
120
        }
121
122
        // throw an exception, if not
123
        throw new \Exception(
124
            sprintf(
125
                'Can\'t find entity type with code %s in file %s on line %d',
126
                $entityTypeCode,
127
                $this->getFilename(),
128
                $this->getLineNumber()
129
            )
130
        );
131
    }
132
133
    /**
134
     * Returns the entity type ID for the passed code, or if no entity type code has
135
     * been passed, the default one from the configuration will be used.
136
     *
137
     * @param string|null $entityTypeCode The entity type code
138
     *
139
     * @return integer The actual entity type ID
140
     */
141
    public function getEntityTypeId($entityTypeCode = null)
142
    {
143
144
        // load the entity type for the given code, or the default one otherwise
145
        $entityType = $this->getEntityType($entityTypeCode ? $entityTypeCode : $this->getDefaultEntityTypeCode());
146
147
        // return the entity type ID
148
        return $entityType[MemberNames::ENTITY_TYPE_ID];
149
    }
150
151
    /**
152
     * Merge the columns from the configuration with all image type columns to define which
153
     * columns should be cleaned-up.
154
     *
155
     * @return array The columns that has to be cleaned-up
156
     */
157
    public function getCleanUpColumns()
158
    {
159
160
        // initialize the array for the columns that has to be cleaned-up
161
        $cleanUpColumns = array();
162
163
        // query whether or not an array has been specified in the configuration
164
        if ($this->getConfiguration()->hasParam(ConfigurationKeys::CLEAN_UP_EMPTY_COLUMNS)) {
165
            $cleanUpColumns = $this->getConfiguration()->getParam(ConfigurationKeys::CLEAN_UP_EMPTY_COLUMNS);
166
        }
167
168
        // return the array with the column names
169
        return $cleanUpColumns;
170
    }
171
}
172