Skip to content

Commit

Permalink
[PriceHistory] Add ChannelPricingLogEntry factory
Browse files Browse the repository at this point in the history
  • Loading branch information
Rafikooo committed Mar 17, 2023
1 parent 68d6139 commit 27987fe
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 0 deletions.
6 changes: 6 additions & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,12 @@
<!-- level 8 issues - some fatal errors in PHP -->

<MethodSignatureMismatch errorLevel="info" />

<InvalidStringClass>
<errorLevel type="info">
<file name="src/Sylius/Component/Core/Factory/ChannelPricingLogEntryFactory.php" />
</errorLevel>
</InvalidStringClass>
</issueHandlers>

<stubs>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?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\ChannelPricingInterface;
use Sylius\Component\Core\Model\ChannelPricingLogEntryInterface;
use Sylius\Component\Resource\Exception\UnsupportedMethodException;

final class ChannelPricingLogEntryFactory implements ChannelPricingLogEntryFactoryInterface
{
public function __construct(private string $className)
{
if (!is_a($className, ChannelPricingLogEntryInterface::class, true)) {
throw new \DomainException(sprintf(
'This factory requires %s or its descend to be used as resource',
ChannelPricingLogEntryInterface::class,
));
}
}

/**
* @throws UnsupportedMethodException
*/
public function createNew(): object
{
throw new UnsupportedMethodException('createNew');
}

public function create(
ChannelPricingInterface $channelPricing,
\DateTimeInterface $loggedAt,
int $price,
?int $originalPrice = null,
): ChannelPricingLogEntryInterface {
/** @var ChannelPricingLogEntryInterface $channelPricingLogEntry */
$channelPricingLogEntry = new $this->className(
$channelPricing,
$loggedAt,
$price,
$originalPrice,
);

return $channelPricingLogEntry;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?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\ChannelPricingInterface;
use Sylius\Component\Core\Model\ChannelPricingLogEntryInterface;
use Sylius\Component\Resource\Factory\FactoryInterface;

interface ChannelPricingLogEntryFactoryInterface extends FactoryInterface
{
public function create(
ChannelPricingInterface $channelPricing,
\DateTimeInterface $loggedAt,
int $price,
?int $originalPrice = null,
): ChannelPricingLogEntryInterface;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?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\Model\ChannelPricingInterface;
use Sylius\Component\Core\Model\ChannelPricingLogEntry;
use Sylius\Component\Resource\Exception\UnsupportedMethodException;
use Sylius\Component\Resource\Model\ResourceInterface;

final class ChannelPricingLogEntryFactorySpec extends ObjectBehavior
{
function let(): void
{
$this->beConstructedWith(ChannelPricingLogEntry::class);
}

function it_throws_an_exception_when_invalid_class_name_is_passed(): void
{
$this->beConstructedWith(ResourceInterface::class);

$this->shouldThrow(\DomainException::class)->duringInstantiation();
}

function it_throws_an_exception_when_create_new_is_called(): void
{
$this->shouldThrow(UnsupportedMethodException::class)->during('createNew');
}

function it_creates_a_channel_pricing_log_entry(ChannelPricingInterface $channelPricing): void
{
$date = new \DateTimeImmutable();
$price = 1000;
$originalPrice = 2000;

$this
->create($channelPricing, $date, $price, $originalPrice)
->shouldBeLike(new ChannelPricingLogEntry(
$channelPricing->getWrappedObject(),
$date,
$price,
$originalPrice,
))
;
}
}

0 comments on commit 27987fe

Please sign in to comment.