Skip to content

Commit

Permalink
Refactor Factory
Browse files Browse the repository at this point in the history
  • Loading branch information
SirDomin committed Jun 18, 2021
1 parent b0247e7 commit 42e35b0
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 117 deletions.
2 changes: 0 additions & 2 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,3 @@ parameters:
- '/Method Sylius\\RefundPlugin\\Entity\\CustomerBillingData::setId\(\) has parameter \$id with no typehint specified./'
- '/Method Sylius\\RefundPlugin\\Entity\\CustomerBillingDataInterface::id\(\) has no return typehint specified./'
- '/Method Sylius\\RefundPlugin\\Entity\\CustomerBillingDataInterface::setId\(\) has parameter \$id with no typehint specified./'
- '/Method Sylius\\RefundPlugin\\Entity\\ShopBillingData::setId\(\) has parameter \$id with no typehint specified./'
- '/Method Sylius\\RefundPlugin\\Entity\\ShopBillingDataInterface::setId\(\) has parameter \$id with no typehint specified./'
23 changes: 11 additions & 12 deletions spec/Entity/ShopBillingDataSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@

final class ShopBillingDataSpec extends ObjectBehavior
{
public function let(): void
{
$this->beConstructedWith('Needful Things', '000222', 'US', 'Main St. 123', 'Los Angeles', '90001');
}

public function it_implements_shop_billing_data_interface(): void
{
$this->shouldImplement(ShopBillingDataInterface::class);
Expand All @@ -28,39 +33,33 @@ public function it_has_no_id_by_default(): void
$this->getId()->shouldReturn(null);
}

public function it_has_a_company(): void
public function it_has_company(): void
{
$this->setCompany('Needful Things');
$this->getCompany()->shouldReturn('Needful Things');
}

public function it_has_a_tax_id(): void
public function it_has_tax_id(): void
{
$this->setTaxId('000222');
$this->getTaxId()->shouldReturn('000222');
}

public function it_has_a_country_code(): void
public function it_has_country_code(): void
{
$this->setCountryCode('US');
$this->getCountryCode()->shouldReturn('US');
}

public function it_has_a_street(): void
public function it_has_street(): void
{
$this->setStreet('Main St. 123');
$this->getStreet()->shouldReturn('Main St. 123');
}

public function it_has_a_city(): void
public function it_has_city(): void
{
$this->setCity('Los Angeles');
$this->getCity()->shouldReturn('Los Angeles');
}

public function it_has_a_postcode(): void
public function it_has_postcode(): void
{
$this->setPostcode('90001');
$this->getPostcode()->shouldReturn('90001');
}
}
34 changes: 8 additions & 26 deletions spec/Factory/ShopBillingDataFactorySpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,43 +14,25 @@
namespace spec\Sylius\RefundPlugin\Factory;

use PhpSpec\ObjectBehavior;
use Sylius\Component\Resource\Factory\FactoryInterface;
use Sylius\RefundPlugin\Entity\ShopBillingDataInterface;
use Sylius\Component\Resource\Exception\UnsupportedMethodException;
use Sylius\RefundPlugin\Entity\ShopBillingData;
use Sylius\RefundPlugin\Factory\ShopBillingDataFactoryInterface;

