Completed
Push — master ( e48710...5fe26c )
by Rafał
40:26 queued 26:25
created

SeoMetadataController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
cc 1
nc 1
nop 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SWP\Bundle\CoreBundle\Controller;
6
7
use Doctrine\Common\Persistence\ObjectManager;
8
use Nelmio\ApiDocBundle\Annotation\ApiDoc;
9
use SWP\Bundle\CoreBundle\Service\SeoImageUploaderInterface;
10
use SWP\Bundle\SeoBundle\Form\Type\SeoMetadataType;
11
use SWP\Component\Storage\Factory\FactoryInterface;
12
use SWP\Component\Storage\Repository\RepositoryInterface;
13
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
14
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
15
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
16
use Symfony\Component\Routing\Annotation\Route;
17
use SWP\Bundle\MultiTenancyBundle\MultiTenancyEvents;
18
use SWP\Component\Common\Response\ResponseContext;
19
use SWP\Component\Common\Response\SingleResourceResponse;
20
use Symfony\Component\HttpFoundation\Request;
21
22
class SeoMetadataController extends AbstractController
23
{
24
    private $seoMetadataFactory;
25
26
    private $seoMetadataRepository;
27
28
    private $seoMetadataObjectManager;
29
30
    private $eventDispatcher;
31
32
    public function __construct(
33
        FactoryInterface $seoMetadataFactory,
34
        RepositoryInterface $seoMetadataRepository,
35
        ObjectManager $seoMetadataObjectManager,
36
        EventDispatcherInterface $eventDispatcher
37
    ) {
38
        $this->seoMetadataFactory = $seoMetadataFactory;
39
        $this->seoMetadataRepository = $seoMetadataRepository;
40
        $this->seoMetadataObjectManager = $seoMetadataObjectManager;
41
        $this->eventDispatcher = $eventDispatcher;
42
    }
43
44
    /**
45
     * @ApiDoc(
46
     *     resource=true,
47
     *     description="Add a new SEO metadata entry",
48
     *     statusCodes={
49
     *         201="Returned on success.",
50
     *         400="Returned when form have errors"
51
     *     },
52
     *     input="SWP\Bundle\SeoBundle\Form\Type\SeoMetadataType"
53
     * )
54
     *
55
     * @Route("/api/{version}/seo/", options={"expose"=true}, defaults={"version"="v2"}, methods={"POST"}, name="swp_api_core_seo_metadata_create")
56
     *
57
     * @param Request $request
58
     *
59
     * @return SingleResourceResponse
60
     */
61
    public function create(Request $request, SeoImageUploaderInterface $seoImageUploader): SingleResourceResponse
62
    {
63
        $this->eventDispatcher->dispatch(MultiTenancyEvents::TENANTABLE_DISABLE);
64
65
        $seoMetadata = $this->seoMetadataFactory->create();
66
        $form = $form = $this->get('form.factory')->createNamed('', SeoMetadataType::class, $seoMetadata, ['method' => $request->getMethod()]);
67
68
        $form->handleRequest($request);
69
70
        if ($form->isSubmitted() && $form->isValid()) {
71
            $existingSeoMetadata = $this->seoMetadataRepository->findOneByPackageGuid($seoMetadata->getPackageGuid());
0 ignored issues
show
Bug introduced by
The method findOneByPackageGuid() does not exist on SWP\Component\Storage\Re...ory\RepositoryInterface. Did you maybe mean findOneBy()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
72
            if (null !== $existingSeoMetadata) {
73
                $this->seoMetadataRepository->remove($existingSeoMetadata);
74
            }
75
76
            $seoImageUploader->handleUpload($seoMetadata);
77
78
            $this->seoMetadataObjectManager->persist($seoMetadata);
79
            $this->seoMetadataObjectManager->flush();
80
81
            return new SingleResourceResponse($seoMetadata, new ResponseContext(200));
82
        }
83
84
        return new SingleResourceResponse($form, new ResponseContext(400));
85
    }
86
87
    /**
88
     * @ApiDoc(
89
     *     resource=true,
90
     *     description="Edits SEO metadata entry",
91
     *     statusCodes={
92
     *         201="Returned on success.",
93
     *         400="Returned when form have errors"
94
     *     },
95
     *     input="SWP\Bundle\SeoBundle\Form\Type\SeoMetadataType"
96
     * )
97
     *
98
     * @Route("/api/{version}/seo/{packageGuid}", options={"expose"=true}, defaults={"version"="v2"}, methods={"PATCH"}, name="swp_api_core_seo_metadata_edit")
99
     *
100
     * @param Request $request
101
     *
102
     * @return SingleResourceResponse
103
     */
104
    public function edit(Request $request, string $packageGuid, SeoImageUploaderInterface $seoImageUploader): SingleResourceResponse
105
    {
106
        $this->eventDispatcher->dispatch(MultiTenancyEvents::TENANTABLE_DISABLE);
107
108
        $existingSeoMetadata = $this->seoMetadataRepository->findOneByPackageGuid($packageGuid);
0 ignored issues
show
Bug introduced by
The method findOneByPackageGuid() does not exist on SWP\Component\Storage\Re...ory\RepositoryInterface. Did you maybe mean findOneBy()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
109
        if (null === $existingSeoMetadata) {
110
            throw new NotFoundHttpException('SEO metadata not found!');
111
        }
112
113
        $form = $form = $this->get('form.factory')->createNamed('', SeoMetadataType::class, $existingSeoMetadata, ['method' => $request->getMethod()]);
114
115
        $form->handleRequest($request);
116
117
        if ($form->isSubmitted() && $form->isValid()) {
118
            $seoImageUploader->handleUpload($existingSeoMetadata);
119
120
            $this->seoMetadataObjectManager->flush();
121
122
            return new SingleResourceResponse($existingSeoMetadata, new ResponseContext(200));
123
        }
124
125
        return new SingleResourceResponse($form, new ResponseContext(400));
126
    }
127
}
128