Completed
Pull Request — master (#16)
by Tim
07:10
created

AttributeObserver::isForceDefaultValues()   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
rs 10
c 0
b 0
f 0
cc 1
eloc 2
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
     * Prepare the attributes of the entity that has to be persisted.
79
     *
80
     * @return array The prepared attributes
81
     */
82
    protected function prepareAttributes()
83
    {
84
85
        // map the entity type code to the ID
86
        $entityType = $this->getEntityType($this->getValue(ColumnKeys::ENTITY_TYPE_CODE));
87
        $entityTypeId = $entityType[MemberNames::ENTITY_TYPE_ID];
88
89
        // load the data from the row
90
        $attributeCode = $this->getValue(ColumnKeys::ATTRIBUTE_CODE);
91
        $attributeModel = $this->getValue(ColumnKeys::ATTRIBUTE_MODEL);
92
        $backendModel = $this->getValue(ColumnKeys::BACKEND_MODEL);
93
        $backendType = $this->getValue(ColumnKeys::BACKEND_TYPE, BackendTypeKeys::BACKEND_TYPE_STATIC);
94
        $backendTable = $this->getValue(ColumnKeys::BACKEND_TABLE);
95
        $frontendModel = $this->getValue(ColumnKeys::FRONTEND_MODEL);
96
        $frontendInput = $this->getValue(ColumnKeys::FRONTEND_INPUT);
97
        $frontendLabel = $this->getValue(ColumnKeys::FRONTEND_LABEL);
98
        $frontendClass = $this->getValue(ColumnKeys::FRONTEND_CLASS);
99
        $sourceModel = $this->getValue(ColumnKeys::SOURCE_MODEL);
100
        $isRequired = $this->getValue(ColumnKeys::IS_REQUIRED, 0);
101
        $isUserDefined = $this->getValue(ColumnKeys::IS_USER_DEFINED, 1);
102
        $defaultValue = $this->getValue(ColumnKeys::DEFAULT_VALUE);
103
        $isUnique = $this->getValue(ColumnKeys::IS_UNIQUE, 0);
104
        $note = $this->getValue(ColumnKeys::NOTE);
105
106
        // return the prepared product
107
        return $this->initializeEntity(
108
            array(
109
                MemberNames::ENTITY_TYPE_ID  => $entityTypeId,
110
                MemberNames::ATTRIBUTE_CODE  => $attributeCode,
111
                MemberNames::ATTRIBUTE_MODEL => $attributeModel,
112
                MemberNames::BACKEND_MODEL   => $backendModel,
113
                MemberNames::BACKEND_TYPE    => $backendType,
114
                MemberNames::BACKEND_TABLE   => $backendTable,
115
                MemberNames::FRONTEND_MODEL  => $frontendModel,
116
                MemberNames::FRONTEND_INPUT  => $frontendInput,
117
                MemberNames::FRONTEND_LABEL  => $frontendLabel,
118
                MemberNames::FRONTEND_CLASS  => $frontendClass,
119
                MemberNames::SOURCE_MODEL    => $sourceModel,
120
                MemberNames::IS_REQUIRED     => $isRequired,
121
                MemberNames::IS_USER_DEFINED => $isUserDefined,
122
                MemberNames::DEFAULT_VALUE   => $defaultValue,
123
                MemberNames::IS_UNIQUE       => $isUnique,
124
                MemberNames::NOTE            => $note
125
            )
126
        );
127
    }
128
129
    /**
130
     * Initialize the attribute with the passed attributes and returns an instance.
131
     *
132
     * @param array $attr The attribute attributes
133
     *
134
     * @return array The initialized attribute
135
     */
136
    protected function initializeAttribute(array $attr)
137
    {
138
        return $attr;
139
    }
140
141
    /**
142
     * Return's the attribute bunch processor instance.
143
     *
144
     * @return \TechDivision\Import\Attribute\Services\AttributeBunchProcessorInterface The attribute bunch processor instance
145
     */
146
    protected function getAttributeBunchProcessor()
147
    {
148
        return $this->attributeBunchProcessor;
149
    }
150
151
    /**
152
     * Map's the passed attribute code to the attribute ID that has been created recently.
153
     *
154
     * @param string $attributeCode The attribute code that has to be mapped
155
     *
156
     * @return void
157
     */
158
    protected function addAttributeCodeIdMapping($attributeCode)
159
    {
160
        $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...
161
    }
162
163
    /**
164
     * Queries whether or not the attribute with the passed code has already been processed.
165
     *
166
     * @param string $attributeCode The attribute code to check
167
     *
168
     * @return boolean TRUE if the path has been processed, else FALSE
169
     */
170
    protected function hasBeenProcessed($attributeCode)
171
    {
172
        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...
173
    }
174
175
    /**
176
     * Set's the ID of the attribute that has been created recently.
177
     *
178
     * @param integer $lastAttributeId The attribute ID
179
     *
180
     * @return void
181
     */
182
    protected function setLastAttributeId($lastAttributeId)
183
    {
184
        $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...
185
    }
186
187
    /**
188
     * Persist the passed attribute.
189
     *
190
     * @param array $attribute The attribute to persist
191
     *
192
     * @return void
193
     */
194
    protected function persistAttribute(array $attribute)
195
    {
196
        return $this->getAttributeBunchProcessor()->persistAttribute($attribute);
197
    }
198
199
    /**
200
     * Return's the entity type for the passed code.
201
     *
202
     * @param string $entityTypeCode The entity type code
203
     *
204
     * @return array The requested entity type
205
     * @throws \Exception Is thrown, if the entity type with the passed code is not available
206
     */
207
    protected function getEntityType($entityTypeCode)
208
    {
209
        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...
210
    }
211
}
212