Completed
Pull Request — master (#40)
by Tim
05:25
created

AbstractFileUploadObserver::uploadFile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 2
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\Observers\AbstractObserver;
24
25
/**
26
 * Abstract observer that uploads the file specified in a CSV file's column
27
 * 'image_path' to a configurable directory.
28
 *
29
 * @author    Tim Wagner <[email protected]>
30
 * @copyright 2016 TechDivision GmbH <[email protected]>
31
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
32
 * @link      https://github.com/techdivision/import
33
 * @link      http://www.techdivision.com
34
 */
35
abstract class AbstractFileUploadObserver extends AbstractObserver
36
{
37
38
    /**
39
     * Will be invoked by the action on the events the listener has been registered for.
40
     *
41
     * @param array $row The row to handle
42
     *
43
     * @return array The modified row
44
     * @see \TechDivision\Import\Product\Observers\ImportObserverInterface::handle()
45
     */
46
    public function handle(array $row)
47
    {
48
49
        // initialize the row
50
        $this->setRow($row);
51
52
        // process the functionality and return the row
53
        $this->process();
54
55
        // return the processed row
56
        return $this->getRow();
57
    }
58
59
    /**
60
     * Return's the name of the source column with the image path.
61
     *
62
     * @return string The image path
63
     */
64
    abstract protected function getSourceColumn();
65
66
    /**
67
     * Return's the target column with the path of the copied image.
68
     *
69
     * @return string The path to the copied image
70
     */
71
    abstract protected function getTargetColumn();
72
73
    /**
74
     * Process the observer's business logic.
75
     *
76
     * @return array The processed row
77
     */
78
    protected function process()
79
    {
80
81
        // query whether or not, the image changed
82
        if ($this->isParentImage($image = $this->getValue($this->getSourceColumn()))) {
83
            return;
84
        }
85
86
        // initialize the image path
87
        $imagePath = $this->getValue($this->getSourceColumn());
88
89
        // query whether or not we've to upload the image files
90
        if ($this->hasCopyImages()) {
91
            // upload the file and set the new image path
92
            $imagePath = $this->uploadFile($image);
93
94
            // log a message that the image has been copied
95
            $this->getSystemLogger()->debug(
96
                sprintf(
97
                    'Successfully copied image %s => %s',
98
                    $image,
99
                    $imagePath
100
                )
101
            );
102
        }
103
104
        // write the real image path to the target column
105
        $this->setValue($this->getTargetColumn(), $imagePath);
106
    }
107
108
    /**
109
     * Return's TRUE if the passed image is the parent one.
110
     *
111
     * @param string $image The imageD to check
112
     *
113
     * @return boolean TRUE if the passed image is the parent one
114
     */
115
    protected function isParentImage($image)
116
    {
117
        return $this->getParentImage() === $image;
118
    }
119
120
    /**
121
     * Upload's the file with the passed name to the Magento
122
     * media directory. If the file already exists, the will
123
     * be given a new name that will be returned.
124
     *
125
     * @param string $filename The name of the file to be uploaded
126
     *
127
     * @return string The name of the uploaded file
128
     */
129
    protected function uploadFile($filename)
130
    {
131
        return $this->getSubject()->uploadFile($filename);
132
    }
133
134
    /**
135
     * Return's the name of the created image.
136
     *
137
     * @return string The name of the created image
138
     */
139
    protected function getParentImage()
140
    {
141
        return $this->getSubject()->getParentImage();
142
    }
143
144
    /**
145
     * Return's the flag to copy images or not.
146
     *
147
     * @return booleas The flag
148
     */
149
    protected function hasCopyImages()
150
    {
151
        return $this->getSubject()->hasCopyImages();
152
    }
153
}
154