Completed
Pull Request — master (#89)
by Tim
03:51
created

AbstractFileUploadObserver::getSourceColumn()

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 1
ccs 0
cts 0
cp 0
c 0
b 0
f 0
nc 1
1
<?php
2
3
/**
4
 * TechDivision\Import\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
 * @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\Subjects\SubjectInterface;
24
use TechDivision\Import\Observers\AbstractObserver;
25
26
/**
27
 * Abstract observer that uploads the file specified in a CSV file's column
28
 * 'image_path' to a configurable directory.
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
abstract class AbstractFileUploadObserver 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\Product\Observers\ImportObserverInterface::handle()
46
     */
47 2
    public function handle(SubjectInterface $subject)
48
    {
49
50
        // initialize the row
51 2
        $this->setSubject($subject);
52 2
        $this->setRow($subject->getRow());
53
54
        // process the functionality and return the row
55 2
        $this->process();
56
57
        // return the processed row
58 2
        return $this->getRow();
59
    }
60
61
    /**
62
     * Process the observer's business logic.
63
     *
64
     * @return array The processed row
65
     */
66 2
    protected function process()
67
    {
68
69
        // query whether or not, the image changed
70 2
        if ($this->isParentImage($image = $this->getValue($this->getSourceColumn()))) {
71 1
            return;
72
        }
73
74
        // initialize the image path
75 1
        $imagePath = $this->getValue($this->getSourceColumn());
76
77
        // load the subjet
78 1
        $subject = $this->getSubject();
79
80
        // query whether or not we've to upload the image files
81 1
        if ($subject->hasCopyImages()) {
0 ignored issues
show
Bug introduced by
The method hasCopyImages() does not seem to exist on object<TechDivision\Impo...jects\SubjectInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
82
            // upload the file and set the new image path
83 1
            $imagePath = $subject->uploadFile($image);
0 ignored issues
show
Bug introduced by
The method uploadFile() does not seem to exist on object<TechDivision\Impo...jects\SubjectInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
84
85
            // log a message that the image has been copied
86 1
            $subject->getSystemLogger()->debug(
87 1
                sprintf('Successfully copied image %s => %s', $image, $imagePath)
88
            );
89
        }
90
91
        // write the real image path to the target column
92 1
        $this->setValue($this->getTargetColumn(), $imagePath);
93 1
    }
94
95
    /**
96
     * Return's TRUE if the passed image is the parent one.
97
     *
98
     * @param string $image The imageD to check
99
     *
100
     * @return boolean TRUE if the passed image is the parent one
101
     */
102 2
    protected function isParentImage($image)
103
    {
104 2
        return $this->getSubject()->getParentImage() === $image;
0 ignored issues
show
Bug introduced by
The method getParentImage() does not seem to exist on object<TechDivision\Impo...jects\SubjectInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
105
    }
106
107
    /**
108
     * Return's the name of the source column with the image path.
109
     *
110
     * @return string The image path
111
     */
112
    abstract protected function getSourceColumn();
113
114
    /**
115
     * Return's the target column with the path of the copied image.
116
     *
117
     * @return string The path to the copied image
118
     */
119
    abstract protected function getTargetColumn();
120
}
121