Create new Originator component and adjust code to match this change

This commit is contained in:
stloyd 2014-11-04 15:50:58 +01:00
parent 72b24e855a
commit 989ac2cf1e
24 changed files with 315 additions and 119 deletions

View file

@ -9,6 +9,7 @@ suites:
Inventory : { namespace: Sylius, spec_path: src/Sylius/Component/Inventory }
Locale : { namespace: Sylius, spec_path: src/Sylius/Component/Locale }
Order : { namespace: Sylius, spec_path: src/Sylius/Component/Order }
Originator : { namespace: Sylius, spec_path: src/Sylius/Component/Originator }
Payment : { namespace: Sylius, spec_path: src/Sylius/Component/Payment }
Payum : { namespace: Sylius, spec_path: src/Sylius/Component/Payum }
Pricing : { namespace: Sylius, spec_path: src/Sylius/Component/Pricing }

View file

@ -150,8 +150,6 @@ sylius_order:
order_item:
model: Sylius\Component\Core\Model\OrderItem
form: Sylius\Bundle\CoreBundle\Form\Type\OrderItemType
adjustment:
model: Sylius\Component\Core\Model\Adjustment
sylius_sequence:
generators:

View file

@ -1,21 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
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.
-->
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping">
<mapped-superclass name="Sylius\Component\Core\Model\Adjustment" table="sylius_adjustment">
<field name="originId" column="origin_id" type="integer" nullable="true" />
<field name="originType" column="origin_type" type="string" nullable="true" />
</mapped-superclass>
</doctrine-mapping>

View file

@ -75,7 +75,6 @@
<parameter key="sylius.dynamic_router.class">Symfony\Cmf\Component\Routing\DynamicRouter</parameter>
<parameter key="sylius.generator.class">Sylius\Bundle\CoreBundle\Routing\SyliusAwareGenerator</parameter>
<parameter key="sylius.route_provider.class">Sylius\Bundle\CoreBundle\Routing\RouteProvider</parameter>
<parameter key="sylius.originator.class">Sylius\Component\Core\Originator\Originator</parameter>
<parameter key="sylius.callback.complete_order.class">Sylius\Bundle\CoreBundle\StateMachineCallback\CompleteOrderCallback</parameter>
</parameters>
@ -413,9 +412,7 @@
<argument>%sylius.route_classes%</argument>
<call method="setRouteCollectionLimit"><argument>%sylius.route_collection_limit%</argument></call>
</service>
<service id="sylius.originator" class="%sylius.originator.class%">
<argument type="service" id="doctrine.orm.entity_manager"/>
</service>
<service id="sylius.enhancer.controllers_by_class" class="%cmf_routing.enhancer.field_by_class.class%">
<argument>_sylius_entity</argument>
<argument>_controller</argument>

View file

@ -28,6 +28,9 @@
<field name="neutral" column="is_neutral" type="boolean" />
<field name="locked" column="is_locked" type="boolean" />
<field name="originId" column="origin_id" type="integer" nullable="true" />
<field name="originType" column="origin_type" type="string" nullable="true" />
<field name="createdAt" column="created_at" type="datetime">
<gedmo:timestampable on="create"/>
</field>

View file

@ -1,5 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
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.
-->
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services
@ -8,6 +19,8 @@
<parameters>
<parameter key="sylius.listener.order_update.class">Sylius\Bundle\OrderBundle\EventListener\OrderUpdateListener</parameter>
<parameter key="sylius.callback.complete_order.class">Sylius\Bundle\OrderBundle\StateMachineCallback\CompleteOrderCallback</parameter>
<parameter key="sylius.originator.class">Sylius\Component\Originator\Originator\Originator</parameter>
</parameters>
<services>
@ -41,6 +54,9 @@
<tag name="form.type" alias="sylius_order_item" />
</service>
<service id="sylius.originator" class="%sylius.originator.class%">
<argument type="service" id="doctrine" />
</service>
<service id="sylius.callback.complete_order" class="%sylius.callback.complete_order.class%" />

View file

