Skip to content

Commit

Permalink
create Core ChannelFactoryInterface
Browse files Browse the repository at this point in the history
  • Loading branch information
oallain committed May 15, 2020
1 parent f35af58 commit 5cbb841
Show file tree
Hide file tree
Showing 11 changed files with 145 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
use Sylius\Bundle\CoreBundle\Fixture\OptionsResolver\LazyOption;
use Sylius\Component\Addressing\Model\Scope as AddressingScope;
use Sylius\Component\Addressing\Model\ZoneInterface;
use Sylius\Component\Channel\Factory\ChannelFactoryInterface;
use Sylius\Component\Core\Factory\ChannelFactoryInterface;
use Sylius\Component\Core\Formatter\StringInflector;
use Sylius\Component\Core\Model\ChannelInterface;
use Sylius\Component\Core\Model\Scope;
Expand Down
4 changes: 4 additions & 0 deletions src/Sylius/Bundle/CoreBundle/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,10 @@
<argument type="service" id="sylius.custom_factory.address.inner" />
</service>

<service id="sylius.custom_factory.channel" class="Sylius\Component\Core\Factory\ChannelFactory" decorates="sylius.factory.channel" decoration-priority="256" public="false">
<argument type="service" id="sylius.custom_factory.channel.inner" />
</service>

<service id="sylius.factory.customer_after_checkout" class="Sylius\Component\Core\Factory\CustomerAfterCheckoutFactory">
<argument type="service" id="sylius.factory.customer" />
</service>
Expand Down
53 changes: 53 additions & 0 deletions src/Sylius/Component/Core/Factory/ChannelFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?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\Component\Core\Factory;

use Sylius\Component\Core\Model\ChannelInterface;
use Sylius\Component\Resource\Factory\FactoryInterface;

class ChannelFactory implements ChannelFactoryInterface
{
/** @var FactoryInterface */
private $decoratedFactory;

public function __construct(FactoryInterface $decoratedFactory)
{
$this->decoratedFactory = $decoratedFactory;
}

/**
* {@inheritdoc}
*/
public function createNew(): ChannelInterface
{
/** @var ChannelInterface $channel */
$channel = $this->decoratedFactory->createNew();
$channel->setTaxCalculationStrategy('order_items_based');

return $channel;
}

/**
* {@inheritdoc}
*/
public function createNamed(string $name): ChannelInterface
{
/** @var ChannelInterface $channel */
$channel = $this->decoratedFactory->createNew();
$channel->setTaxCalculationStrategy('order_items_based');
$channel->setName($name);

return $channel;
}
}
22 changes: 22 additions & 0 deletions src/Sylius/Component/Core/Factory/ChannelFactoryInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?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\Component\Core\Factory;

use Sylius\Component\Core\Model\ChannelInterface;
use Sylius\Component\Resource\Factory\FactoryInterface;

interface ChannelFactoryInterface extends FactoryInterface
{
public function createNamed(string $name): ChannelInterface;
}
2 changes: 1 addition & 1 deletion src/Sylius/Component/Core/Model/Channel.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class Channel extends BaseChannel implements ChannelInterface
protected $defaultTaxZone;

/** @var string */
protected $taxCalculationStrategy = 'order_items_based';
protected $taxCalculationStrategy;

/** @var CurrencyInterface[]|Collection */
protected $currencies;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

namespace Sylius\Component\Core\Test\Services;

use Sylius\Component\Channel\Factory\ChannelFactoryInterface;
use Sylius\Component\Core\Factory\ChannelFactoryInterface;
use Sylius\Component\Core\Model\ChannelInterface;
use Sylius\Component\Currency\Model\CurrencyInterface;
use Sylius\Component\Locale\Model\LocaleInterface;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
use Sylius\Component\Addressing\Factory\ZoneFactoryInterface;
use Sylius\Component\Addressing\Model\CountryInterface;
use Sylius\Component\Addressing\Model\ZoneInterface;
use Sylius\Component\Channel\Factory\ChannelFactoryInterface;
use Sylius\Component\Core\Factory\ChannelFactoryInterface;
use Sylius\Component\Core\Model\ChannelInterface;
use Sylius\Component\Currency\Model\CurrencyInterface;
use Sylius\Component\Locale\Model\LocaleInterface;
Expand Down
58 changes: 58 additions & 0 deletions src/Sylius/Component/Core/spec/Factory/ChannelFactorySpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?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 spec\Sylius\Component\Core\Factory;

use PhpSpec\ObjectBehavior;
use Sylius\Component\Core\Factory\ChannelFactoryInterface;
use Sylius\Component\Core\Model\ChannelInterface;
use Sylius\Component\Resource\Factory\FactoryInterface;

final class ChannelFactorySpec extends ObjectBehavior
{
function let(FactoryInterface $decoratedFactory): void
{
$this->beConstructedWith($decoratedFactory);
}

function it_implements_channel_factory_interface(): void
{
$this->shouldImplement(ChannelFactoryInterface::class);
}

function it_is_a_resource_factory(): void
{
$this->shouldImplement(FactoryInterface::class);
}

function it_creates_a_new_channel(FactoryInterface $decoratedFactory, ChannelInterface $channel): void
{
$decoratedFactory->createNew()->willReturn($channel);

$channel->setTaxCalculationStrategy('order_items_based')->shouldBeCalled();

$this->createNew()->shouldReturn($channel);
}

function it_creates_a_new_channel_with_name(
FactoryInterface $decoratedFactory,
ChannelInterface $channel
): void {
$decoratedFactory->createNew()->willReturn($channel);

$channel->setTaxCalculationStrategy('order_items_based')->shouldBeCalled();
$channel->setName('name')->shouldBeCalled();

$this->createNamed('name')->shouldReturn($channel);
}
}
4 changes: 2 additions & 2 deletions src/Sylius/Component/Core/spec/Model/ChannelSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ function its_default_tax_zone_is_mutable(ZoneInterface $defaultTaxZone): void
$this->getDefaultTaxZone()->shouldReturn($defaultTaxZone);
}

function it_has_a_tax_calculation_strategy_by_default(): void
function it_has_no_tax_calculation_strategy_by_default(): void
{
$this->getTaxCalculationStrategy()->shouldReturn('order_items_based');
$this->getTaxCalculationStrategy()->shouldReturn(null);
}

function its_tax_calculation_strategy_is_mutable(): void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
namespace spec\Sylius\Component\Core\Test\Services;

use PhpSpec\ObjectBehavior;
use Sylius\Component\Channel\Factory\ChannelFactoryInterface;
use Sylius\Component\Core\Factory\ChannelFactoryInterface;
use Sylius\Component\Core\Model\ChannelInterface;
use Sylius\Component\Core\Test\Services\DefaultChannelFactoryInterface;
use Sylius\Component\Currency\Model\CurrencyInterface;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
use Sylius\Component\Addressing\Factory\ZoneFactoryInterface;
use Sylius\Component\Addressing\Model\CountryInterface;
use Sylius\Component\Addressing\Model\ZoneInterface;
use Sylius\Component\Channel\Factory\ChannelFactoryInterface;
use Sylius\Component\Core\Factory\ChannelFactoryInterface;
use Sylius\Component\Core\Model\ChannelInterface;
use Sylius\Component\Core\Test\Services\DefaultChannelFactoryInterface;
use Sylius\Component\Currency\Model\CurrencyInterface;
Expand Down

0 comments on commit 5cbb841

Please sign in to comment.