Completed
Pull Request — master (#89)
by Tim
07:38
created

AbstractFileUploadObserver   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 3
dl 0
loc 85
ccs 20
cts 20
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A handle() 0 13 1
B process() 0 28 3
A isParentImage() 0 4 1
getSourceColumn() 0 1 ?
getTargetColumn() 0 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
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 \TechDivision\Import\Subjects\SubjectInterface $subject The subject instance
42
     *
43
     * @return array The modified row
44
     * @see \TechDivision\Import\Observers\ObserverInterface::handle()
45
     */
46 2
    public function handle(SubjectInterface $subject)
47
    {
48
49
        // initialize the row
50 2
        $this->setSubject($subject);
51 2
        $this->setRow($subject->getRow());
52
53
        // process the functionality and return the row
54 2
        $this->process();
55
56
        // return the processed row
57 2
        return $this->getRow();
58
    }
59
60
    /**
61
     * Process the observer's business logic.
62
     *
63
     * @return array The processed row
64
     */
65 2
    protected function process()
66
    {
67
68
        // query whether or not, the image changed
69 2
        if ($this->isParentImage($image = $this->getValue($this->getSourceColumn()))) {
70 1
            return;
71
        }
72
73
        // initialize the image path
74 1
        $imagePath = $this->getValue($this->getSourceColumn());
75
76
        // load the subjet
77 1
        $subject = $this->getSubject();
78
79
        // query whether or not we've to upload the image files
80 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...
81
            // upload the file and set the new image path
82 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...
83
84
            // log a message that the image has been copied
85 1
            $subject->getSystemLogger()->debug(
86 1
                sprintf('Successfully copied image %s => %s', $image, $imagePath)
87 1
            );
88 1
        }
89
90
        // write the real image path to the target column
91 1
        $this->setValue($this->getTargetColumn(), $imagePath);
92 1
    }
93
94
    /**
95
     * Return's TRUE if the passed image is the parent one.
96
     *
97
     * @param string $image The imageD to check
98
     *
99
     * @return boolean TRUE if the passed image is the parent one
100
     */
101 2
    protected function isParentImage($image)
102
    {
103 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...
104
    }
105
106
    /**
107
     * Return's the name of the source column with the image path.
108
     *
109
     * @return string The image path
110
     */
111
    abstract protected function getSourceColumn();
112
113
    /**
114
     * Return's the target column with the path of the copied image.
115
     *
116
     * @return string The path to the copied image
117
     */
118
    abstract protected function getTargetColumn();
119
}
120