Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use shop interface instead of abstract shop class #41

Merged
merged 1 commit into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions infection.json5
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,10 @@
},
"mutators": {
"@default": true,
"MatchArmRemoval": {
"ignoreSourceCodeByRegex": [
"default => throw new \\\\RuntimeException\\(sprintf\\('Unsupported type %s', \\$type\\)\\).*"
]
}
}
}
11 changes: 5 additions & 6 deletions src/Entity/ShopRepositoryBridge.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,19 @@
use Shopware\App\SDK\Shop\ShopRepositoryInterface;

/**
* @implements ShopRepositoryInterface<AbstractShop>
* @implements ShopRepositoryInterface<ShopInterface>
*/
class ShopRepositoryBridge implements ShopRepositoryInterface
{
/**
* @param class-string<AbstractShop> $entityClass
* @param class-string<ShopInterface> $entityClass
*/
public function __construct(
private readonly string $entityClass,
private readonly ManagerRegistry $registry
) {
if (!is_subclass_of($this->entityClass, AbstractShop::class)) {
throw new \InvalidArgumentException(sprintf('The shop entity class "%s" must extend "%s"', $this->entityClass, AbstractShop::class));
if (!is_subclass_of($this->entityClass, ShopInterface::class)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this really working for interfaces? not class_implements?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://www.php.net/manual/en/function.is-subclass-of.php

"Checks if the given object_or_class has the class class as one of its parents or implements it."

throw new \InvalidArgumentException(sprintf('The shop entity class "%s" must implement "%s"', $this->entityClass, ShopInterface::class));
}
if ($this->registry->getManagerForClass($this->entityClass) === null) {
throw new \InvalidArgumentException(sprintf('The shop entity class "%s" must be a doctrine managed entity', $this->entityClass));
Expand Down Expand Up @@ -65,9 +65,8 @@ public function deleteShop(string $shopId): void

private function getManager(): ObjectManager
{
/** @var ObjectManager $manager */
$manager = $this->registry->getManagerForClass($this->entityClass);
// we check that $shopEntity is a doctrine entity in the constructor
assert($manager !== null);
return $manager;
}
}
6 changes: 3 additions & 3 deletions tests/Entity/BaseShopRepositoryBridgeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

class BaseShopRepositoryBridgeTest extends TestCase
{
public function testConstructionFailsWithIncorrectShopEntity()
public function testConstructionFailsWithIncorrectShopEntity(): void
{
static::expectException(\InvalidArgumentException::class);

Expand All @@ -34,7 +34,7 @@ public function testConstructionFailsWithIncorrectShopEntity()
}


public function testConstructionFailsForNonDoctrineEntities()
public function testConstructionFailsForNonDoctrineEntities(): void
{
$this->expectException(\InvalidArgumentException::class);

Expand All @@ -51,7 +51,7 @@ public function __construct(string $shopId, string $shopUrl, string $shopSecret)
);
}

public function testBridgeCanConstructCustomShopEntity()
public function testBridgeCanConstructCustomShopEntity(): void
{
$customShop = new class ('', '', '') extends AbstractShop {
public function __construct(string $shopId, string $shopUrl, string $shopSecret)
Expand Down