Completed
Pull Request — master (#2)
by Tim
03:26
created

FileUploadObserver   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 1
dl 0
loc 65
ccs 0
cts 21
cp 0
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A handle() 0 17 2
A uploadFile() 0 4 1
A getParentImage() 0 4 1
A isParentImage() 0 4 1
1
<?php
2
3
/**
4
 * TechDivision\Import\Product\Media\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-product-media
18
 * @link      http://www.techdivision.com
19
 */
20
21
namespace TechDivision\Import\Product\Media\Observers;
22
23
use TechDivision\Import\Product\Media\Utils\ColumnKeys;
24
use TechDivision\Import\Product\Observers\AbstractProductImportObserver;
25
26
/**
27
 * Observer that uploads the file specified in a CSV file's column 'image_path' to a
28
 * configurable directoy.
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-product-media
34
 * @link      http://www.techdivision.com
35
 */
36
class FileUploadObserver extends AbstractProductImportObserver
37
{
38
39
    /**
40
     * Will be invoked by the action on the events the listener has been registered for.
41
     *
42
     * @param array $row The row to handle
43
     *
44
     * @return array The modified row
45
     * @see \TechDivision\Import\Product\Observers\ImportObserverInterface::handle()
46
     */
47
    public function handle(array $row)
48
    {
49
50
        // load the header information
51
        $headers = $this->getHeaders();
52
53
        // query whether or not, the image changed
54
        if ($this->isParentImage($image = $row[$headers[ColumnKeys::IMAGE_PATH]])) {
55
            return $row;
56
        }
57
58
        // upload the image file and set the new filename
59
        $row[$headers[ColumnKeys::IMAGE_PATH_NEW]] = $this->uploadFile($image);
60
61
        // returns the row
62
        return $row;
63
    }
64
65
    /**
66
     * Upload's the file with the passed name to the Magento
67
     * media directory. If the file already exists, the will
68
     * be given a new name that will be returned.
69
     *
70
     * @param string $filename The name of the file to be uploaded
71
     *
72
     * @return string The name of the uploaded file
73
     */
74
    public function uploadFile($filename)
75
    {
76
        return $this->getSubject()->uploadFile($filename);
77
    }
78
79
    /**
80
     * Return's the name of the created image.
81
     *
82
     * @return string The name of the created image
83
     */
84
    public function getParentImage()
85
    {
86
        return $this->getSubject()->getParentImage();
87
    }
88
89
    /**
90
     * Return's TRUE if the passed image is the parent one.
91
     *
92
     * @param string $image The imageD to check
93
     *
94
     * @return boolean TRUE if the passed image is the parent one
95
     */
96
    public function isParentImage($image)
97
    {
98
        return $this->getParentImage() === $image;
99
    }
100
}
101