@ -1,47 +0,0 @@
<?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.
*/
namespace Sylius\Component\Core\Model;
use Sylius\Component\Order\Model\Adjustment as BaseAdjustment;
/**
* @author Saša Stamenković <umpirsky@gmail.com>
*/
class Adjustment extends BaseAdjustment implements AdjustmentInterface
{
protected $originId;
protected $originType;
public function getOriginId()
{
return $this->originId;
}
public function setOriginId($originId)
{
$this->originId = $originId;
return $this;
}
public function getOriginType()
{
return $this->originType;
}
public function setOriginType($originType)
{
$this->originType = $originType;
return $this;
}
}

View file

@ -13,7 +13,7 @@ namespace Sylius\Component\Core\Model;
use Sylius\Component\Order\Model\AdjustmentInterface as BaseAdjustmentInterface;
interface AdjustmentInterface extends BaseAdjustmentInterface, OriginAwareInterface
interface AdjustmentInterface extends BaseAdjustmentInterface
{
// Labels for tax, shipping and promotion adjustments.
const TAX_ADJUSTMENT = 'tax';

View file

@ -14,7 +14,7 @@ namespace Sylius\Component\Core\Promotion\Action;
use Sylius\Component\Core\Model\AdjustmentInterface;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\Model\OrderItemInterface;
use Sylius\Component\Core\Originator\OriginatorInterface;
use Sylius\Component\Originator\Originator\OriginatorInterface;
use Sylius\Component\Promotion\Action\PromotionActionInterface;
use Sylius\Component\Promotion\Model\PromotionInterface;
use Sylius\Component\Promotion\Model\PromotionSubjectInterface;
@ -56,10 +56,14 @@ abstract class DiscountAction implements PromotionActionInterface
}
}
/**
* @param PromotionInterface $promotion
*
* @return AdjustmentInterface
*/
protected function createAdjustment(PromotionInterface $promotion)
{
$adjustment = $this->adjustmentRepository->createNew();
$adjustment->setLabel(AdjustmentInterface::PROMOTION_ADJUSTMENT);
$adjustment->setDescription($promotion->getDescription());

View file

@ -39,8 +39,8 @@ class AddProductActionSpec extends ObjectBehavior
}
function it_adds_product_as_promotion(
RepositoryInterface $itemRepository,
RepositoryInterface $variantRepository,
$itemRepository,
$variantRepository,
OrderInterface $order,
OrderItemInterface $item,
ProductVariantInterface $variant,
@ -64,8 +64,8 @@ class AddProductActionSpec extends ObjectBehavior
}
function it_does_not_add_product_if_exists(
RepositoryInterface $variantRepository,
RepositoryInterface $itemRepository,
$variantRepository,
$itemRepository,
OrderInterface $order,
OrderItemInterface $item,
ProductVariantInterface $variant,
@ -89,8 +89,8 @@ class AddProductActionSpec extends ObjectBehavior
}
function it_reverts_product(
RepositoryInterface $variantRepository,
RepositoryInterface $itemRepository,
$variantRepository,
$itemRepository,
OrderInterface $order,
OrderItemInterface $item,
ProductVariantInterface $variant,

View file

@ -14,7 +14,7 @@ namespace spec\Sylius\Component\Core\Promotion\Action;
use PhpSpec\ObjectBehavior;
use Sylius\Component\Core\Model\AdjustmentInterface;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\Originator\OriginatorInterface;
use Sylius\Component\Originator\Originator\OriginatorInterface;
use Sylius\Component\Promotion\Model\PromotionInterface;
use Sylius\Component\Resource\Repository\RepositoryInterface;

View file

@ -14,7 +14,7 @@ namespace spec\Sylius\Component\Core\Promotion\Action;
use PhpSpec\ObjectBehavior;
use Sylius\Component\Core\Model\AdjustmentInterface;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\Originator\OriginatorInterface;
use Sylius\Component\Originator\Originator\OriginatorInterface;
use Sylius\Component\Promotion\Model\PromotionInterface;
use Sylius\Component\Resource\Repository\RepositoryInterface;

View file

@ -75,6 +75,20 @@ class Adjustment implements AdjustmentInterface
*/
protected $locked = false;
/**
* Origin identifier.
*
* @var int
*/
protected $originId;
/**
* Origin type.
*
* @var string
*/
protected $originType;
/**
* Creation time.
*
@ -255,6 +269,42 @@ class Adjustment implements AdjustmentInterface
return 0 < $this->amount;
}
/**
* {@inheritdoc}
*/
public function getOriginId()
{
return $this->originId;
}
/**
* {@inheritdoc}
*/
public function setOriginId($originId)
{
$this->originId = $originId;
return $this;
}
/**
* {@inheritdoc}
*/
public function getOriginType()
{
return $this->originType;
}
/**
* {@inheritdoc}
*/
public function setOriginType($originType)
{
$this->originType = $originType;
return $this;
}
/**
* {@inheritdoc}
*/

View file

@ -11,6 +11,7 @@
namespace Sylius\Component\Order\Model;
use Sylius\Component\Originator\Model\OriginAwareInterface;
use Sylius\Component\Resource\Model\TimestampableInterface;
/**
@ -18,7 +19,7 @@ use Sylius\Component\Resource\Model\TimestampableInterface;
*
* @author Paweł Jędrzejewski <pawel@sylius.org>
*/
interface AdjustmentInterface extends TimestampableInterface
interface AdjustmentInterface extends TimestampableInterface, OriginAwareInterface
{
/**
* Get adjustment subject.

View file

@ -22,8 +22,8 @@
"require": {
"php": ">=5.3.3",
"sylius/resource": "0.12.*@dev",
"sylius/sequence": "0.12.*@dev"
"sylius/originator": "0.12.*@dev",
"sylius/sequence": "0.12.*@dev"
},
"require-dev": {
"phpspec/phpspec": "~2.0"

View file

@ -0,0 +1,5 @@
vendor/
bin/
composer.phar
composer.lock

View file

@ -0,0 +1,14 @@
language: php
php:
- 5.3
- 5.4
- 5.5
before_script: composer install --dev --prefer-source
script: bin/phpspec run -fpretty --verbose
notifications:
email: "travis-ci@sylius.org"
irc: "irc.freenode.org#sylius-dev"

View file

@ -0,0 +1,19 @@
Copyright (c) 2011-2014 Paweł Jędrzejewski
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is furnished
to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

View file

@ -9,15 +9,42 @@
* file that was distributed with this source code.
*/
namespace Sylius\Component\Core\Model;
namespace Sylius\Component\Originator\Model;
/**
* @author Saša Stamenković <umpirsky@gmail.com>
*/
interface OriginAwareInterface
{
/**
* Return origin identifier.
*
* @return int
*/
public function getOriginId();
/**
* Set origin identifier.
*
* @param int $originId
*
* @return self
*/
public function setOriginId($originId);
/**
* Return origin type info.
*
* @return string
*/
public function getOriginType();
/**
* Set origin type info.
*
* @param string $originType
*
* @return self
*/
public function setOriginType($originType);
}

View file

@ -9,36 +9,51 @@
* file that was distributed with this source code.
*/
namespace Sylius\Component\Core\Originator;
namespace Sylius\Component\Originator\Originator;
use Sylius\Component\Core\Model\OriginAwareInterface;
use Doctrine\Common\Persistence\ManagerRegistry;
use Sylius\Component\Originator\Model\OriginAwareInterface;
use Sylius\Component\Resource\Exception\UnexpectedTypeException;
use Symfony\Component\PropertyAccess\PropertyAccess;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\PropertyAccess\PropertyAccessor;
/**
* @author Saša Stamenković <umpirsky@gmail.com>
*/
class Originator implements OriginatorInterface
{
protected $em;
/**
* @var ManagerRegistry
*/
protected $manager;
/**
* @var PropertyAccessor
*/
protected $accessor;
/**
* @var string
*/
protected $identifier;
public function __construct(EntityManagerInterface $em, $identifier = 'id')
public function __construct(ManagerRegistry $manager, $identifier = 'id')
{
$this->em = $em;
$this->manager = $manager;
$this->accessor = PropertyAccess::createPropertyAccessor();
$this->identifier = $identifier;
}
/**
* {@inheritdoc}
*/
public function getOrigin(OriginAwareInterface $originAware)
{
if (null === $originAware->getOriginId() || null === $originAware->getOriginType()) {
return null;
}
return $this->em
return $this->manager
->getRepository($originAware->getOriginType())
->findOneBy(array(
$this->identifier => $originAware->getOriginId()
@ -46,6 +61,9 @@ class Originator implements OriginatorInterface
;
}
/**
* {@inheritdoc}
*/
public function setOrigin(OriginAwareInterface $originAware, $origin)
{
if (!is_object($origin)) {

View file

@ -9,15 +9,25 @@
* file that was distributed with this source code.
*/
namespace Sylius\Component\Core\Originator;
namespace Sylius\Component\Originator\Originator;
use Sylius\Component\Core\Model\OriginAwareInterface;
use Sylius\Component\Originator\Model\OriginAwareInterface;
/**
* @author Saša Stamenković <umpirsky@gmail.com>
*/
interface OriginatorInterface
{
/**
* @param OriginAwareInterface $originAware
*
* @return null|object
*/
public function getOrigin(OriginAwareInterface $originAware);
/**
* @param OriginAwareInterface $originAware
* @param object $origin
*/
public function setOrigin(OriginAwareInterface $originAware, $origin);
}

View file

@ -0,0 +1,47 @@
Originator Component [![Build status...](https://secure.travis-ci.org/Sylius/Originator.png?branch=master)](http://travis-ci.org/Sylius/Originator)
====================
It is a component that enables storing object origin reference given by type and identifier.
Sylius
------
Modern ecommerce for PHP and Symfony2. Visit [Sylius.org](http://sylius.org).
Documentation
-------------
Documentation is available on [**docs.sylius.org**](http://docs.sylius.org/en/latest/components/Originator/index.html).
Contributing
------------
All instructions for contributing to Sylius can be found in the [Contributing Guide](http://docs.sylius.org/en/latest/contributing/index.html).
Support
-------
If you have a question regarding the usage of this library, please ask on
[Stackoverflow](http://stackoverflow.com). You should use "sylius"
tag when posting and make sure to [browse existing questions](http://stackoverflow.com/questions/tagged/sylius).
Sylius on Twitter
-----------------
[Follow the official Sylius account on Twitter!](http://twitter.com/Sylius).
Bug Tracking
------------
If you find a bug, please refer to the [Reporting a Bug](http://docs.sylius.org/en/latest/contributing/code/bugs.html) guide.
MIT License
-----------
License can be found [here](https://github.com/Sylius/Originator/blob/master/LICENSE).
Authors
-------
The component was originally created by [Paweł Jędrzejewski](http://pjedrzejewski.com).
See the list of [contributors](https://github.com/Sylius/Originator/contributors).

View file

@ -0,0 +1,47 @@
{
"name": "sylius/originator",
"type": "library",
"description": "Sylius component that enables storing object origin reference given by type and identifier.",
"keywords": ["shop", "ecommerce", "originator", "sylius"],
"homepage": "http://sylius.org",
"license": "MIT",
"authors": [
{
"name": "Paweł Jędrzejewski",
"homepage": "http://pjedrzejewski.com"
},
{
"name": "Sylius project",
"homepage": "http://sylius.org"
},
{
"name": "Community contributions",
"homepage": "http://github.com/Sylius/Sylius/contributors"
}
],
"require": {
"php": ">=5.3.3",
"doctrine/common": "~2.3",
"sylius/resource": "0.12.*@dev",
"symfony/property-access": "~2.3"
},
"require-dev": {
"phpspec/phpspec": "~2.0"
},
"config": {
"bin-dir": "bin"
},
"autoload": {
"psr-4": { "Sylius\\Component\\Originator\\": "" }
},
"autoload-dev": {
"psr-4": { "Sylius\\Component\\Originator\\spec\\": "spec/" }
},
"extra": {
"branch-alias": {
"dev-master": "0.12-dev"
}
},
"minimum-stability": "dev"
}

View file

@ -9,31 +9,29 @@
* file that was distributed with this source code.
*/
namespace spec\Sylius\Component\Core\Originator;
namespace spec\Sylius\Component\Originator\Originator;
use Doctrine\Common\Persistence\ManagerRegistry;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
use Doctrine\ORM\EntityManagerInterface;
use Sylius\Component\Core\Model\OriginAwareInterface;
use Sylius\Component\Promotion\Model\PromotionInterface;
use Sylius\Component\Promotion\Model\Promotion;
use Sylius\Component\Originator\Model\OriginAwareInterface;
use Sylius\Component\Resource\Repository\RepositoryInterface;
class OriginatorSpec extends ObjectBehavior
{
public function let(EntityManagerInterface $em)
public function let(ManagerRegistry $manager)
{
$this->beConstructedWith($em);
$this->beConstructedWith($manager);
}
public function it_should_be_initializable()
{
$this->shouldHaveType('Sylius\Component\Core\Originator\Originator');
$this->shouldHaveType('Sylius\Component\Originator\Originator\Originator');
}
public function it_should_be_Sylius_originator()
{
$this->shouldImplement('Sylius\Component\Core\Originator\OriginatorInterface');
$this->shouldImplement('Sylius\Component\Originator\Originator\OriginatorInterface');
}
public function it_throws_exception_if_origin_is_not_an_object(OriginAwareInterface $originAware)
@ -41,21 +39,21 @@ class OriginatorSpec extends ObjectBehavior
$this->shouldThrow('InvalidArgumentException')->duringSetOrigin($originAware, 'umpirsky');
}
public function it_throws_exception_if_origin_has_no_id(OriginAwareInterface $originAware, Promotion $promotion)
public function it_throws_exception_if_origin_has_no_id(OriginAwareInterface $originAware, FakeEntity $entity)
{
$promotion->getId()->willReturn(null);
$entity->getId(null)->willReturn(null);
$this->shouldThrow('InvalidArgumentException')->duringSetOrigin($originAware, $promotion);
$this->shouldThrow('InvalidArgumentException')->duringSetOrigin($originAware, $entity);
}
public function it_sets_origin(OriginAwareInterface $originAware, Promotion $promotion)
public function it_sets_origin(OriginAwareInterface $originAware, FakeEntity $entity)
{
$promotion->getId()->shouldBeCalled()->willReturn(5);
$entity->getId()->willReturn(5);
$originAware->setOriginId(5)->shouldBeCalled()->willReturn($originAware);
$originAware->setOriginId(5)->willReturn($originAware);
$originAware->setOriginType(Argument::any())->shouldBeCalled();
$this->setOrigin($originAware, $promotion);
$this->setOrigin($originAware, $entity);
}
public function it_returns_null_if_there_is_no_origin_id(OriginAwareInterface $originAware)
@ -73,14 +71,23 @@ class OriginatorSpec extends ObjectBehavior
$this->getOrigin($originAware)->shouldReturn(null);
}
public function it_gets_origin($em, RepositoryInterface $repository, OriginAwareInterface $originAware, PromotionInterface $promotion)
public function it_gets_origin($manager, RepositoryInterface $repository, OriginAwareInterface $originAware, FakeEntity $entity)
{
$originAware->getOriginId()->willReturn(5);
$originAware->getOriginType()->willReturn('Sylius\Component\Promotion\Model\Promotion');
$originAware->getOriginType()->willReturn('Sylius\Component\Originator\Model\FakeEntity');
$em->getRepository('Sylius\Component\Promotion\Model\Promotion')->shouldBeCalled()->willReturn($repository);
$repository->findOneBy(array('id' => 5))->shouldBeCalled()->willReturn($promotion);
$manager->getRepository('Sylius\Component\Originator\Model\FakeEntity')->willReturn($repository);
$this->getOrigin($originAware)->shouldReturn($promotion);
$repository->findOneBy(array('id' => 5))->willReturn($entity);
$this->getOrigin($originAware)->shouldReturn($entity);
}
}
class FakeEntity
{
public function getId($value = 5)
{
return $value;
}
}