1 <?php
2
3 /* --------------------------------------------------------------
4 ProductImageFileStorage.inc.php 2016-02-15
5 Gambio GmbH
6 http://www.gambio.de
7 Copyright (c) 2016 Gambio GmbH
8 Released under the GNU General Public License (Version 2)
9 [http://www.gnu.org/licenses/gpl-2.0.html]
10 --------------------------------------------------------------
11 */
12
13 /**
14 * Class ProductImageFileStorage
15 *
16 * @category System
17 * @package Product
18 * @subpackage Storages
19 */
20 class ProductImageFileStorage extends ImageFileStorage
21 {
22 /**
23 * Settings.
24 *
25 * @var EnvProductImageFileStorageSettings
26 */
27 protected $settings;
28
29 /**
30 * Original Dir (The original_images folder).
31 *
32 * @var WritableDirectory
33 */
34 protected $originalDir;
35
36 /**
37 * Processing.
38 *
39 * @var ProductImageProcessingInterface
40 */
41 protected $processing;
42
43
44 public function __construct(ProductImagePathsSettingsInterface $settings,
45 ProductImageProcessingInterface $processing)
46 {
47 $this->originalDir = MainFactory::create('WritableDirectory', $settings->getProductOriginalImagesDirPath());
48 parent::__construct($this->originalDir);
49 $this->settings = $settings;
50 $this->processing = $processing;
51 }
52
53
54 /**
55 * Import File.
56 *
57 * Saves an image to a writable directory.
58 *
59 * @param ExistingFile $sourceFile The source file to import.
60 * @param FilenameStringType $preferredFilename The preferred name of the file to be saved.
61 *
62 * @throws InvalidArgumentException If the provided source file of the preferred filename is not valid.
63 *
64 * @return string Preferred filename
65 */
66 public function importFile(ExistingFile $sourceFile, FilenameStringType $preferredFilename)
67 {
68 $filename = parent::importFile($sourceFile, $preferredFilename);
69
70 $filename = new FilenameStringType($filename);
71
72 $this->processing->proceedImage($filename);
73
74 return $filename->asString();
75 }
76
77
78 /**
79 * Rename File
80 *
81 * Renames an existing image file.
82 *
83 * @param FilenameStringType $oldName The old name of the file.
84 * @param FilenameStringType $newName The new name of the file.
85 *
86 * @throws InvalidArgumentException If the file that should be renamed does not exists.
87 * @throws InvalidArgumentException If a file with the preferred name already exists.
88 * @throws FileNotFoundException If processed product image does not exists.
89 *
90 * @return ProductImageFileStorage Same instance for chained method calls.
91 */
92 public function renameFile(FilenameStringType $oldName, FilenameStringType $newName)
93 {
94 parent::renameFile($oldName, $newName);
95
96 // Product images folders excluding the original images folder because it will
97 // be handled in the parent renameFile method.
98 $productImageFoldersExcludingOriginalImages = array(
99 $this->settings->getProductGalleryImagesDirPath(),
100 $this->settings->getProductInfoImagesDirPath(),
101 $this->settings->getProductPopupImagesDirPath(),
102 $this->settings->getProductThumbnailImagesDirPath()
103 );
104
105 foreach($productImageFoldersExcludingOriginalImages as $folder)
106 {
107 if(!file_exists($folder . $oldName->asString()))
108 {
109 throw new FileNotFoundException($oldName->asString() . ' does not exist in ' . $folder);
110 }
111
112 rename($folder . $oldName->asString(), $folder . $newName->asString());
113 }
114
115 return $this;
116 }
117
118
119 /**
120 * Delete File.
121 *
122 * Deletes an existing file.
123 *
124 * @param FilenameStringType $filename The file to delete.
125 *
126 * @throws InvalidArgumentException If the provided filename is not valid.
127 *
128 * @return ProductImageFileStorage Same instance for chained method calls.
129 */
130 public function deleteFile(FilenameStringType $filename)
131 {
132 parent::deleteFile($filename);
133
134 // Product images folders, excluding the original images folder, because it will
135 // be handled by the parent deleteFile method.
136 $productImageFoldersExcludingOriginalImages = array(
137 $this->settings->getProductGalleryImagesDirPath(),
138 $this->settings->getProductInfoImagesDirPath(),
139 $this->settings->getProductPopupImagesDirPath(),
140 $this->settings->getProductThumbnailImagesDirPath()
141 );
142
143 foreach($productImageFoldersExcludingOriginalImages as $folder)
144 {
145 $filepath = $folder . $filename->asString();
146 if(file_exists($filepath) && !is_dir($filepath))
147 {
148
149 unlink($folder . $filename->asString());
150 }
151 }
152
153 return $this;
154 }
155 }