class ShopBillingDataFactorySpec extends ObjectBehavior
{
public function let(FactoryInterface $shopBillingDataFactory): void
{
$this->beConstructedWith($shopBillingDataFactory);
}

public function it_implements_shop_billing_data_factory_interface(): void
{
$this->shouldImplement(ShopBillingDataFactoryInterface::class);
}

public function it_creates_new_shop_billing_data(
FactoryInterface $shopBillingDataFactory,
ShopBillingDataInterface $shopBillingData
): void {
$shopBillingDataFactory->createNew()->willReturn($shopBillingData);

$this->createNew()->shouldReturn($shopBillingData);
public function it_throws_unsupported_method_exception_on_create_new(): void
{
$this->shouldThrow(UnsupportedMethodException::class)->during('createNew');
}

public function it_creates_new_shop_billing_data_with_data(
ShopBillingDataInterface $shopBillingData,
FactoryInterface $shopBillingDataFactory
): void {
$shopBillingDataFactory->createNew()->willReturn($shopBillingData);

$shopBillingData->setCompany('Needful Things')->shouldBeCalled();
$shopBillingData->setTaxId('000222')->shouldBeCalled();
$shopBillingData->setCountryCode('US')->shouldBeCalled();
$shopBillingData->setStreet('Main St. 123')->shouldBeCalled();
$shopBillingData->setCity('Los Angeles')->shouldBeCalled();
$shopBillingData->setPostcode('90001')->shouldBeCalled();
public function it_creates_new_shop_billing_data_with_data(): void
{
$shopBillingData = new ShopBillingData('Needful Things', '000222', 'US', 'Main St. 123', 'Los Angeles', '90001');

$this
->createWithData('Needful Things', '000222', 'US', 'Main St. 123', 'Los Angeles', '90001')
Expand Down
40 changes: 7 additions & 33 deletions src/Entity/ShopBillingData.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@

namespace Sylius\RefundPlugin\Entity;

use Sylius\Component\Resource\Exception\UnsupportedMethodException;

/** final */
class ShopBillingData implements ShopBillingDataInterface
{
Expand Down Expand Up @@ -47,8 +45,14 @@ public function __construct(
?string $city,
?string $postcode
) {
throw new UnsupportedMethodException('construct');
$this->company = $company;
$this->taxId = $taxId;
$this->countryCode = $countryCode;
$this->street = $street;
$this->city = $city;
$this->postcode = $postcode;
}

/**
* @return mixed
*/
Expand All @@ -62,61 +66,31 @@ public function getCompany(): ?string
return $this->company;
}

public function setCompany(?string $company): void
{
$this->company = $company;
}

public function getTaxId(): ?string
{
return $this->taxId;
}

public function setTaxId(?string $taxId): void
{
$this->taxId = $taxId;
}

public function getCountryCode(): ?string
{
return $this->countryCode;
}

public function setCountryCode(?string $countryCode): void
{
$this->countryCode = $countryCode;
}

public function getStreet(): ?string
{
return $this->street;
}

public function setStreet(?string $street): void
{
$this->street = $street;
}

public function getCity(): ?string
{
return $this->city;
}

public function setCity(?string $city): void
{
$this->city = $city;
}

public function getPostcode(): ?string
{
return $this->postcode;
}

public function setPostcode(?string $postcode): void
{
$this->postcode = $postcode;
}

/**
* @return mixed
*/
Expand Down
13 changes: 1 addition & 12 deletions src/Entity/ShopBillingDataInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,30 +24,19 @@ public function getId();

public function getCompany(): ?string;

public function setCompany(?string $company): void;

public function getTaxId(): ?string;

public function setTaxId(?string $taxId): void;

public function getCountryCode(): ?string;

public function setCountryCode(?string $countryCode): void;

public function getStreet(): ?string;

public function setStreet(?string $street): void;

public function getCity(): ?string;

public function setCity(?string $city): void;

public function getPostcode(): ?string;

public function setPostcode(?string $postcode): void;

/**
* @return mixed
*
* @deprecated this function is deprecated and will be removed in v1.0.0. Use CustomerBillingDataInterface::getId() instead
* @see CustomerBillingDataInterface::getId
*/
Expand Down
27 changes: 4 additions & 23 deletions src/Factory/ShopBillingDataFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,15 @@

namespace Sylius\RefundPlugin\Factory;

use Sylius\Component\Resource\Factory\FactoryInterface;
use Sylius\Component\Resource\Exception\UnsupportedMethodException;
use Sylius\RefundPlugin\Entity\ShopBillingData;
use Sylius\RefundPlugin\Entity\ShopBillingDataInterface;

final class ShopBillingDataFactory implements ShopBillingDataFactoryInterface
{
/** @var FactoryInterface */
private $shopBillingDataFactory;

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

public function createNew(): ShopBillingDataInterface
{
/** @var ShopBillingDataInterface $shopBillingData */
$shopBillingData = $this->shopBillingDataFactory->createNew();

return $shopBillingData;
throw new UnsupportedMethodException('createNew');
}

public function createWithData(
Expand All @@ -42,15 +32,6 @@ public function createWithData(
?string $city,
?string $postcode
): ShopBillingDataInterface {
$shopBillingData = $this->createNew();

$shopBillingData->setCompany($company);
$shopBillingData->setTaxId($taxId);
$shopBillingData->setCountryCode($countryCode);
$shopBillingData->setStreet($street);
$shopBillingData->setCity($city);
$shopBillingData->setPostcode($postcode);

return $shopBillingData;
return new ShopBillingData($company, $taxId, $countryCode, $street, $city, $postcode);
}
}
2 changes: 2 additions & 0 deletions src/Resources/config/services/factories.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
<deprecated>The "%alias_id%" service alias is deprecated and will be removed in RefundPlugin 1.0, use Sylius\RefundPlugin\Factory\RefundPaymentFactoryInterface instead.</deprecated>
</service>

<service id="Sylius\RefundPlugin\Factory\ShopBillingDataFactoryInterface" class="Sylius\RefundPlugin\Factory\ShopBillingDataFactory" />

<service
id="Sylius\RefundPlugin\Factory\CreditMemoFactory"
decorates="sylius_refund.factory.credit_memo"
Expand Down
9 changes: 0 additions & 9 deletions src/Resources/config/services/generators.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,6 @@
<argument>%sylius.refund.template.logo_file%</argument>
</service>

<service
id="Sylius\RefundPlugin\Factory\ShopBillingDataFactory"
decorates="sylius_refund.factory.shop_billing_data"
decoration-priority="256"
public="false"
>
<argument type="service" id="Sylius\RefundPlugin\Factory\ShopBillingDataFactory.inner" />
</service>

<service id="Sylius\RefundPlugin\Generator\CreditMemoPdfFileGenerator" alias="Sylius\RefundPlugin\Generator\CreditMemoPdfFileGeneratorInterface">
<deprecated>The "%alias_id%" service alias is deprecated and will be removed in RefundPlugin 1.0, use Sylius\RefundPlugin\Generator\CreditMemoPdfFileGeneratorInterface instead.</deprecated>
</service>
Expand Down

0 comments on commit 42e35b0

Please sign in to comment.