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

FileUploadTrait::getMediaDir()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
/**
4
 * TechDivision\Import\Subjects\FileUploadTrait
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\Subjects;
22
23
/**
24
 * The trait implementation for the file upload functionality.
25
 *
26
 * @author    Tim Wagner <[email protected]>
27
 * @copyright 2016 TechDivision GmbH <[email protected]>
28
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
29
 * @link      https://github.com/techdivision/import
30
 * @link      http://www.techdivision.com
31
 */
32
trait FileUploadTrait
33
{
34
35
    /**
36
     * The directory with the Magento media files => target directory for images (relative to the root directory).
37
     *
38
     * @var string
39
     */
40
    protected $mediaDir;
41
42
    /**
43
     * The directory with the images that have to be imported (relative to the root directory).
44
     *
45
     * @var string
46
     */
47
    protected $imagesFileDir;
48
49
    /**
50
     * The name of the craeted image.
51
     *
52
     * @var integer
53
     */
54
    protected $parentImage;
55
56
    /**
57
     * The flag whether to copy the images or not.
58
     *
59
     * @var boolean
60
     */
61
    protected $copyImages;
62
63
    /**
64
     * Set's the flag to copy the images or not.
65
     *
66
     * @param boolean $copyImages The flag
67
     *
68
     * @return void
69
     */
70 1
    public function setCopyImages($copyImages)
71
    {
72 1
        $this->copyImages = $copyImages;
73 1
    }
74
75
    /**
76
     * Return's the flag to copy images or not.
77
     *
78
     * @return boolean The flag
79
     */
80 1
    public function hasCopyImages()
81
    {
82 1
        return $this->copyImages;
83
    }
84
85
    /**
86
     * Set's directory with the Magento media files => target directory for images.
87
     *
88
     * @param string $mediaDir The directory with the Magento media files => target directory for images
89
     *
90
     * @return void
91
     */
92 3
    public function setMediaDir($mediaDir)
93
    {
94 3
        $this->mediaDir = $mediaDir;
95 3
    }
96
97
    /**
98
     * Return's the directory with the Magento media files => target directory for images.
99
     *
100
     * @return string The directory with the Magento media files => target directory for images
101
     */
102 3
    public function getMediaDir()
103
    {
104 3
        return $this->mediaDir;
105
    }
106
107
    /**
108
     * Set's directory with the images that have to be imported.
109
     *
110
     * @param string $imagesFileDir The directory with the images that have to be imported
111
     *
112
     * @return void
113
     */
114 3
    public function setImagesFileDir($imagesFileDir)
115
    {
116 3
        $this->imagesFileDir = $imagesFileDir;
117 3
    }
118
119
    /**
120
     * Return's the directory with the images that have to be imported.
121
     *
122
     * @return string The directory with the images that have to be imported
123
     */
124 3
    public function getImagesFileDir()
125
    {
126 3
        return $this->imagesFileDir;
127
    }
128
129
    /**
130
     * Set's the name of the created image.
131
     *
132
     * @param string $parentImage The name of the created image
133
     *
134
     * @return void
135
     */
136 1
    public function setParentImage($parentImage)
137
    {
138 1
        $this->parentImage = $parentImage;
0 ignored issues
show
Documentation Bug introduced by
The property $parentImage was declared of type integer, but $parentImage is of type string. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
139 1
    }
140
141
    /**
142
     * Return's the name of the created image.
143
     *
144
     * @return string The name of the created image
145
     */
146 1
    public function getParentImage()
147
    {
148 1
        return $this->parentImage;
149
    }
150
151
    /**
152
     * Get new file name if the same is already exists.
153
     *
154
     * @param string $targetFilename The name of the exisising files
155
     *
156
     * @return string The new filename
157
     */
158 3
    public function getNewFileName($targetFilename)
159
    {
160
161
        // load the file information
162 3
        $fileInfo = pathinfo($targetFilename);
163
164
        // query whether or not, the file exists
165 3
        if ($this->getFilesystem()->has($targetFilename)) {
0 ignored issues
show
Bug introduced by
It seems like getFilesystem() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
166
            // initialize the incex and the basename
167 1
            $index = 1;
168 1
            $baseName = $fileInfo['filename'] . '.' . $fileInfo['extension'];
169
170
            // prepare the new filename by raising the index
171 1
            while ($this->getFilesystem()->has($fileInfo['dirname'] . '/' . $baseName)) {
0 ignored issues
show
Bug introduced by
It seems like getFilesystem() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
172 1
                $baseName = $fileInfo['filename'] . '_' . $index . '.' . $fileInfo['extension'];
173 1
                $index++;
174
            }
175
176
            // set the new filename
177 1
            $targetFilename = $baseName;
178
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
179
        } else {
180
            // if not, simply return the filename
181 2
            return $fileInfo['basename'];
182
        }
183
184
        // return the new filename
185 1
        return $targetFilename;
186
    }
187
188
    /**
189
     * Upload's the file with the passed name to the Magento
190
     * media directory. If the file already exists, the will
191
     * be given a new name that will be returned.
192
     *
193
     * @param string $filename The name of the file to be uploaded
194
     *
195
     * @return string The name of the uploaded file
196
     * @throws \Exception Is thrown, if the file with the passed name is not available
197
     */
198 2
    public function uploadFile($filename)
199
    {
200
201
        // trim the leading /, if available
202 2
        $trimmedFilename = ltrim($filename, '/');
203 2
        $mediaDir = ltrim($this->getMediaDir(), '/');
204 2
        $imagesFileDir = ltrim($this->getImagesFileDir(), '/');
205
206
        // prepare source/target filename
207 2
        $sourceFilename = sprintf('%s/%s', $imagesFileDir, $trimmedFilename);
208 2
        $targetFilename = sprintf('%s/%s', $mediaDir, $trimmedFilename);
209
210
        // query whether or not the image file to be imported is available
211 2
        if (!$this->getFilesystem()->has($sourceFilename)) {
0 ignored issues
show
Bug introduced by
It seems like getFilesystem() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
212 1
            throw new \Exception(sprintf('Media file %s not available', $sourceFilename));
213
        }
214
215
        // prepare the target filename, if necessary
216 1
        $newTargetFilename = $this->getNewFileName($targetFilename);
217 1
        $targetFilename = str_replace(basename($targetFilename), $newTargetFilename, $targetFilename);
218
219
        // copy the image to the target directory
220 1
        $this->getFilesystem()->copy($sourceFilename, $targetFilename);
0 ignored issues
show
Bug introduced by
It seems like getFilesystem() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
221
222
        // return the new target filename
223 1
        return str_replace($mediaDir, '', $targetFilename);
224
    }
225
}
226