Completed
Push — master ( ffe826...49f402 )
by
unknown
01:44
created

AbstractAttributeImportObserver::getDefaultValue()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
1
<?php
2
3
/**
4
 * TechDivision\Import\Attribute\Observers\AbstractAttributeImportObserver
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\Observers;
22
23
use TechDivision\Import\Subjects\SubjectInterface;
24
use TechDivision\Import\Observers\AbstractObserver;
25
use TechDivision\Import\Attribute\Utils\ColumnKeys;
26
27
/**
28
 * Abstract attribute observer that handles the process to import attribute bunches.
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 AbstractAttributeImportObserver extends AbstractObserver implements AttributeImportObserverInterface
37
{
38
39
    /**
40
     * Will be invoked by the action on the events the listener has been registered for.
41
     *
42
     * @param \TechDivision\Import\Subjects\SubjectInterface $subject The subject instance
43
     *
44
     * @return array The modified row
45
     * @see \TechDivision\Import\Product\Observers\ImportObserverInterface::handle()
46
     */
47
    public function handle(SubjectInterface $subject)
48
    {
49
50
        // initialize the row
51
        $this->setSubject($subject);
52
        $this->setRow($subject->getRow());
53
54
        // process the functionality and return the row
55
        $this->process();
56
57
        // return the processed row
58
        return $this->getRow();
59
    }
60
61
    /**
62
     * Return's whether or not this is the admin store view.
63
     *
64
     * @return boolean TRUE if we're in admin store view, else FALSE
65
     */
66
    protected function isAdminStore()
67
    {
68
        return $this->getValue(ColumnKeys::STORE_VIEW_CODE) === null;
69
    }
70
71
    /**
72
     * Process the observer's business logic.
73
     *
74
     * @return void
75
     */
76
    abstract protected function process();
77
78
79
    /**
80
     * Prepare data array by given values in source
81
     *
82
     * @param array $keys
83
     * @param bool $useDefaults
84
     * @return array
85
     */
86
    protected function getPreparedAttributeData(array $keys, $useDefaults = true)
87
    {
88
        $attribute = [];
89
        foreach ($keys as $key) {
90
            if ($this->hasValue($key) || $useDefaults) {
91
                $attribute[$key] = $this->getValue($key, $this->getDefaultValue($key), $this->getCallback($key));
92
            }
93
        }
94
        return $attribute;
95
    }
96
97
    /**
98
     * @return array
99
     */
100
    protected function getDefaultValues()
101
    {
102
        return array();
103
    }
104
105
    /**
106
     * Retrieve default value for given key
107
     *
108
     * @param string $key
109
     * @return null
110
     */
111
    protected function getDefaultValue($key)
112
    {
113
        $defaultValues = $this->getDefaultValues();
114
115
        if (isset($defaultValues[$key])) {
116
            return $defaultValues[$key];
117
        }
118
119
        return null;
120
    }
121
122
    /**
123
     * @return array
124
     */
125
    protected function getCallbacks()
126
    {
127
        return array();
128
    }
129
130
    /**
131
     * Retrieve callbacks for given column key
132
     *
133
     * @param string $key
134
     * @return null
135
     */
136
    protected function getCallback($key)
137
    {
138
        $callbacks = $this->getCallbacks();
139
140
        if (isset($callbacks[$key])) {
141
            return $callbacks[$key];
142
        }
143
144
        return null;
145
    }
146
}
147