Completed
Push — 18.x ( ac68fb )
by Marcus
06:04
created

AttributeOptionSwatchFileUploadObserver::process()   A

Complexity

Conditions 5
Paths 3

Size

Total Lines 36

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
dl 0
loc 36
ccs 0
cts 25
cp 0
rs 9.0328
c 0
b 0
f 0
cc 5
nc 3
nop 0
crap 30
1
<?php
2
3
/**
4
 * TechDivision\Import\Attribute\Observers\AttributeOptionSwatchFileUploadObserver
5
 *
6
 * @author    Marcus Döllerer <[email protected]>
7
 * @copyright 2020 TechDivision GmbH <[email protected]>
8
 * @link      https://www.techdivision.com
9
 */
10
11
namespace TechDivision\Import\Attribute\Observers;
12
13
use TechDivision\Import\Attribute\Utils\ConfigurationKeys;
14
use TechDivision\Import\Attribute\Utils\MemberNames;
15
16
/**
17
 * Abstract attribute observer that handles files of visual swatches during import.
18
 *
19
 * @author    Marcus Döllerer <[email protected]>
20
 * @copyright 2020 TechDivision GmbH <[email protected]>
21
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
22
 * @link      https://github.com/techdivision/import-attribute
23
 * @link      http://www.techdivision.com
24
 */
25
class AttributeOptionSwatchFileUploadObserver extends AttributeOptionSwatchUpdateObserver
26
{
27
28
    /**
29
     * Process the observer's business logic.
30
     *
31
     * @return void
32
     */
33
    protected function process()
34
    {
35
        // skip this step if the configuration value 'copy-images' is undefined or set to 'false'
36
        if (!$this->getSubject()->getConfiguration()->hasParam(ConfigurationKeys::COPY_IMAGES) ||
37
            !$this->getSubject()->getConfiguration()->getParam(ConfigurationKeys::COPY_IMAGES)) {
38
            return;
39
        }
40
41
        // initialize the option swatch attribute
42
        $attributeOptionSwatch = $this->initializeAttribute(array(
43
            MemberNames::OPTION_ID  => $this->getLastOptionId()
44
        ));
45
46
        // skip this step for color swatches and text swatches
47
        if (!isset($attributeOptionSwatch['type']) || $attributeOptionSwatch['type'] !== '2') {
48
            return;
49
        }
50
51
        // upload the file to the configured directory
52
        $imagePath = $this->getSubject()->uploadFile($attributeOptionSwatch['value']);
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 uploadFile() does only exist in the following implementations of said interface: TechDivision\Import\Attr...\Subjects\OptionSubject, TechDivision\Import\Obse...s\FileUploadSubjectImpl.

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...
53
54
        // inject the new image path and update the attribute option swatch
55
        $attributeOptionSwatch['value'] = $imagePath;
56
        $this->getAttributeBunchProcessor()->persistAttributeOptionSwatch($attributeOptionSwatch);
57
58
        // add debug log entry
59
        $this->getSubject()
60
            ->getSystemLogger()
61
            ->debug(
62
                sprintf(
63
                    'Successfully copied image %s for swatch with id %s',
64
                    $imagePath,
65
                    $attributeOptionSwatch['swatch_id']
66
                )
67
            );
68
    }
69
}
70