Skip to content

Commit

Permalink
Merge pull request #1 from delyriand/feautre/add-custom-factory
Browse files Browse the repository at this point in the history
Add custom factory and template events to facilitate override when form fields
  • Loading branch information
delyriand authored Aug 9, 2023
2 parents 455657d + 04e6b69 commit c2e7972
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 9 deletions.
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ PLUGIN_NAME=sylius-${COMPOSE_PROJECT_NAME}-plugin
COMPOSE=docker-compose
YARN=yarn
DOCTRINE_MIGRATIONS_NAMESPACE=MonsieurBiz\SyliusContactRequestPlugin\Migrations
DOCTRINE_ORM_CONFLICT_VERSION=">= 2.15.2"

###
### DEVELOPMENT
Expand Down Expand Up @@ -72,6 +73,9 @@ setup_application:
(cd ${APP_DIR} && ${COMPOSER} config minimum-stability dev)
(cd ${APP_DIR} && ${COMPOSER} config --no-plugins allow-plugins true)
(cd ${APP_DIR} && ${COMPOSER} config --no-plugins --json extra.symfony.endpoint '["https://api.github.com/repos/monsieurbiz/symfony-recipes/contents/index.json?ref=flex/master","flex://defaults"]')
ifdef DOCTRINE_ORM_CONFLICT_VERSION
(cd ${APP_DIR} && cat composer.json | jq --indent 4 '.conflict += {"doctrine/orm": ${DOCTRINE_ORM_CONFLICT_VERSION}}' > composer.json.tmp && mv composer.json.tmp composer.json)
endif
(cd ${APP_DIR} && ${COMPOSER} require --no-install --no-scripts --no-progress sylius/sylius="~${SYLIUS_VERSION}") # Make sure to install the required version of sylius because the sylius-standard has a soft constraint
$(MAKE) ${APP_DIR}/.php-version
$(MAKE) ${APP_DIR}/php.ini
Expand Down
12 changes: 3 additions & 9 deletions src/EmailManager/DecorateContactEmailManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,15 @@
namespace MonsieurBiz\SyliusContactRequestPlugin\EmailManager;

use Doctrine\ORM\EntityManagerInterface;
use MonsieurBiz\SyliusContactRequestPlugin\Entity\ContactRequestInterface;
use MonsieurBiz\SyliusContactRequestPlugin\Factory\ContactRequestFactoryInterface;
use Sylius\Bundle\ShopBundle\EmailManager\ContactEmailManagerInterface;
use Sylius\Component\Core\Model\ChannelInterface;
use Sylius\Component\Resource\Factory\FactoryInterface;

final class DecorateContactEmailManager implements ContactEmailManagerInterface
{
public function __construct(
private ContactEmailManagerInterface $decoratedContactEmailManager,
private FactoryInterface $contactRequestFactory,
private ContactRequestFactoryInterface $contactRequestFactory,
private EntityManagerInterface $contactRequestManager,
) {
}
Expand All @@ -34,12 +33,7 @@ public function sendContactRequest(array $data, array $recipients, ChannelInterf
return;
}

/** @var ContactRequestInterface $contactRequest */
$contactRequest = $this->contactRequestFactory->createNew();
$contactRequest->setEmail($data['email']);
$contactRequest->setMessage($data['message']);
$contactRequest->setChannel($channel);

$contactRequest = $this->contactRequestFactory->createNewFromChannelAndData($channel, $data);
$this->contactRequestManager->persist($contactRequest);
$this->contactRequestManager->flush();
}
Expand Down
39 changes: 39 additions & 0 deletions src/Factory/ContactRequestFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

/*
* This file is part of Monsieur Biz' Contact Request plugin for Sylius.
* (c) Monsieur Biz <[email protected]>
* For the full copyright and license information, please view the LICENSE.txt
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace MonsieurBiz\SyliusContactRequestPlugin\Factory;

use MonsieurBiz\SyliusContactRequestPlugin\Entity\ContactRequestInterface;
use Sylius\Component\Core\Model\ChannelInterface;
use Sylius\Component\Resource\Factory\FactoryInterface;

final class ContactRequestFactory implements ContactRequestFactoryInterface
{
public function __construct(private FactoryInterface $factory)
{
}

public function createNew(): ContactRequestInterface
{
// @phpstan-ignore-next-line
return $this->factory->createNew();
}

public function createNewFromChannelAndData(ChannelInterface $channel, array $data): ContactRequestInterface
{
$contactRequest = $this->createNew();
$contactRequest->setEmail($data['email']);
$contactRequest->setMessage($data['message']);
$contactRequest->setChannel($channel);

return $contactRequest;
}
}
24 changes: 24 additions & 0 deletions src/Factory/ContactRequestFactoryInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

/*
* This file is part of Monsieur Biz' Contact Request plugin for Sylius.
* (c) Monsieur Biz <[email protected]>
* For the full copyright and license information, please view the LICENSE.txt
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace MonsieurBiz\SyliusContactRequestPlugin\Factory;

use MonsieurBiz\SyliusContactRequestPlugin\Entity\ContactRequestInterface;
use Sylius\Component\Core\Model\ChannelInterface;
use Sylius\Component\Resource\Factory\FactoryInterface;

/**
* @method ContactRequestInterface createNew()
*/
interface ContactRequestFactoryInterface extends FactoryInterface
{
public function createNewFromChannelAndData(ChannelInterface $channel, array $data): ContactRequestInterface;
}
6 changes: 6 additions & 0 deletions src/Resources/config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,9 @@ services:
MonsieurBiz\SyliusContactRequestPlugin\Menu\AdminMenuListener:
tags:
- { name: kernel.event_listener, event: sylius.menu.admin.main, priority: -100 }

# Custom contact request factory
MonsieurBiz\SyliusContactRequestPlugin\Factory\ContactRequestFactory:
decorates: 'monsieurbiz_contact_request.factory.contact_request'
arguments: [ '@.inner' ]

2 changes: 2 additions & 0 deletions src/Resources/views/Admin/ContactRequest/Crud/show.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

<div class="ui fluid card">
<div class="content">
{{ sylius_template_event('monsieurbiz_contact.admin.contact_request.before_content', {'resource': resource}) }}
<h2>{{ 'monsieurbiz.contact_request.ui.email'|trans }}</h2>
<p>
{{ resource.email|default('') }}
Expand All @@ -36,6 +37,7 @@
<div>
{{ resource.message|nl2br|default('')|raw }}
</div>
{{ sylius_template_event('monsieurbiz_contact.admin.contact_request.after_content', {'resource': resource}) }}
</div>
</div>
{% endblock %}

0 comments on commit c2e7972

Please sign in to comment.