|
| 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 | +} |
0 commit comments