Skip to content

Commit

Permalink
Merge pull request Sylius#7831 from pamil/pull-request/7790
Browse files Browse the repository at this point in the history
[Core] [RFC] Images related semantic fixes (alternative solution)
  • Loading branch information
pjedrzejewski committed Mar 25, 2017
2 parents 6a0a653 + 95a8eae commit c1d1d10
Show file tree
Hide file tree
Showing 12 changed files with 37 additions and 36 deletions.
Expand Up @@ -11,15 +11,15 @@

namespace Sylius\Bundle\CoreBundle\EventListener;

use Sylius\Component\Core\Model\ImageAwareInterface;
use Sylius\Component\Core\Model\ImagesAwareInterface;
use Sylius\Component\Core\Uploader\ImageUploaderInterface;
use Symfony\Component\EventDispatcher\GenericEvent;
use Webmozart\Assert\Assert;

/**
* @author Grzegorz Sadowski <grzegorz.sadowski@lakion.com>
*/
final class ImageUploadListener
final class ImagesUploadListener
{
/**
* @var ImageUploaderInterface
Expand All @@ -37,18 +37,18 @@ public function __construct(ImageUploaderInterface $uploader)
/**
* @param GenericEvent $event
*/
public function uploadImage(GenericEvent $event)
public function uploadImages(GenericEvent $event)
{
$subject = $event->getSubject();
Assert::isInstanceOf($subject, ImageAwareInterface::class);
Assert::isInstanceOf($subject, ImagesAwareInterface::class);

$this->uploadImages($subject);
$this->uploadSubjectImages($subject);
}

/**
* @param ImageAwareInterface $subject
* @param ImagesAwareInterface $subject
*/
private function uploadImages(ImageAwareInterface $subject)
private function uploadSubjectImages(ImagesAwareInterface $subject)
{
$images = $subject->getImages();
foreach ($images as $image) {
Expand Down
Expand Up @@ -23,12 +23,12 @@
<argument type="service" id="sylius.repository.channel" />
<tag name="kernel.event_listener" event="sylius.channel.pre_delete" method="onChannelPreDelete" />
</service>
<service id="sylius.listener.image_upload" class="Sylius\Bundle\CoreBundle\EventListener\ImageUploadListener">
<service id="sylius.listener.images_upload" class="Sylius\Bundle\CoreBundle\EventListener\ImagesUploadListener">
<argument type="service" id="sylius.image_uploader" />
<tag name="kernel.event_listener" event="sylius.product.pre_create" method="uploadImage" />
<tag name="kernel.event_listener" event="sylius.product.pre_update" method="uploadImage" />
<tag name="kernel.event_listener" event="sylius.taxon.pre_create" method="uploadImage" />
<tag name="kernel.event_listener" event="sylius.taxon.pre_update" method="uploadImage" />
<tag name="kernel.event_listener" event="sylius.product.pre_create" method="uploadImages" />
<tag name="kernel.event_listener" event="sylius.product.pre_update" method="uploadImages" />
<tag name="kernel.event_listener" event="sylius.taxon.pre_create" method="uploadImages" />
<tag name="kernel.event_listener" event="sylius.taxon.pre_update" method="uploadImages" />
</service>
<service id="sylius.listener.order_recalculation" class="Sylius\Bundle\CoreBundle\EventListener\OrderRecalculationListener">
<argument type="service" id="sylius.order_processing.order_processor" />
Expand Down
Expand Up @@ -12,16 +12,16 @@
namespace spec\Sylius\Bundle\CoreBundle\EventListener;

use PhpSpec\ObjectBehavior;
use Sylius\Bundle\CoreBundle\EventListener\ImageUploadListener;
use Sylius\Component\Core\Model\ImageAwareInterface;
use Sylius\Bundle\CoreBundle\EventListener\ImagesUploadListener;
use Sylius\Component\Core\Model\ImagesAwareInterface;
use Sylius\Component\Core\Model\ImageInterface;
use Sylius\Component\Core\Uploader\ImageUploaderInterface;
use Symfony\Component\EventDispatcher\GenericEvent;

/**
* @author Grzegorz Sadowski <grzegorz.sadowski@lakion.com>
*/
final class ImageUploadListenerSpec extends ObjectBehavior
final class ImagesUploadListenerSpec extends ObjectBehavior
{
function let(ImageUploaderInterface $uploader)
{
Expand All @@ -30,12 +30,12 @@ function let(ImageUploaderInterface $uploader)

function it_is_initializable()
{
$this->shouldHaveType(ImageUploadListener::class);
$this->shouldHaveType(ImagesUploadListener::class);
}

function it_uses_image_uploader_to_upload_images(
GenericEvent $event,
ImageAwareInterface $subject,
ImagesAwareInterface $subject,
ImageInterface $image,
ImageUploaderInterface $uploader
) {
Expand All @@ -45,7 +45,7 @@ function it_uses_image_uploader_to_upload_images(
$image->getPath()->willReturn('some_path');
$uploader->upload($image)->shouldBeCalled();

$this->uploadImage($event);
$this->uploadImages($event);
}

function it_throws_exception_if_event_subject_is_not_an_image_aware(
Expand All @@ -56,7 +56,7 @@ function it_throws_exception_if_event_subject_is_not_an_image_aware(

$this
->shouldThrow(\InvalidArgumentException::class)
->duringUploadImage($event)
->duringUploadImages($event)
;
}
}
4 changes: 2 additions & 2 deletions src/Sylius/Component/Core/Model/Image.php
Expand Up @@ -37,7 +37,7 @@ abstract class Image implements ImageInterface
protected $path;

/**
* @var ImageAwareInterface
* @var object
*/
protected $owner;

Expand Down Expand Up @@ -129,7 +129,7 @@ public function getOwner()
/**
* {@inheritdoc}
*/
public function setOwner(ImageAwareInterface $owner = null)
public function setOwner($owner)
{
$this->owner = $owner;
}
Expand Down
7 changes: 3 additions & 4 deletions src/Sylius/Component/Core/Model/ImageInterface.php
Expand Up @@ -11,7 +11,6 @@

namespace Sylius\Component\Core\Model;

use Sylius\Component\Resource\Model\CodeAwareInterface;
use Sylius\Component\Resource\Model\ResourceInterface;

/**
Expand Down Expand Up @@ -55,12 +54,12 @@ public function getPath();
public function setPath($path);

/**
* @return ImageAwareInterface
* @return object
*/
public function getOwner();

/**
* @param ImageAwareInterface|null $owner
* @param object|null $owner
*/
public function setOwner(ImageAwareInterface $owner = null);
public function setOwner($owner);
}
Expand Up @@ -16,7 +16,7 @@
/**
* @author Grzegorz Sadowski <grzegorz.sadowski@lakion.com>
*/
interface ImageAwareInterface
interface ImagesAwareInterface
{
/**
* @return Collection|ImageInterface[]
Expand Down
2 changes: 1 addition & 1 deletion src/Sylius/Component/Core/Model/ProductInterface.php
Expand Up @@ -25,7 +25,7 @@ interface ProductInterface extends
ProductTaxonsAwareInterface,
ChannelsAwareInterface,
ReviewableInterface,
ImageAwareInterface
ImagesAwareInterface
{
/*
* Variant selection methods.
Expand Down
2 changes: 1 addition & 1 deletion src/Sylius/Component/Core/Model/TaxonInterface.php
Expand Up @@ -17,7 +17,7 @@
/**
* @author Paweł Jędrzejewski <pawel@sylius.org>
*/
interface TaxonInterface extends BaseTaxonInterface, ImageAwareInterface
interface TaxonInterface extends BaseTaxonInterface, ImagesAwareInterface
{

}
5 changes: 3 additions & 2 deletions src/Sylius/Component/Core/spec/Model/ProductImageSpec.php
Expand Up @@ -14,7 +14,6 @@
use Doctrine\Common\Collections\Collection;
use PhpSpec\ObjectBehavior;
use Sylius\Component\Core\Model\Image;
use Sylius\Component\Core\Model\ImageAwareInterface;
use Sylius\Component\Core\Model\ProductImage;
use Sylius\Component\Core\Model\ProductImageInterface;
use Sylius\Component\Core\Model\ProductVariantInterface;
Expand Down Expand Up @@ -79,8 +78,10 @@ function it_does_not_have_owner_by_default()
$this->getOwner()->shouldReturn(null);
}

function its_owner_is_mutable(ImageAwareInterface $owner)
function its_owner_is_mutable()
{
$owner = new \stdClass();

$this->setOwner($owner);
$this->getOwner()->shouldReturn($owner);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Sylius/Component/Core/spec/Model/ProductSpec.php
Expand Up @@ -14,7 +14,7 @@
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use PhpSpec\ObjectBehavior;
use Sylius\Component\Core\Model\ImageAwareInterface;
use Sylius\Component\Core\Model\ImagesAwareInterface;
use Sylius\Component\Core\Model\ImageInterface;
use Sylius\Component\Core\Model\Product;
use Sylius\Component\Core\Model\ProductInterface;
Expand Down Expand Up @@ -48,7 +48,7 @@ function it_implements_a_product_interface()

function it_implements_an_image_aware_interface()
{
$this->shouldImplement(ImageAwareInterface::class);
$this->shouldImplement(ImagesAwareInterface::class);
}

function it_extends_a_product_model()
Expand Down
5 changes: 3 additions & 2 deletions src/Sylius/Component/Core/spec/Model/TaxonImageSpec.php
Expand Up @@ -13,7 +13,6 @@

use PhpSpec\ObjectBehavior;
use Sylius\Component\Core\Model\Image;
use Sylius\Component\Core\Model\ImageAwareInterface;
use Sylius\Component\Core\Model\TaxonImage;

/**
Expand Down Expand Up @@ -71,8 +70,10 @@ function it_does_not_have_owner_by_default()
$this->getOwner()->shouldReturn(null);
}

function its_owner_is_mutable(ImageAwareInterface $owner)
function its_owner_is_mutable()
{
$owner = new \stdClass();

$this->setOwner($owner);
$this->getOwner()->shouldReturn($owner);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Sylius/Component/Core/spec/Model/TaxonSpec.php
Expand Up @@ -14,7 +14,7 @@
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use PhpSpec\ObjectBehavior;
use Sylius\Component\Core\Model\ImageAwareInterface;
use Sylius\Component\Core\Model\ImagesAwareInterface;
use Sylius\Component\Core\Model\ImageInterface;
use Sylius\Component\Core\Model\Taxon;
use Sylius\Component\Core\Model\TaxonInterface;
Expand All @@ -36,7 +36,7 @@ function it_is_a_taxon()

function it_implements_an_image_aware_interface()
{
$this->shouldImplement(ImageAwareInterface::class);
$this->shouldImplement(ImagesAwareInterface::class);
}

function it_initializes_an_image_collection_by_default()
Expand Down

0 comments on commit c1d1d10

Please sign in to comment.