|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* TechDivision\Import\Observers\AttributeSetObserver |
|
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 |
|
18
|
|
|
* @link http://www.techdivision.com |
|
19
|
|
|
*/ |
|
20
|
|
|
|
|
21
|
|
|
namespace TechDivision\Import\Observers; |
|
22
|
|
|
|
|
23
|
|
|
use TechDivision\Import\Utils\ColumnKeys; |
|
24
|
|
|
use TechDivision\Import\Subjects\SubjectInterface; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Observer that prepares the attribute set found in the CSV file |
|
28
|
|
|
* in the row 'attribute_set_code'. |
|
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 |
|
34
|
|
|
* @link http://www.techdivision.com |
|
35
|
|
|
*/ |
|
36
|
|
|
class AttributeSetObserver extends AbstractObserver |
|
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\Observers\ObserverInterface::handle() |
|
46
|
|
|
*/ |
|
47
|
1 |
|
public function handle(SubjectInterface $subject) |
|
48
|
|
|
{ |
|
49
|
|
|
|
|
50
|
|
|
// initialize the row |
|
51
|
1 |
|
$this->setSubject($subject); |
|
52
|
1 |
|
$this->setRow($subject->getRow()); |
|
53
|
|
|
|
|
54
|
|
|
// process the functionality and return the row |
|
55
|
1 |
|
$this->process(); |
|
56
|
|
|
|
|
57
|
|
|
// return the processed row |
|
58
|
1 |
|
return $this->getRow(); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Process the observer's business logic. |
|
63
|
|
|
* |
|
64
|
|
|
* @return array The processed row |
|
65
|
|
|
*/ |
|
66
|
1 |
|
protected function process() |
|
67
|
|
|
{ |
|
68
|
|
|
|
|
69
|
|
|
// load and set the attribute set |
|
70
|
1 |
|
$this->setAttributeSet($this->getAttributeSetByAttributeSetName($this->getValue(ColumnKeys::ATTRIBUTE_SET_CODE))); |
|
71
|
1 |
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Set's the attribute set of the product that has to be created. |
|
75
|
|
|
* |
|
76
|
|
|
* @param array $attributeSet The attribute set |
|
77
|
|
|
* |
|
78
|
|
|
* @return void |
|
79
|
|
|
*/ |
|
80
|
1 |
|
protected function setAttributeSet(array $attributeSet) |
|
81
|
|
|
{ |
|
82
|
1 |
|
$this->getSubject()->setAttributeSet($attributeSet); |
|
|
|
|
|
|
83
|
1 |
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Return's the attribute set with the passed attribute set name. |
|
87
|
|
|
* |
|
88
|
|
|
* @param string $attributeSetName The name of the requested attribute set |
|
89
|
|
|
* |
|
90
|
|
|
* @return array The attribute set data |
|
91
|
|
|
*/ |
|
92
|
1 |
|
protected function getAttributeSetByAttributeSetName($attributeSetName) |
|
93
|
|
|
{ |
|
94
|
1 |
|
return $this->getSubject()->getAttributeSetByAttributeSetName($attributeSetName); |
|
|
|
|
|
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: