-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor checking whether the channel can be deleted
- Loading branch information
1 parent
14a94c0
commit 2e05cbc
Showing
17 changed files
with
295 additions
and
171 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
47 changes: 47 additions & 0 deletions
47
src/Sylius/Bundle/ApiBundle/DataPersister/ChannelDataPersister.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,47 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Sylius package. | ||
* | ||
* (c) Sylius Sp. z o.o. | ||
* | ||
* 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\Bundle\ApiBundle\DataPersister; | ||
|
||
use ApiPlatform\Core\DataPersister\ContextAwareDataPersisterInterface; | ||
use Sylius\Bundle\ApiBundle\Exception\ChannelCannotBeRemoved; | ||
use Sylius\Component\Channel\Checker\ChannelDeletionCheckerInterface; | ||
use Sylius\Component\Core\Model\ChannelInterface; | ||
|
||
final class ChannelDataPersister implements ContextAwareDataPersisterInterface | ||
{ | ||
public function __construct ( | ||
private ContextAwareDataPersisterInterface $decoratedDataPersister, | ||
private ChannelDeletionCheckerInterface $channelDeletionChecker, | ||
) { | ||
} | ||
|
||
public function supports($data, array $context = []): bool | ||
{ | ||
return $data instanceof ChannelInterface; | ||
} | ||
|
||
public function persist($data, array $context = []) | ||
{ | ||
return $this->decoratedDataPersister->persist($data, $context); | ||
} | ||
|
||
public function remove($data, array $context = []) | ||
{ | ||
if (!$this->channelDeletionChecker->isDeletable($data)) { | ||
throw new ChannelCannotBeRemoved('The channel cannot be deleted. At least one enabled channel is required.'); | ||
} | ||
|
||
return $this->decoratedDataPersister->remove($data, $context); | ||
} | ||
} |
47 changes: 0 additions & 47 deletions
47
src/Sylius/Bundle/ApiBundle/EventListener/ChannelDeletionEventListener.php
This file was deleted.
Oops, something went wrong.
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
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
68 changes: 68 additions & 0 deletions
68
src/Sylius/Bundle/ApiBundle/spec/DataPersister/ChannelDataPersisterSpec.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,68 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Sylius package. | ||
* | ||
* (c) Sylius Sp. z o.o. | ||
* | ||
* 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\Bundle\ApiBundle\DataPersister; | ||
|
||
use ApiPlatform\Core\DataPersister\ContextAwareDataPersisterInterface; | ||
use PhpSpec\ObjectBehavior; | ||
use Sylius\Bundle\ApiBundle\Exception\ChannelCannotBeRemoved; | ||
use Sylius\Component\Channel\Checker\ChannelDeletionCheckerInterface; | ||
use Sylius\Component\Core\Model\ChannelInterface; | ||
|
||
final class ChannelDataPersisterSpec extends ObjectBehavior | ||
{ | ||
function let( | ||
ContextAwareDataPersisterInterface $decoratedDataPersister, | ||
ChannelDeletionCheckerInterface $channelDeletionChecker, | ||
): void { | ||
$this->beConstructedWith($decoratedDataPersister, $channelDeletionChecker); | ||
} | ||
|
||
function it_supports_only_channel_entity(ChannelInterface $channel, \stdClass $object): void | ||
{ | ||
$this->supports($channel)->shouldReturn(true); | ||
$this->supports($object)->shouldReturn(false); | ||
} | ||
|
||
function it_persists_channel( | ||
ContextAwareDataPersisterInterface $decoratedDataPersister, | ||
ChannelInterface $channel | ||
): void { | ||
$decoratedDataPersister->persist($channel, [])->willReturn($channel); | ||
|
||
$this->persist($channel, [])->shouldReturn($channel); | ||
} | ||
|
||
function it_throws_an_exception_if_channel_is_not_deletable( | ||
ChannelDeletionCheckerInterface $channelDeletionChecker, | ||
ChannelInterface $channel | ||
): void { | ||
$channelDeletionChecker->isDeletable($channel)->willReturn(false); | ||
|
||
$this | ||
->shouldThrow(ChannelCannotBeRemoved::class) | ||
->during('remove', [$channel]) | ||
; | ||
} | ||
|
||
function it_removes_channel( | ||
ContextAwareDataPersisterInterface $decoratedDataPersister, | ||
ChannelDeletionCheckerInterface $channelDeletionChecker, | ||
ChannelInterface $channel | ||
): void { | ||
$channelDeletionChecker->isDeletable($channel)->willReturn(true); | ||
$decoratedDataPersister->remove($channel, [])->willReturn(null); | ||
|
||
$this->remove($channel, [])->shouldReturn(null); | ||
} | ||
} |
112 changes: 0 additions & 112 deletions
112
src/Sylius/Bundle/ApiBundle/spec/EventListener/ChannelDeletionEventListenerSpec.php
This file was deleted.
Oops, something went wrong.
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
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
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
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
Oops, something went wrong.