Skip to content

Commit a872c4d

Browse files
authored
Merge pull request #1708 from ossinkine/issue-1708
Remove code duplications in UploadableListener
2 parents 95e9d6c + c4a6e53 commit a872c4d

File tree

1 file changed

+31
-22
lines changed

1 file changed

+31
-22
lines changed

lib/Gedmo/Uploadable/UploadableListener.php

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -167,13 +167,7 @@ public function onFlush(EventArgs $args)
167167

168168
if ($config = $this->getConfiguration($om, $meta->name)) {
169169
if (isset($config['uploadable']) && $config['uploadable']) {
170-
if ($config['filePathField']) {
171-
$this->pendingFileRemovals[] = $this->getFilePathFieldValue($meta, $config, $object);
172-
} else {
173-
$path = $this->getPath($meta, $config, $object);
174-
$fileName = $this->getFileNameFieldValue($meta, $config, $object);
175-
$this->pendingFileRemovals[] = $path.DIRECTORY_SEPARATOR.$fileName;
176-
}
170+
$this->addFileRemoval($meta, $config, $object);
177171
}
178172
}
179173
}
@@ -281,13 +275,7 @@ public function processFile(AdapterInterface $ea, $object, $action)
281275

282276
if ($action === self::ACTION_UPDATE) {
283277
// First we add the original file to the pendingFileRemovals array
284-
if ($config['filePathField']) {
285-
$this->pendingFileRemovals[] = $this->getFilePathFieldValue($meta, $config, $object);
286-
} else {
287-
$path = $this->getPath($meta, $config, $object);
288-
$fileName = $this->getFileNameFieldValue($meta, $config, $object);
289-
$this->pendingFileRemovals[] = $path.DIRECTORY_SEPARATOR.$fileName;
290-
}
278+
$this->addFileRemoval($meta, $config, $object);
291279
}
292280

293281
// We generate the filename based on configuration
@@ -390,6 +378,34 @@ protected function getPath(ClassMetadata $meta, array $config, $object)
390378
return $path;
391379
}
392380

381+
/**
382+
* @param ClassMetadata $meta
383+
* @param array $config
384+
* @param object $object Entity
385+
*/
386+
protected function addFileRemoval($meta, $config, $object)
387+
{
388+
if ($config['filePathField']) {
389+
$this->pendingFileRemovals[] = $this->getFilePathFieldValue($meta, $config, $object);
390+
} else {
391+
$path = $this->getPath($meta, $config, $object);
392+
$fileName = $this->getFileNameFieldValue($meta, $config, $object);
393+
$this->pendingFileRemovals[] = $path.DIRECTORY_SEPARATOR.$fileName;
394+
}
395+
}
396+
397+
/**
398+
* @param string $filePath
399+
*/
400+
protected function cancelFileRemoval($filePath)
401+
{
402+
$k = array_search($filePath, $this->pendingFileRemovals, true);
403+
404+
if (false !== $k) {
405+
unset($this->pendingFileRemovals[$k]);
406+
}
407+
}
408+
393409
/**
394410
* Returns value of the entity's property
395411
*
@@ -563,14 +579,7 @@ public function moveFile(FileInfoInterface $fileInfo, $path, $filenameGeneratorC
563579

564580
if (is_file($info['filePath'])) {
565581
if ($overwrite) {
566-
567-
$k = array_search($info['filePath'], $this->pendingFileRemovals);
568-
569-
if ($k !== false)
570-
{
571-
unset($this->pendingFileRemovals[$k]);
572-
}
573-
582+
$this->cancelFileRemoval($info['filePath']);
574583
$this->removeFile($info['filePath']);
575584
} elseif ($appendNumber) {
576585
$counter = 1;

0 commit comments

Comments
 (0)