Skip to content

Commit

Permalink
[OPSRC-417] Add API Platform support (#28)
Browse files Browse the repository at this point in the history
* Enable sylius_api in the test application
  • Loading branch information
jakubtobiasz authored Dec 30, 2021
1 parent b10559c commit 25dc277
Show file tree
Hide file tree
Showing 117 changed files with 4,913 additions and 484 deletions.
7 changes: 5 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
matrix:
php: [7.4, 7.3, 8.0]
symfony: [^4.4, ^5.2]
sylius: [~1.8.0, ~1.9.0, ~1.10.0]
sylius: [~1.10.0]
node: [10.x]
mysql: [5.7]

Expand Down Expand Up @@ -173,11 +173,14 @@ jobs:
name: Run PHPSpec
run: vendor/bin/phpspec run --ansi -f progress --no-interaction


-
name: Run Behat
run: vendor/bin/behat --colors --strict -vvv --no-interaction || vendor/bin/behat --colors --strict -vvv --no-interaction --rerun

-
name: Run PHPUnit
run: ./vendor/bin/phpunit -c ./phpunit.xml.dist

-
name: Upload Behat logs
uses: actions/upload-artifact@v2
Expand Down
8 changes: 4 additions & 4 deletions .github/workflows/coding_standard.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
matrix:
php: [7.4, 7.3, 8.0]
symfony: [^4.4, ^5.2]
sylius: [~1.8.0, ~1.9.0, ~1.10.0]
sylius: [~1.10.0]
node: [10.x]
mysql: [5.7]

Expand Down Expand Up @@ -82,11 +82,11 @@ jobs:
name: Restrict Sylius version
if: matrix.sylius != ''
run: composer require "sylius/sylius:${{ matrix.sylius }}" --no-update --no-scripts --no-interaction

-
name: Install PHP dependencies
run: composer install --no-interaction

- name: Run ECS
run: vendor/bin/ecs

Expand All @@ -98,4 +98,4 @@ jobs:
name: Run PHPStan in /test directory
run: ./vendor/bin/phpstan analyze --configuration=vendor/bitbag/coding-standard/phpstan.neon tests --level=5


2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@

/behat.yml
/phpspec.yml
/phpunit.xml
/.phpunit.result.cache
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ This **open-source plugin was developed to help the Sylius community**. If you h
composer require bitbag/product-bundle-plugin
```

2. Add plugin dependencies to your `config/bundles.php` file:
2. Add plugin dependencies to your `config/bundles.php` file after `Sylius\Bundle\ApiBundle\SyliusApiBundle`.

```php
return [
Expand Down
10 changes: 8 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
"license": "MIT",
"require": {
"php": "^7.3 || ^8.0",
"sylius/sylius": "~1.8.0 || ~1.9.0 || ~1.10.0"
"sylius/sylius": "~1.10.0"
},
"require-dev": {
"ext-json": "*",
"behat/behat": "^3.6.1",
"behat/mink-selenium2-driver": "^1.4",
"dmore/behat-chrome-extension": "^1.3",
Expand Down Expand Up @@ -39,7 +40,12 @@
"vimeo/psalm": "^4.12",
"composer/xdebug-handler": "^2.0",
"friendsofphp/php-cs-fixer": "^3.0",
"bitbag/coding-standard": "^1.0"
"bitbag/coding-standard": "^1.0",
"lchrusciel/api-test-case": "^5.1",
"polishsymfonycommunity/symfony-mocker-container": "^1.0"
},
"conflict": {
"doctrine/orm": "^2.10.0"
},
"autoload": {
"psr-4": {
Expand Down
4 changes: 4 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ parameters:
checkMissingIterableValueType: false
checkGenericClassInNonGenericObjectType: false

level: 7
paths:
- src

excludes_analyse:
# Makes PHPStan crash
- 'src/DependencyInjection/Configuration.php'
Expand Down
25 changes: 25 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>

<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/8.5/phpunit.xsd"
colors="true"
bootstrap="tests/Application/config/bootstrap.php">
<testsuites>
<testsuite name="SyliusProductBundlePlugin Api Test Suite">
<directory>./tests/Api</directory>
</testsuite>
<testsuite name="SyliusProductBundlePlugin Unit Test Suite">
<directory>./tests/Api</directory>
</testsuite>
</testsuites>

<php>
<ini name="error_reporting" value="-1" />

<server name="IS_DOCTRINE_ORM_SUPPORTED" value="true" />
<server name="ESCAPE_JSON" value="true" />

<env name="APP_ENV" value="test"/>
<env name="SHELL_VERBOSITY" value="-1" />
</php>
</phpunit>
9 changes: 9 additions & 0 deletions src/BitBagSyliusProductBundlePlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,19 @@

namespace BitBag\SyliusProductBundlePlugin;

use BitBag\SyliusProductBundlePlugin\DependencyInjection\CompilerPass\AuthenticationManagerPolyfillPass;
use Sylius\Bundle\CoreBundle\Application\SyliusPluginTrait;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Bundle\Bundle;

final class BitBagSyliusProductBundlePlugin extends Bundle
{
use SyliusPluginTrait;

public function build(ContainerBuilder $container): void
{
$container->addCompilerPass(new AuthenticationManagerPolyfillPass());

parent::build($container);
}
}
56 changes: 19 additions & 37 deletions src/Command/AddProductBundleToCartCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,57 +10,39 @@

namespace BitBag\SyliusProductBundlePlugin\Command;

use BitBag\SyliusProductBundlePlugin\Entity\OrderItemInterface;
use BitBag\SyliusProductBundlePlugin\Entity\ProductBundleItemInterface;
use BitBag\SyliusProductBundlePlugin\Entity\ProductInterface;
use Sylius\Component\Order\Model\OrderInterface;

final class AddProductBundleToCartCommand
final class AddProductBundleToCartCommand implements OrderIdentityAwareInterface, ProductCodeAwareInterface
{
/** @var OrderInterface */
private $cart;

/** @var OrderItemInterface */
private $cartItem;
/** @var int */
private $orderId;

/** @var ProductInterface */
private $product;
/** @var string */
private $productCode;

/** @var AddProductBundleItemToCartCommand[] */
private $productBundleItems = [];
/** @var int */
private $quantity;

public function __construct(
OrderInterface $cart,
OrderItemInterface $cartItem,
ProductInterface $product
int $orderId,
string $productCode,
int $quantity = 1
) {
$this->cart = $cart;
$this->cartItem = $cartItem;
$this->product = $product;
assert(null !== $product->getProductBundle());
/** @var ProductBundleItemInterface $productBundleItem */
foreach ($product->getProductBundle()->getProductBundleItems() as $productBundleItem) {
$this->productBundleItems[] = new AddProductBundleItemToCartCommand($productBundleItem);
}
}

public function getProduct(): ProductInterface
{
return $this->product;
$this->orderId = $orderId;
$this->productCode = $productCode;
$this->quantity = $quantity;
}

public function getProductBundleItems(): array
public function getOrderId(): int
{
return $this->productBundleItems;
return $this->orderId;
}

public function getCart(): OrderInterface
public function getProductCode(): string
{
return $this->cart;
return $this->productCode;
}

public function getCartItem(): OrderItemInterface
public function getQuantity(): int
{
return $this->cartItem;
return $this->quantity;
}
}
16 changes: 16 additions & 0 deletions src/Command/OrderIdentityAwareInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

/*
* This file was created by developers working at BitBag
* Do you need more information about us and what we do? Visit our https://bitbag.io website!
* We are hiring developers from all over the world. Join us and start your new, exciting adventure and become part of us: https://bitbag.io/career
*/

declare(strict_types=1);

namespace BitBag\SyliusProductBundlePlugin\Command;

interface OrderIdentityAwareInterface
{
public function getOrderId(): int;
}
16 changes: 16 additions & 0 deletions src/Command/ProductCodeAwareInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

/*
* This file was created by developers working at BitBag
* Do you need more information about us and what we do? Visit our https://bitbag.io website!
* We are hiring developers from all over the world. Join us and start your new, exciting adventure and become part of us: https://bitbag.io/career
*/

declare(strict_types=1);

namespace BitBag\SyliusProductBundlePlugin\Command;

interface ProductCodeAwareInterface
{
public function getProductCode(): string;
}
41 changes: 35 additions & 6 deletions src/Controller/OrderItemController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,16 @@

namespace BitBag\SyliusProductBundlePlugin\Controller;

use BitBag\SyliusProductBundlePlugin\Command\AddProductBundleToCartCommand;
use BitBag\SyliusProductBundlePlugin\Dto\AddProductBundleToCartDto;
use BitBag\SyliusProductBundlePlugin\Entity\OrderItemInterface;
use BitBag\SyliusProductBundlePlugin\Entity\ProductInterface;
use BitBag\SyliusProductBundlePlugin\Factory\AddProductBundleToCartCommandFactoryInterface;
use BitBag\SyliusProductBundlePlugin\Factory\AddProductBundleToCartDtoFactoryInterface;
use Doctrine\ORM\EntityManagerInterface;
use FOS\RestBundle\View\View;
use Sylius\Bundle\OrderBundle\Controller\OrderItemController as BaseOrderItemController;
use Sylius\Bundle\ResourceBundle\Controller;
use Sylius\Component\Core\Repository\OrderRepositoryInterface;
use Sylius\Component\Order\CartActions;
use Sylius\Component\Resource\Factory\FactoryInterface;
use Sylius\Component\Resource\Metadata\MetadataInterface;
Expand All @@ -32,6 +35,15 @@ class OrderItemController extends BaseOrderItemController
/** @var MessageBusInterface */
protected $messageBus;

/** @var OrderRepositoryInterface */
protected $orderRepository;

/** @var AddProductBundleToCartDtoFactoryInterface */
private $addProductBundleToCartDtoFactory;

/** @var AddProductBundleToCartCommandFactoryInterface */
private $addProductBundleToCartCommandFactory;

public function __construct(
MetadataInterface $metadata,
Controller\RequestConfigurationFactoryInterface $requestConfigurationFactory,
Expand All @@ -50,7 +62,10 @@ public function __construct(
Controller\StateMachineInterface $stateMachine,
Controller\ResourceUpdateHandlerInterface $resourceUpdateHandler,
Controller\ResourceDeleteHandlerInterface $resourceDeleteHandler,
MessageBusInterface $messageBus
MessageBusInterface $messageBus,
OrderRepositoryInterface $orderRepository,
AddProductBundleToCartDtoFactoryInterface $addProductBundleToCartDtoFactory,
AddProductBundleToCartCommandFactoryInterface $addProductBundleToCartCommandFactory
) {
parent::__construct(
$metadata,
Expand All @@ -73,6 +88,9 @@ public function __construct(
);

$this->messageBus = $messageBus;
$this->orderRepository = $orderRepository;
$this->addProductBundleToCartDtoFactory = $addProductBundleToCartDtoFactory;
$this->addProductBundleToCartCommandFactory = $addProductBundleToCartCommandFactory;
}

public function addProductBundleAction(Request $request): ?Response
Expand All @@ -90,9 +108,11 @@ public function addProductBundleAction(Request $request): ?Response
/** @var ProductInterface $product */
$product = $orderItem->getProduct();
assert(null !== $configuration->getFormType());

$addProductBundleToCartDto = $this->addProductBundleToCartDtoFactory->createNew($cart, $orderItem, $product);
$form = $this->getFormFactory()->create(
$configuration->getFormType(),
new AddProductBundleToCartCommand($cart, $orderItem, $product),
$addProductBundleToCartDto,
$configuration->getFormOptions()
);

Expand Down Expand Up @@ -120,9 +140,10 @@ private function handleForm(
OrderItemInterface $orderItem,
Request $request
): ?Response {
/** @var AddProductBundleToCartCommand $addProductBundleToCartCommand */
$addProductBundleToCartCommand = $form->getData();
$errors = $this->getCartItemErrors($addProductBundleToCartCommand->getCartItem());
/** @var AddProductBundleToCartDto $addProductBundleToCartDto */
$addProductBundleToCartDto = $form->getData();

$errors = $this->getCartItemErrors($addProductBundleToCartDto->getCartItem());
if (0 < count($errors)) {
$form = $this->getAddToCartFormWithErrors($errors, $form);

Expand All @@ -137,7 +158,15 @@ private function handleForm(

return $this->redirectHandler->redirectToIndex($configuration, $orderItem);
}

$cart = $addProductBundleToCartDto->getCart();
if (null === $cart->getId()) {
$this->orderRepository->add($cart);
}

$addProductBundleToCartCommand = $this->addProductBundleToCartCommandFactory->createFromDto($addProductBundleToCartDto);
$this->messageBus->dispatch($addProductBundleToCartCommand);

$resourceControllerEvent = $this->eventDispatcher->dispatchPostEvent(CartActions::ADD, $configuration, $orderItem);
if ($resourceControllerEvent->hasResponse()) {
return $resourceControllerEvent->getResponse();
Expand Down
Loading

0 comments on commit 25dc277

Please sign in to comment.