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

AttributeObserver::getDefaultValues()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 0
1
<?php
2
3
/**
4
 * TechDivision\Import\Attribute\Observers\AttributeObserver
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\Utils\BackendTypeKeys;
24
use TechDivision\Import\Attribute\Utils\ColumnKeys;
25
use TechDivision\Import\Attribute\Utils\MemberNames;
26
use TechDivision\Import\Attribute\Services\AttributeBunchProcessorInterface;
27
28
/**
29
 * Observer that create's the EAV attribute itself.
30
 *
31
 * @author    Tim Wagner <[email protected]>
32
 * @copyright 2016 TechDivision GmbH <[email protected]>
33
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
34
 * @link      https://github.com/techdivision/import-attribute
35
 * @link      http://www.techdivision.com
36
 */
37
class AttributeObserver extends AbstractAttributeImportObserver
38
{
39
40
    /**
41
     * The attribute processor instance.
42
     *
43
     * @var \TechDivision\Import\Attribute\Services\AttributeBunchProcessorInterface
44
     */
45
    protected $attributeBunchProcessor;
46
47
    /**
48
     * Initializes the observer with the passed subject instance.
49
     *
50
     * @param \TechDivision\Import\Attribute\Services\AttributeBunchProcessorInterface $attributeBunchProcessor The attribute bunch processor instance
51
     */
52
    public function __construct(AttributeBunchProcessorInterface $attributeBunchProcessor)
53
    {
54
        $this->attributeBunchProcessor = $attributeBunchProcessor;
55
    }
56
57
    /**
58
     * Process the observer's business logic.
59
     *
60
     * @return void
61
     */
62
    protected function process()
63
    {
64
65
        // query whether or not, we've found a new attribute code => means we've found a new attribute
66
        if ($this->hasBeenProcessed($this->getValue(ColumnKeys::ATTRIBUTE_CODE))) {
67
            return;
68
        }
69
70
        // prepare the attribue values
71
        $attribute = $this->initializeAttribute($this->prepareAttributes());
72
73
        // insert the entity and set the entity ID
74
        $this->setLastAttributeId($this->persistAttribute($attribute));
75
    }
76
77
    /**
78
     * @return array
79
     */
80
    protected function getDefaultValues()
81
    {
82
        return array(
83
            MemberNames::BACKEND_TYPE => BackendTypeKeys::BACKEND_TYPE_STATIC,
84
            MemberNames::IS_REQUIRED => 0,
85
            MemberNames::IS_USER_DEFINED => 1,
86
            MemberNames::IS_UNIQUE => 0,
87
        );
88
    }
89
90
    /**
91
     * Prepare the attributes of the entity that has to be persisted.
92
     *
93
     * @return array The prepared attributes
94
     */
95
    protected function prepareAttributes()
96
    {
97
        // map the entity type code to the ID
98
        $entityType = $this->getEntityType($this->getValue(ColumnKeys::ENTITY_TYPE_CODE));
99
        $entityTypeId = $entityType[MemberNames::ENTITY_TYPE_ID];
100
101
        return $this->initializeEntity(array_merge(
102
            array(
103
                MemberNames::ENTITY_TYPE_ID => $entityTypeId
104
            ),
105
            $this->getPreparedAttributeData(
106
                array(
107
                    MemberNames::ATTRIBUTE_CODE,
108
                    MemberNames::ATTRIBUTE_MODEL,
109
                    MemberNames::BACKEND_MODEL,
110
                    MemberNames::BACKEND_TYPE,
111
                    MemberNames::BACKEND_TABLE,
112
                    MemberNames::FRONTEND_MODEL,
113
                    MemberNames::FRONTEND_INPUT,
114
                    MemberNames::FRONTEND_LABEL,
115
                    MemberNames::FRONTEND_CLASS,
116
                    MemberNames::SOURCE_MODEL,
117
                    MemberNames::IS_REQUIRED,
118
                    MemberNames::IS_USER_DEFINED,
119
                    MemberNames::DEFAULT_VALUE,
120
                    MemberNames::IS_UNIQUE,
121
                    MemberNames::NOTE,
122
                ),
123
                $this->isForceDefaultValues()
124
            )
125
        ));
126
    }
127
128
    /**
129
     * Should default values be used for undefined columns
130
     * @return bool
131
     */
132
    protected function isForceDefaultValues()
133
    {
134
        return true;
135
    }
136
137
    /**
138
     * Initialize the attribute with the passed attributes and returns an instance.
139
     *
140
     * @param array $attr The attribute attributes
141
     *
142
     * @return array The initialized attribute
143
     */
144
    protected function initializeAttribute(array $attr)
145
    {
146
        return $attr;
147
    }
148
149
    /**
150
     * Return's the attribute bunch processor instance.
151
     *
152
     * @return \TechDivision\Import\Attribute\Services\AttributeBunchProcessorInterface The attribute bunch processor instance
153
     */
154
    protected function getAttributeBunchProcessor()
155
    {
156
        return $this->attributeBunchProcessor;
157
    }
158
159
    /**
160
     * Map's the passed attribute code to the attribute ID that has been created recently.
161
     *
162
     * @param string $attributeCode The attribute code that has to be mapped
163
     *
164
     * @return void
165
     */
166
    protected function addAttributeCodeIdMapping($attributeCode)
167
    {
168
        $this->getSubject()->addAttributeCodeIdMapping($attributeCode);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface TechDivision\Import\Subjects\SubjectInterface as the method addAttributeCodeIdMapping() does only exist in the following implementations of said interface: TechDivision\Import\Attr...e\Subjects\BunchSubject.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

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

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
169
    }
170
171
    /**
172
     * Queries whether or not the attribute with the passed code has already been processed.
173
     *
174
     * @param string $attributeCode The attribute code to check
175
     *
176
     * @return boolean TRUE if the path has been processed, else FALSE
177
     */
178
    protected function hasBeenProcessed($attributeCode)
179
    {
180
        return $this->getSubject()->hasBeenProcessed($attributeCode);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface TechDivision\Import\Subjects\SubjectInterface as the method hasBeenProcessed() does only exist in the following implementations of said interface: TechDivision\Import\Attr...e\Subjects\BunchSubject, TechDivision\Import\Attr...\Subjects\OptionSubject.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

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

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
181
    }
182
183
    /**
184
     * Set's the ID of the attribute that has been created recently.
185
     *
186
     * @param integer $lastAttributeId The attribute ID
187
     *
188
     * @return void
189
     */
190
    protected function setLastAttributeId($lastAttributeId)
191
    {
192
        $this->getSubject()->setLastAttributeId($lastAttributeId);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface TechDivision\Import\Subjects\SubjectInterface as the method setLastAttributeId() does only exist in the following implementations of said interface: TechDivision\Import\Attr...e\Subjects\BunchSubject.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

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

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
193
    }
194
195
    /**
196
     * Persist the passed attribute.
197
     *
198
     * @param array $attribute The attribute to persist
199
     *
200
     * @return void
201
     */
202
    protected function persistAttribute(array $attribute)
203
    {
204
        return $this->getAttributeBunchProcessor()->persistAttribute($attribute);
205
    }
206
207
    /**
208
     * Return's the entity type for the passed code.
209
     *
210
     * @param string $entityTypeCode The entity type code
211
     *
212
     * @return array The requested entity type
213
     * @throws \Exception Is thrown, if the entity type with the passed code is not available
214
     */
215
    protected function getEntityType($entityTypeCode)
216
    {
217
        return $this->getSubject()->getEntityType($entityTypeCode);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface TechDivision\Import\Subjects\SubjectInterface as the method getEntityType() does only exist in the following implementations of said interface: TechDivision\Import\Attr...bstractAttributeSubject, TechDivision\Import\Attr...e\Subjects\BunchSubject, TechDivision\Import\Attr...\Subjects\OptionSubject.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

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

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
218
    }
219
}
220