Completed
Pull Request — master (#4)
by Tim
02:29
created

AbstractAttributeSubject::getLastAttributeId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
eloc 2
nc 1
nop 0
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\Subjects\AbstractSubject;
25
26
/**
27
 * The abstract product subject implementation that provides basic attribute
28
 * handling business logic.
29
 *
30
 * @author    Tim Wagner <[email protected]>
31
 * @copyright 2016 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
abstract class AbstractAttributeSubject extends AbstractSubject implements AttributeSubjectInterface
37
{
38
39
    /**
40
     * The array with the available entity types.
41
     *
42
     * @var array
43
     */
44
    protected $entityTypes = array();
45
46
    /**
47
     * Mappings for attribute code => CSV column header.
48
     *
49
     * @var array
50
     */
51
    protected $headerMappings = array();
52
53
    /**
54
     * Intializes the previously loaded global data for exactly one bunch.
55
     *
56
     * @param string $serial The serial of the actual import
57
     *
58
     * @return void
59
     * @see \Importer\Csv\Actions\ProductImportAction::prepare()
60
     */
61
    public function setUp($serial)
62
    {
63
64
        // load the status of the actual import
65
        $status = $this->getRegistryProcessor()->getAttribute($serial);
66
67
        // load the global data we've prepared initially
68
        $this->entityTypes = $status[RegistryKeys::GLOBAL_DATA][RegistryKeys::ENTITY_TYPES];
69
70
        // prepare the callbacks
71
        parent::setUp($serial);
72
    }
73
74
    /**
75
     * Clean up the global data after importing the bunch.
76
     *
77
     * @param string $serial The serial of the actual import
78
     *
79
     * @return void
80
     */
81
    public function tearDown($serial)
82
    {
83
84
        // invoke the parent method
85
        parent::tearDown($serial);
86
87
        // update the status
88
        $this->getRegistryProcessor()->mergeAttributesRecursive(
89
            $serial,
90
            array(
91
                RegistryKeys::FILES => array($this->getFilename() => array(RegistryKeys::STATUS => 1))
92
            )
93
        );
94
    }
95
96
    /**
97
     * Return's the header mappings for the actual entity.
98
     *
99
     * @return array The header mappings
100
     */
101
    public function getHeaderMappings()
102
    {
103
        return $this->headerMappings;
104
    }
105
106
    /**
107
     * Map's the passed attribute code to the attribute ID that has been created recently.
108
     *
109
     * @param string $attributeCode The attribute code that has to be mapped
110
     *
111
     * @return void
112
     */
113
    public function addAttributeCodeIdMapping($attributeCode)
114
    {
115
        $this->attributeCodeIdMapping[$attributeCode] = $this->getLastEntityId();
0 ignored issues
show
Bug introduced by
The property attributeCodeIdMapping does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
116
    }
117
118
    /**
119
     * Queries whether or not the attribute with the passed code has already been processed.
120
     *
121
     * @param string $attributeCode The attribute code to check
122
     *
123
     * @return boolean TRUE if the path has been processed, else FALSE
124
     */
125
    public function hasBeenProcessed($attributeCode)
126
    {
127
        return isset($this->attributeCodeIdMapping[$attributeCode]);
128
    }
129
130
    /**
131
     * Return's the entity type for the passed code.
132
     *
133
     * @param string $entityTypeCode The entity type code
134
     *
135
     * @return array The requested entity type
136
     * @throws \Exception Is thrown, if the entity type with the passed code is not available
137
     */
138
    public function getEntityType($entityTypeCode)
139
    {
140
141
        // query whether or not, the entity type with the passed code is available
142
        if (isset($this->entityTypes[$entityTypeCode])) {
143
            return $this->entityTypes[$entityTypeCode];
144
        }
145
146
        // throw an exception, if not
147
        throw new \Exception(
148
            sprintf(
149
                'Can\'t find entity type with code %s in file %s on line %d',
150
                $entityTypeCode,
151
                $this->getFilename(),
152
                $this->getLineNumber()
153
            )
154
        );
155
    }
156
}
157