Completed
Push — master ( 2a896c...01321b )
by Tim
8s
created

FileUploadObserver   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A process() 0 11 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
     * Process the observer's business logic.
41
     *
42
     * @return array The processed row
43
     */
44
    protected function process()
45
    {
46
47
        // query whether or not, the image changed
48
        if ($this->isParentImage($image = $this->getValue(ColumnKeys::IMAGE_PATH))) {
49
            return;
50
        }
51
52
        // upload the image file and set the new filename
53
        $this->setValue(ColumnKeys::IMAGE_PATH_NEW, $this->uploadFile($image));
54
    }
55
56
    /**
57
     * Upload's the file with the passed name to the Magento
58
     * media directory. If the file already exists, the will
59
     * be given a new name that will be returned.
60
     *
61
     * @param string $filename The name of the file to be uploaded
62
     *
63
     * @return string The name of the uploaded file
64
     */
65
    protected function uploadFile($filename)
66
    {
67
        return $this->getSubject()->uploadFile($filename);
68
    }
69
70
    /**
71
     * Return's the name of the created image.
72
     *
73
     * @return string The name of the created image
74
     */
75
    protected function getParentImage()
76
    {
77
        return $this->getSubject()->getParentImage();
78
    }
79
80
    /**
81
     * Return's TRUE if the passed image is the parent one.
82
     *
83
     * @param string $image The imageD to check
84
     *
85
     * @return boolean TRUE if the passed image is the parent one
86
     */
87
    protected function isParentImage($image)
88
    {
89
        return $this->getParentImage() === $image;
90
    }
91
}
92