Completed
Push — master ( d5f4e5...716f0e )
by Tim
12s
created

FileUploadTrait   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 199
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 2
dl 0
loc 199
ccs 0
cts 61
cp 0
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A setCopyImages() 0 4 1
A hasCopyImages() 0 4 1
A setMediaDir() 0 4 1
A getMediaDir() 0 4 1
A setImagesFileDir() 0 4 1
A getImagesFileDir() 0 4 1
A setParentImage() 0 4 1
A getParentImage() 0 4 1
B getNewFileName() 0 29 3
B uploadFile() 0 25 2
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 trait that provides basic filesystem handling functionality.
37
     *
38
     * @var TechDivision\Import\Subjects\FilesystemTrait
39
     */
40
    use FilesystemTrait;
41
42
    /**
43
     * The directory with the Magento media files => target directory for images (relative to the root directory).
44
     *
45
     * @var string
46
     */
47
    protected $mediaDir;
48
49
    /**
50
     * The directory with the images that have to be imported (relative to the root directory).
51
     *
52
     * @var string
53
     */
54
    protected $imagesFileDir;
55
56
    /**
57
     * The name of the craeted image.
58
     *
59
     * @var integer
60
     */
61
    protected $parentImage;
62
63
    /**
64
     * The flag whether to copy the images or not.
65
     *
66
     * @var boolean
67
     */
68
    protected $copyImages;
69
70
    /**
71
     * Set's the flag to copy the images or not.
72
     *
73
     * @param boolean $copyImages The flag
74
     *
75
     * @return void
76
     */
77
    public function setCopyImages($copyImages)
78
    {
79
        $this->copyImages = $copyImages;
80
    }
81
82
    /**
83
     * Return's the flag to copy images or not.
84
     *
85
     * @return boolean The flag
86
     */
87
    public function hasCopyImages()
88
    {
89
        return $this->copyImages;
90
    }
91
92
    /**
93
     * Set's directory with the Magento media files => target directory for images.
94
     *
95
     * @param string $mediaDir The directory with the Magento media files => target directory for images
96
     *
97
     * @return void
98
     */
99
    public function setMediaDir($mediaDir)
100
    {
101
        $this->mediaDir = $mediaDir;
102
    }
103
104
    /**
105
     * Return's the directory with the Magento media files => target directory for images.
106
     *
107
     * @return string The directory with the Magento media files => target directory for images
108
     */
109
    public function getMediaDir()
110
    {
111
        return $this->mediaDir;
112
    }
113
114
    /**
115
     * Set's directory with the images that have to be imported.
116
     *
117
     * @param string $imagesFileDir The directory with the images that have to be imported
118
     *
119
     * @return void
120
     */
121
    public function setImagesFileDir($imagesFileDir)
122
    {
123
        $this->imagesFileDir = $imagesFileDir;
124
    }
125
126
    /**
127
     * Return's the directory with the images that have to be imported.
128
     *
129
     * @return string The directory with the images that have to be imported
130
     */
131
    public function getImagesFileDir()
132
    {
133
        return $this->imagesFileDir;
134
    }
135
136
    /**
137
     * Set's the name of the created image.
138
     *
139
     * @param string $parentImage The name of the created image
140
     *
141
     * @return void
142
     */
143
    public function setParentImage($parentImage)
144
    {
145
        $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...
146
    }
147
148
    /**
149
     * Return's the name of the created image.
150
     *
151
     * @return string The name of the created image
152
     */
153
    public function getParentImage()
154
    {
155
        return $this->parentImage;
156
    }
157
158
    /**
159
     * Get new file name if the same is already exists.
160
     *
161
     * @param string $targetFilename The name of the exisising files
162
     *
163
     * @return string The new filename
164
     */
165
    public function getNewFileName($targetFilename)
166
    {
167
168
        // load the file information
169
        $fileInfo = pathinfo($targetFilename);
170
171
        // query whether or not, the file exists
172
        if ($this->getFilesystem()->has($targetFilename)) {
173
            // initialize the incex and the basename
174
            $index = 1;
175
            $baseName = $fileInfo['filename'] . '.' . $fileInfo['extension'];
176
177
            // prepare the new filename by raising the index
178
            while ($this->getFilesystem()->has($fileInfo['dirname'] . '/' . $baseName)) {
179
                $baseName = $fileInfo['filename'] . '_' . $index . '.' . $fileInfo['extension'];
180
                $index++;
181
            }
182
183
            // set the new filename
184
            $targetFilename = $baseName;
185
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
186
        } else {
187
            // if not, simply return the filename
188
            return $fileInfo['basename'];
189
        }
190
191
        // return the new filename
192
        return $targetFilename;
193
    }
194
195
    /**
196
     * Upload's the file with the passed name to the Magento
197
     * media directory. If the file already exists, the will
198
     * be given a new name that will be returned.
199
     *
200
     * @param string $filename The name of the file to be uploaded
201
     *
202
     * @return string The name of the uploaded file
203
     * @throws \Exception Is thrown, if the file with the passed name is not available
204
     */
205
    public function uploadFile($filename)
206
    {
207
208
        // trim the leading /, if available
209
        $trimmedFilename = ltrim($filename, '/');
210
211
        // prepare source/target filename
212
        $sourceFilename = sprintf('%s/%s', $this->getImagesFileDir(), $trimmedFilename);
213
        $targetFilename = sprintf('%s/%s', $this->getMediaDir(), $trimmedFilename);
214
215
        // query whether or not the image file to be imported is available
216
        if (!$this->getFilesystem()->has($sourceFilename)) {
217
            throw new \Exception(sprintf('Media file %s not available', $sourceFilename));
218
        }
219
220
        // prepare the target filename, if necessary
221
        $newTargetFilename = $this->getNewFileName($targetFilename);
222
        $targetFilename = str_replace(basename($targetFilename), $newTargetFilename, $targetFilename);
223
224
        // copy the image to the target directory
225
        $this->getFilesystem()->copy($sourceFilename, $targetFilename);
226
227
        // return the new target filename
228
        return str_replace($this->getMediaDir(), '', $targetFilename);
229
    }
230
}
231