forked from Sylius/Sylius
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[PriceHistory] Add ChannelPricingLogEntry factory
- Loading branch information
Showing
4 changed files
with
147 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
src/Sylius/Component/Core/Factory/ChannelPricingLogEntryFactory.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
src/Sylius/Component/Core/Factory/ChannelPricingLogEntryFactoryInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
57 changes: 57 additions & 0 deletions
57
src/Sylius/Component/Core/spec/Factory/ChannelPricingLogEntryFactorySpec.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
)) | ||
; | ||
} | ||
} |