Skip to content

Commit d573a5b

Browse files
sophie-mulardjuchi
authored andcommitted
Content duplicate
1 parent fa78394 commit d573a5b

File tree

3 files changed

+94
-0
lines changed

3 files changed

+94
-0
lines changed

src/Factory/NewContentFactory.php

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
3+
namespace Sherlockode\SyliusAdvancedContentPlugin\Factory;
4+
5+
use Doctrine\ORM\EntityManagerInterface;
6+
use Sherlockode\AdvancedContentBundle\Manager\ConfigurationManager;
7+
use Sherlockode\AdvancedContentBundle\Manager\ContentManager;
8+
use Sherlockode\AdvancedContentBundle\Model\ContentInterface;
9+
use Sylius\Bundle\ResourceBundle\Controller\NewResourceFactoryInterface;
10+
use Sylius\Bundle\ResourceBundle\Controller\RequestConfiguration;
11+
use Sylius\Component\Resource\Factory\FactoryInterface;
12+
use Sylius\Component\Resource\Model\ResourceInterface;
13+
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
14+
15+
class NewContentFactory implements NewResourceFactoryInterface
16+
{
17+
/**
18+
* @var NewResourceFactoryInterface
19+
*/
20+
private $newResourceFactory;
21+
22+
/**
23+
* @var EntityManagerInterface
24+
*/
25+
private $em;
26+
27+
/**
28+
* @var ConfigurationManager
29+
*/
30+
private $configurationManager;
31+
32+
/**
33+
* @var ContentManager
34+
*/
35+
private $contentManager;
36+
37+
/**
38+
* @param NewResourceFactoryInterface $newResourceFactory
39+
* @param EntityManagerInterface $em
40+
* @param ConfigurationManager $configurationManager
41+
* @param ContentManager $contentManager
42+
*/
43+
public function __construct(
44+
NewResourceFactoryInterface $newResourceFactory,
45+
EntityManagerInterface $em,
46+
ConfigurationManager $configurationManager,
47+
ContentManager $contentManager
48+
) {
49+
$this->newResourceFactory = $newResourceFactory;
50+
$this->em = $em;
51+
$this->configurationManager = $configurationManager;
52+
$this->contentManager = $contentManager;
53+
}
54+
55+
public function create(RequestConfiguration $requestConfiguration, FactoryInterface $factory): ResourceInterface
56+
{
57+
$resource = $this->newResourceFactory->create($requestConfiguration, $factory);
58+
59+
if (!$resource instanceof ContentInterface) {
60+
return $resource;
61+
}
62+
63+
$request = $requestConfiguration->getRequest();
64+
$duplicateId = $request->get('duplicateId');
65+
66+
if ($duplicateId === null) {
67+
return $resource;
68+
}
69+
70+
$content = $this->em->getRepository($this->configurationManager->getEntityClass('content'))->find($duplicateId);
71+
if ($content === null) {
72+
throw new NotFoundHttpException(sprintf('The content has not been found'));
73+
}
74+
75+
return $this->contentManager->duplicate($content);
76+
}
77+
}

src/Resources/config/services.yaml

+8
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,11 @@ services:
5858
- '@doctrine.orm.entity_manager'
5959
- '@sherlockode_advanced_content.configuration_manager'
6060
- '@sherlockode_advanced_content.page_manager'
61+
sherlockode_sylius_acb_content_factory:
62+
class: Sherlockode\SyliusAdvancedContentPlugin\Factory\NewContentFactory
63+
decorates: 'sylius.resource_controller.new_resource_factory'
64+
arguments:
65+
- '@sherlockode_sylius_acb_content_factory.inner'
66+
- '@doctrine.orm.entity_manager'
67+
- '@sherlockode_advanced_content.configuration_manager'
68+
- '@sherlockode_advanced_content.content_manager'

src/Resources/config/sylius_grid.yaml

+9
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,15 @@ sylius_grid:
6464
type: update
6565
delete:
6666
type: delete
67+
duplicate:
68+
type: acb_duplicate
69+
label: sherlockode_sylius_acb.ui.duplicate
70+
icon: copy
71+
options:
72+
link:
73+
route: sherlockode_sylius_acb_admin_content_create
74+
parameters:
75+
duplicateId: resource.id
6776
templates:
6877
action:
6978
acb_duplicate: "@SherlockodeSyliusAdvancedContentPlugin/Grid/Action/duplicate.html.twig"

0 commit comments

Comments
 (0)