Skip to content

Commit

Permalink
Change behavior of ImagesRemoveListener
Browse files Browse the repository at this point in the history
  • Loading branch information
Zales0123 committed Nov 7, 2018
1 parent 329ec97 commit 1f3c663
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 31 deletions.
13 changes: 11 additions & 2 deletions src/Sylius/Behat/Service/Checker/ImageExistenceChecker.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Sylius\Behat\Service\Checker;
Expand All @@ -22,10 +31,10 @@ public function __construct(FilterService $filterService, string $mediaRootPath)

public function doesImageWithUrlExist(string $imageUrl, string $liipImagineFilter): bool
{
$imageUrl = str_replace($liipImagineFilter.'/', '', substr($imageUrl, strpos($imageUrl, $liipImagineFilter), strlen($imageUrl)));
$imageUrl = str_replace($liipImagineFilter . '/', '', substr($imageUrl, strpos($imageUrl, $liipImagineFilter), strlen($imageUrl)));

$browserImagePath = $this->filterService->getUrlOfFilteredImage($imageUrl, $liipImagineFilter);

return file_exists($this->mediaRootPath.parse_url($browserImagePath, PHP_URL_PATH));
return file_exists($this->mediaRootPath . parse_url($browserImagePath, PHP_URL_PATH));
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Sylius\Behat\Service\Checker;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,16 @@

namespace Sylius\Bundle\CoreBundle\EventListener;

use Doctrine\ORM\Event\LifecycleEventArgs;
use Doctrine\ORM\Event\OnFlushEventArgs;
use Doctrine\ORM\Event\PostFlushEventArgs;
use Liip\ImagineBundle\Imagine\Cache\CacheManager;
use Liip\ImagineBundle\Imagine\Filter\FilterManager;
use Sylius\Component\Core\Model\ImageInterface;
use Sylius\Component\Core\Uploader\ImageUploaderInterface;

/**
* @internal
*/
final class ImagesRemoveListener
{
/** @var ImageUploaderInterface */
Expand All @@ -30,20 +34,34 @@ final class ImagesRemoveListener
/** @var FilterManager */
private $filterManager;

/** @var string[] */
private $imagesToDelete = [];

public function __construct(ImageUploaderInterface $imageUploader, CacheManager $cacheManager, FilterManager $filterManager)
{
$this->imageUploader = $imageUploader;
$this->cacheManager = $cacheManager;
$this->filterManager = $filterManager;
}

public function postRemove(LifecycleEventArgs $event): void
public function onFlush(OnFlushEventArgs $event): void
{
$image = $event->getEntity();
foreach ($event->getEntityManager()->getUnitOfWork()->getScheduledEntityDeletions() as $entityDeletion) {
if (!$entityDeletion instanceof ImageInterface) {
continue;
}

if (!in_array($entityDeletion->getPath(), $this->imagesToDelete)) {
$this->imagesToDelete[] = $entityDeletion->getPath();
}
}
}

if ($image instanceof ImageInterface) {
$this->imageUploader->remove($image->getPath());
$this->cacheManager->remove($image->getPath(), array_keys($this->filterManager->getFilterConfiguration()->all()));
public function postFlush(PostFlushEventArgs $event): void
{
foreach ($this->imagesToDelete as $imagePath) {
$this->imageUploader->remove($imagePath);
$this->cacheManager->remove($imagePath, array_keys($this->filterManager->getFilterConfiguration()->all()));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@
<argument type="service" id="sylius.image_uploader" />
<argument type="service" id="liip_imagine.cache.manager" />
<argument type="service" id="liip_imagine.filter.manager" />
<tag name="doctrine.event_listener" event="postRemove" lazy="true" />
<tag name="doctrine.event_listener" event="onFlush" lazy="true" />
<tag name="doctrine.event_listener" event="postFlush" lazy="true" />
</service>
<service id="sylius.listener.order_recalculation" class="Sylius\Bundle\CoreBundle\EventListener\OrderRecalculationListener">
<argument type="service" id="sylius.order_processing.order_processor" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
<service id="sylius.test.factory.promotion" class="Sylius\Component\Core\Test\Factory\TestPromotionFactory">
<argument type="service" id="sylius.factory.promotion" />
</service>
</services>

<service id="sylius.liip.filter_service" alias="liip_imagine.service.filter" public="true" />
</services>
</container>
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,17 @@

namespace spec\Sylius\Bundle\CoreBundle\EventListener;

use Doctrine\ORM\Event\LifecycleEventArgs;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Event\OnFlushEventArgs;
use Doctrine\ORM\Event\PostFlushEventArgs;
use Doctrine\ORM\UnitOfWork;
use Liip\ImagineBundle\Imagine\Cache\CacheManager;
use Liip\ImagineBundle\Imagine\Filter\FilterConfiguration;
use Liip\ImagineBundle\Imagine\Filter\FilterManager;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
use Sylius\Bundle\CoreBundle\EventListener\ImagesRemoveListener;
use Sylius\Component\Core\Model\ImageInterface;
use Sylius\Component\Core\Model\ProductInterface;
use Sylius\Component\Core\Uploader\ImageUploaderInterface;

final class ImagesRemoveListenerSpec extends ObjectBehavior
Expand All @@ -35,36 +38,49 @@ function it_is_initializable(): void
$this->shouldHaveType(ImagesRemoveListener::class);
}

function it_removes_file_on_post_remove_event(
function it_saves_scheduled_entity_deletions_images_paths(
OnFlushEventArgs $event,
EntityManagerInterface $entityManager,
UnitOfWork $unitOfWork,
ImageInterface $image,
ProductInterface $product
): void {
$event->getEntityManager()->willReturn($entityManager);
$entityManager->getUnitOfWork()->willReturn($unitOfWork);
$unitOfWork->getScheduledEntityDeletions()->willReturn([$image, $product]);

$image->getPath()->shouldBeCalled();

$this->onFlush($event);
}

function it_removes_saved_images_paths(
ImageUploaderInterface $imageUploader,
CacheManager $cacheManager,
FilterManager $filterManager,
LifecycleEventArgs $event,
OnFlushEventArgs $onFlushEvent,
PostFlushEventArgs $postFlushEvent,
EntityManagerInterface $entityManager,
UnitOfWork $unitOfWork,
ImageInterface $image,
ProductInterface $product,
FilterConfiguration $filterConfiguration
): void {
$event->getEntity()->willReturn($image);
$onFlushEvent->getEntityManager()->willReturn($entityManager);
$entityManager->getUnitOfWork()->willReturn($unitOfWork);
$unitOfWork->getScheduledEntityDeletions()->willReturn([$image, $product]);

$image->getPath()->willReturn('image/path');

$filterManager->getFilterConfiguration()->willReturn($filterConfiguration);
$filterConfiguration->all()->willReturn(['sylius_small' => 'thumbnalis']);
$this->onFlush($onFlushEvent);

$imageUploader->remove('image/path')->shouldBeCalled();
$cacheManager->remove('image/path', ['sylius_small'])->shouldBeCalled();

$this->postRemove($event);
}
$filterManager->getFilterConfiguration()->willReturn($filterConfiguration);
$filterConfiguration->all()->willReturn(['test' => 'Test']);

function it_does_nothing_if_entity_is_not_image(
ImageUploaderInterface $imageUploader,
CacheManager $cacheManager,
FilterManager $filterManager,
LifecycleEventArgs $event
): void {
$event->getEntity()->willReturn(new \stdClass());
$filterManager->getFilterConfiguration()->shouldNotBeCalled();
$imageUploader->remove(Argument::any())->shouldNotBeCalled();
$cacheManager->remove(Argument::any(), Argument::any())->shouldNotBeCalled();
$cacheManager->remove('image/path', ['test'])->shouldBeCalled();

$this->postRemove($event);
$this->postFlush($postFlushEvent);
}
}

0 comments on commit 1f3c663

Please sign in to comment.