Skip to content

Commit 1ff84e2

Browse files
committed
Add interfaces for easier testing
1 parent 8a4c742 commit 1ff84e2

15 files changed

Lines changed: 69 additions & 24 deletions

config/factory.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,6 @@
1919
</service>
2020

2121
<service id="Vich\UploaderBundle\Mapping\PropertyMappingFactory" alias="vich_uploader.property_mapping_factory" public="false"/>
22+
<service id="Vich\UploaderBundle\Mapping\PropertyMappingFactoryInterface" alias="vich_uploader.property_mapping_factory" public="false"/>
2223
</services>
2324
</container>

config/handler.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
<argument type="service" id="vich_uploader.property_mapping_factory" />
99
<argument type="service" id="vich_uploader.storage" />
1010
</service>
11+
<service id="Vich\UploaderBundle\Handler\DownloadHandlerInterface" alias="vich_uploader.download_handler"/>
1112

1213
<!-- global handler -->
1314
<service id="vich_uploader.upload_handler" class="Vich\UploaderBundle\Handler\UploadHandler" public="true">
@@ -16,6 +17,7 @@
1617
<argument type="service" id="vich_uploader.file_injector" />
1718
<argument type="service" id="event_dispatcher" />
1819
</service>
20+
<service id="Vich\UploaderBundle\Handler\UploadHandlerInterface" alias="vich_uploader.upload_handler"/>
1921

2022
<service id="Vich\UploaderBundle\Handler\DownloadHandler" alias="vich_uploader.download_handler" public="false"/>
2123
<service id="Vich\UploaderBundle\Handler\UploadHandler" alias="vich_uploader.upload_handler" public="false"/>

src/EventListener/Doctrine/BaseListener.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace Vich\UploaderBundle\EventListener\Doctrine;
44

55
use Vich\UploaderBundle\Adapter\AdapterInterface;
6-
use Vich\UploaderBundle\Handler\UploadHandler;
6+
use Vich\UploaderBundle\Handler\UploadHandlerInterface;
77
use Vich\UploaderBundle\Metadata\MetadataReader;
88
use Vich\UploaderBundle\Util\ClassUtils;
99

@@ -18,7 +18,7 @@ public function __construct(
1818
protected readonly string $mapping,
1919
protected readonly AdapterInterface $adapter,
2020
protected readonly MetadataReader $metadata,
21-
protected readonly UploadHandler $handler,
21+
protected readonly UploadHandlerInterface $handler,
2222
) {
2323
}
2424

src/Form/Type/VichFileType.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
1515
use Symfony\Component\PropertyAccess\PropertyPath;
1616
use Vich\UploaderBundle\Form\DataTransformer\FileTransformer;
17-
use Vich\UploaderBundle\Handler\UploadHandler;
18-
use Vich\UploaderBundle\Mapping\PropertyMappingFactory;
17+
use Vich\UploaderBundle\Handler\UploadHandlerInterface;
18+
use Vich\UploaderBundle\Mapping\PropertyMappingFactoryInterface;
1919
use Vich\UploaderBundle\Storage\StorageInterface;
2020

2121
/**
@@ -29,8 +29,8 @@ class VichFileType extends AbstractType
2929

3030
public function __construct(
3131
protected readonly StorageInterface $storage,
32-
protected readonly UploadHandler $handler,
33-
protected readonly PropertyMappingFactory $factory,
32+
protected readonly UploadHandlerInterface $handler,
33+
protected readonly PropertyMappingFactoryInterface $factory,
3434
?PropertyAccessorInterface $propertyAccessor = null
3535
) {
3636
$this->propertyAccessor = $propertyAccessor ?: PropertyAccess::createPropertyAccessor();

src/Form/Type/VichImageType.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
use Symfony\Component\OptionsResolver\Options;
99
use Symfony\Component\OptionsResolver\OptionsResolver;
1010
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
11-
use Vich\UploaderBundle\Handler\UploadHandler;
12-
use Vich\UploaderBundle\Mapping\PropertyMappingFactory;
11+
use Vich\UploaderBundle\Handler\UploadHandlerInterface;
12+
use Vich\UploaderBundle\Mapping\PropertyMappingFactoryInterface;
1313
use Vich\UploaderBundle\Storage\StorageInterface;
1414

1515
/**
@@ -27,8 +27,8 @@ class VichImageType extends VichFileType
2727

2828
public function __construct(
2929
StorageInterface $storage,
30-
UploadHandler $handler,
31-
PropertyMappingFactory $factory,
30+
UploadHandlerInterface $handler,
31+
PropertyMappingFactoryInterface $factory,
3232
?PropertyAccessorInterface $propertyAccessor = null,
3333
private readonly ?CacheManager $cacheManager = null
3434
) {

src/Handler/AbstractHandler.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use Vich\UploaderBundle\Exception\MappingNotFoundException;
66
use Vich\UploaderBundle\Mapping\PropertyMapping;
7-
use Vich\UploaderBundle\Mapping\PropertyMappingFactory;
7+
use Vich\UploaderBundle\Mapping\PropertyMappingFactoryInterface;
88
use Vich\UploaderBundle\Storage\StorageInterface;
99

1010
/**
@@ -13,7 +13,7 @@
1313
abstract class AbstractHandler
1414
{
1515
public function __construct(
16-
protected readonly PropertyMappingFactory $factory,
16+
protected readonly PropertyMappingFactoryInterface $factory,
1717
protected readonly StorageInterface $storage,
1818
) {
1919
}

src/Handler/DownloadHandler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
/**
1111
* @author Kévin Gomez <contact@kevingomez.fr>
1212
*/
13-
final class DownloadHandler extends AbstractHandler
13+
final class DownloadHandler extends AbstractHandler implements DownloadHandlerInterface
1414
{
1515
/**
1616
* Create a response object that will trigger the download of a file.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Vich\UploaderBundle\Handler;
6+
7+
use Symfony\Component\HttpFoundation\StreamedResponse;
8+
9+
interface DownloadHandlerInterface
10+
{
11+
public function downloadObject(object|array $object, string $field, ?string $className = null, string|bool|null $fileName = null, bool $forceDownload = true): StreamedResponse;
12+
}

src/Handler/UploadHandler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*
1919
* @author Kévin Gomez <contact@kevingomez.fr>
2020
*/
21-
final class UploadHandler extends AbstractHandler
21+
final class UploadHandler extends AbstractHandler implements UploadHandlerInterface
2222
{
2323
public function __construct(
2424
PropertyMappingFactory $factory,
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Vich\UploaderBundle\Handler;
6+
7+
interface UploadHandlerInterface
8+
{
9+
public function upload(object $obj, string $fieldName): void;
10+
11+
public function inject(object $obj, string $fieldName): void;
12+
13+
public function clean(object $obj, string $fieldName): void;
14+
15+
public function remove(object $obj, string $fieldName): void;
16+
}

0 commit comments

Comments
 (0)