Skip to content

Commit

Permalink
Merge pull request #1285 from garak/restore-file-deleting
Browse files Browse the repository at this point in the history
revert changes to clean files
  • Loading branch information
garak committed May 5, 2022
2 parents 7593b60 + 4808753 commit 044940a
Show file tree
Hide file tree
Showing 9 changed files with 17 additions and 62 deletions.
5 changes: 0 additions & 5 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,6 @@ parameters:
count: 1
path: src/Event/Event.php

-
message: "#^Call to an undefined method Doctrine\\\\Common\\\\EventArgs\\:\\:getEntityChangeSet\\(\\)\\.$#"
count: 1
path: src/EventListener/Doctrine/CleanListener.php

-
message: "#^Method Vich\\\\UploaderBundle\\\\Handler\\\\UploadHandler\\:\\:clean\\(\\) has parameter \\$obj with no type specified\\.$#"
count: 1
Expand Down
16 changes: 0 additions & 16 deletions src/EventListener/Doctrine/BaseListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,20 +70,4 @@ protected function getUploadableFields($object): array
return $data['propertyName'];
}, $fields);
}

/**
* Returns a list of uploadable fields for the given object and mapping.
*
* @return array<string, string> A map with property name as key and file name as value
*
* @throws \Vich\UploaderBundle\Exception\MappingNotFoundException
*/
protected function getUploadableFilenameFields(object $object): array
{
$fields = $this->metadata->getUploadableFields(ClassUtils::getClass($object), $this->mapping);

return \array_map(static function (array $data): string {
return $data['fileNameProperty'];
}, $fields);
}
}
10 changes: 2 additions & 8 deletions src/EventListener/Doctrine/CleanListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,8 @@ public function preUpdate(EventArgs $event): void
return;
}

$changeSet = $event->getEntityChangeSet();

foreach ($this->getUploadableFilenameFields($object) as $field => $fileName) {
if (!isset($changeSet[$fileName])) {
continue;
}

$this->handler->clean($object, $field, $changeSet[$fileName][0]);
foreach ($this->getUploadableFields($object) as $field) {
$this->handler->clean($object, $field);
}

$this->adapter->recomputeChangeSet($event);
Expand Down
8 changes: 4 additions & 4 deletions src/Handler/UploadHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public function inject($obj, string $fieldName): void
$this->dispatch(Events::POST_INJECT, new Event($obj, $mapping));
}

public function clean($obj, string $fieldName, ?string $forcedFilename = null): void
public function clean($obj, string $fieldName): void
{
$mapping = $this->getMapping($obj, $fieldName);

Expand All @@ -86,10 +86,10 @@ public function clean($obj, string $fieldName, ?string $forcedFilename = null):
return;
}

$this->remove($obj, $fieldName, $forcedFilename);
$this->remove($obj, $fieldName);
}

public function remove($obj, string $fieldName, ?string $forcedFilename = null): void
public function remove($obj, string $fieldName): void
{
$mapping = $this->getMapping($obj, $fieldName);
$oldFilename = $mapping->getFileName($obj);
Expand All @@ -107,7 +107,7 @@ public function remove($obj, string $fieldName, ?string $forcedFilename = null):
return;
}

$this->storage->remove($obj, $mapping, $forcedFilename);
$this->storage->remove($obj, $mapping);
$mapping->erase($obj);

$this->dispatch(Events::POST_REMOVE, new Event($obj, $mapping));
Expand Down
4 changes: 2 additions & 2 deletions src/Storage/AbstractStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,15 @@ public function upload($obj, PropertyMapping $mapping): void

abstract protected function doRemove(PropertyMapping $mapping, ?string $dir, string $name): ?bool;

