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