FileUploadObserver::getTargetColumn()   A
last analyzed

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
c 0
b 0
f 0
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
/**
4
 * TechDivision\Import\Product\Magic360\Observers\FileUploadObserver
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
 * @author    Bernhard Wick <[email protected]>
16
 * @copyright 2017 TechDivision GmbH <[email protected]>
17
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
18
 * @link      https://github.com/techdivision/import-product-magic360
19
 * @link      http://www.techdivision.com
20
 */
21
22
namespace TechDivision\Import\Product\Magic360\Observers;
23
24
use TechDivision\Import\Subjects\SubjectInterface;
25
use TechDivision\Import\Observers\AbstractObserver;
26
use TechDivision\Import\Product\Magic360\Utils\ColumnKeys;
27
28
/**
29
 * Observer that uploads the file specified in a CSV file's column 'image_path' to a
30
 * configurable directoy.
31
 *
32
 * @author    Tim Wagner <[email protected]>
33
 * @author    Bernhard Wick <[email protected]>
34
 * @copyright 2017 TechDivision GmbH <[email protected]>
35
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
36
 * @link      https://github.com/techdivision/import-product-magic360
37
 * @link      http://www.techdivision.com
38
 */
39
class FileUploadObserver extends AbstractObserver
40
{
41
42
    /**
43
     * Will be invoked by the action on the events the listener has been registered for.
44
     *
45
     * @param \TechDivision\Import\Subjects\SubjectInterface $subject The subject instance
46
     *
47
     * @return array The modified row
48
     * @see \TechDivision\Import\Observers\ObserverInterface::handle()
49
     */
50
    public function handle(SubjectInterface $subject)
51
    {
52
53
        // initialize the row
54
        $this->setSubject($subject);
55
        $this->setRow($subject->getRow());
56
57
        // process the functionality and return the row
58
        $this->process();
59
60
        // return the processed row
61
        return $this->getRow();
62
    }
63
64
    /**
65
     * Process the observer's business logic.
66
     *
67
     * @return array The processed row
68
     */
69
    protected function process()
70
    {
71
72
        // query whether or not, the image changed
73
        if ($this->isParentImage($image = $this->getValue($this->getSourceColumn()))) {
74
            return;
75
        }
76
77
        // initialize the image path
78
        $imagePath = $this->getValue($this->getSourceColumn());
79
80
        // load the subjet
81
        $subject = $this->getSubject();
82
83
        // query whether or not we've to upload the image files
84
        if ($subject->hasCopyImages()) {
85
            // upload the file and set the new image path
86
            $imagePath = $subject->uploadFile($image);
87
88
            // log a message that the image has been copied
89
            $subject->getSystemLogger()->debug(
90
                sprintf('Successfully copied image %s => %s', $image, $imagePath)
91
            );
92
        }
93
94
        // write the real image path to the target column
95
        $this->setValue($this->getTargetColumn(), $imagePath);
96
    }
97
98
    /**
99
     * Return's TRUE if the passed image is the parent one.
100
     *
101
     * @param string $image The imageD to check
102
     *
103
     * @return boolean TRUE if the passed image is the parent one
104
     */
105
    protected function isParentImage($image)
106
    {
107
        return $this->getSubject()->getParentImage() === $image;
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 getParentImage() does only exist in the following implementations of said interface: TechDivision\Import\Prod...ubjects\Magic360Subject.

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...
108
    }
109
110
    /**
111
     * Return's the name of the source column with the image path.
112
     *
113
     * @return string The image path
114
     */
115
    protected function getSourceColumn()
116
    {
117
        return ColumnKeys::IMAGE_PATH;
118
    }
119
120
    /**
121
     * Return's the target column with the path of the copied image.
122
     *
123
     * @return string The path to the copied image
124
     */
125
    protected function getTargetColumn()
126
    {
127
        return ColumnKeys::IMAGE_PATH_NEW;
128
    }
129
}
130