public function remove($obj, PropertyMapping $mapping, ?string $forcedFilename = null): ?bool
public function remove($obj, PropertyMapping $mapping): ?bool
{
$name = $mapping->getFileName($obj);

if (empty($name)) {
return false;
}

return $this->doRemove($mapping, $mapping->getUploadDir($obj), $forcedFilename ?? $name);
return $this->doRemove($mapping, $mapping->getUploadDir($obj), $name);
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/Storage/StorageInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function upload($obj, PropertyMapping $mapping): void;
* @param object $obj The object
* @param PropertyMapping $mapping The mapping representing the field to remove
*/
public function remove($obj, PropertyMapping $mapping, ?string $forcedFilename = null): ?bool;
public function remove($obj, PropertyMapping $mapping): ?bool;

/**
* Resolves the path for a file based on the specified object
Expand All @@ -39,7 +39,7 @@ public function remove($obj, PropertyMapping $mapping, ?string $forcedFilename =
*/
public function resolvePath($obj, ?string $fieldName = null, ?string $className = null, ?bool $relative = false): ?string;

//TODO: inconsistency - use PropertyMapping instead of fieldName+className
// TODO: inconsistency - use PropertyMapping instead of fieldName+className

/**
* Resolves the uri based on the specified object and mapping name.
Expand All @@ -57,7 +57,7 @@ public function resolveUri($obj, ?string $fieldName = null, ?string $className =
*
* @param object|array $obj The object
* @param string $fieldName The field to use
* @param string $className The object's class. Mandatory if $obj can't be used to determine it
* @param string|null $className The object's class. Mandatory if $obj can't be used to determine it
*
* @return resource|null The resolved resource or null if file not stored
*/
Expand Down
14 changes: 3 additions & 11 deletions tests/EventListener/Doctrine/CleanListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ class CleanListenerTest extends ListenerTestCase
*/
protected function setUp(): void
{
self::$usePreUpdateEventArgs = true;
parent::setUp();

$this->listener = new CleanListener(self::MAPPING_NAME, $this->adapter, $this->metadata, $this->handler);
Expand Down Expand Up @@ -45,24 +44,17 @@ public function testPreUpdate(): void
->willReturn(true);

$this->metadata
->expects(self::once())
->method('getUploadableFields')
->with(DummyEntity::class, self::MAPPING_NAME)
->willReturn([
'field_name' => ['propertyName' => 'field_name', 'fileNameProperty' => 'path_name'],
]);

$this->event
->method('getEntityChangeSet')
->willReturn([
'path_name' => [
0 => 'dummy.jpg',
],
['propertyName' => 'field_name'],
]);

$this->handler
->expects(self::once())
->method('clean')
->with($this->object, 'field_name', 'dummy.jpg');
->with($this->object, 'field_name');

$this->adapter
->expects(self::once())
Expand Down
12 changes: 1 addition & 11 deletions tests/EventListener/Doctrine/ListenerTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ protected function setUp(): void
$this->metadata = $this->getMetadataReaderMock();
$this->handler = $this->getHandlerMock();
$this->object = new DummyEntity();
$this->event = self::$usePreUpdateEventArgs ? $this->getPreEventMock() : $this->getEventMock();
$this->event = $this->getEventMock();

$that = $this;

Expand Down Expand Up @@ -114,14 +114,4 @@ protected function getEventMock(): EventArgs
->disableOriginalConstructor()
->getMock();
}

/**
* @return PreUpdateEventArgs&\PHPUnit\Framework\MockObject\MockObject
*/
protected function getPreEventMock(): PreUpdateEventArgs
{
return $this->getMockBuilder(PreUpdateEventArgs::class)
->disableOriginalConstructor()
->getMock();
}
}
4 changes: 2 additions & 2 deletions tests/Handler/UploadHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -289,12 +289,12 @@ protected function validEvent(): object

protected function expectEvents(array $events): void
{
$arguments = array_map(function (string $event): array {
$arguments = \array_map(function (string $event): array {
return [$this->validEvent(), $event];
}, $events);

$this->dispatcher
->expects(self::exactly(count($events)))
->expects(self::exactly(\count($events)))
->method('dispatch')
->withConsecutive(...$arguments)
;
Expand Down

0 comments on commit 044940a

Please sign in to comment.