Completed
Push — master ( 678bac...955920 )
by Paweł
09:19 queued 10s
created

SeoMetadataController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
cc 1
nc 1
nop 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SWP\Bundle\CoreBundle\Controller;
6
7
use Nelmio\ApiDocBundle\Annotation\Model;
8
use Nelmio\ApiDocBundle\Annotation\Operation;
9
use Swagger\Annotations as SWG;
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 $eventDispatcher;
29
30
    public function __construct(
31
        FactoryInterface $seoMetadataFactory,
32
        RepositoryInterface $seoMetadataRepository,
33
        EventDispatcherInterface $eventDispatcher
34
    ) {
35
        $this->seoMetadataFactory = $seoMetadataFactory;
36
        $this->seoMetadataRepository = $seoMetadataRepository;
37
        $this->eventDispatcher = $eventDispatcher;
38
    }
39
40
    /**
41
     * @Operation(
42
     *     tags={"seo"},
43
     *     summary="Change SEO metadata entry",
44
     *     @SWG\Parameter(
45
     *         name="body",
46
     *         in="body",
47
     *         @SWG\Schema(
48
     *             ref=@Model(type=SeoMetadataType::class)
49
     *         )
50
     *     ),
51
     *     @SWG\Response(
52
     *         response="200",
53
     *         description="Returned on success.",
54
     *         @Model(type=\SWP\Bundle\ContentBundle\Model\ArticleSeoMetadata::class, groups={"api"})
55
     *     ),
56
     *     @SWG\Response(
57
     *         response="400",
58
     *         description="Returned when form have errors"
59
     *     )
60
     * )
61
     *
62
     * @Route("/api/{version}/packages/seo/{packageGuid}", options={"expose"=true}, defaults={"version"="v2"}, methods={"PUT"}, name="swp_api_core_seo_metadata_put")
63
     *
64
     * @param Request $request
65
     *
66
     * @return SingleResourceResponse
67
     */
68
    public function put(Request $request, string $packageGuid): SingleResourceResponse
69
    {
70
        $this->eventDispatcher->dispatch(MultiTenancyEvents::TENANTABLE_DISABLE);
71
72
        $seoMetadata = $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...
73
        if (null === $seoMetadata) {
74
            $seoMetadata = $this->seoMetadataFactory->create();
75
        }
76
77
        $form = $form = $this->get('form.factory')->createNamed('', SeoMetadataType::class, $seoMetadata, ['method' => $request->getMethod()]);
78
79
        $form->handleRequest($request);
80
81
        if ($form->isSubmitted() && $form->isValid()) {
82
            $seoMetadata->setPackageGuid($packageGuid);
83
            $this->seoMetadataRepository->add($seoMetadata);
84
85
            return new SingleResourceResponse($seoMetadata, new ResponseContext(200));
86
        }
87
88
        return new SingleResourceResponse($form, new ResponseContext(400));
89
    }
90
91
    /**
92
     * @Operation(
93
     *     tags={"seo"},
94
     *     summary="Gets SEO metadata entry",
95
     *     @SWG\Response(
96
     *         response="200",
97
     *         description="Returned on success.",
98
     *         @Model(type=\SWP\Bundle\ContentBundle\Model\ArticleSeoMetadata::class, groups={"api"})
99
     *     ),
100
     *     @SWG\Response(
101
     *         response="400",
102
     *         description="Returned when form have errors"
103
     *     )
104
     * )
105
     *
106
     * @Route("/api/{version}/packages/seo/{packageGuid}", options={"expose"=true}, defaults={"version"="v2"}, methods={"GET"}, name="swp_api_core_seo_metadata_get")
107
     *
108
     * @return SingleResourceResponse
109
     */
110
    public function getAction(string $packageGuid): SingleResourceResponse
111
    {
112
        $this->eventDispatcher->dispatch(MultiTenancyEvents::TENANTABLE_DISABLE);
113
114
        $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...
115
        if (null === $existingSeoMetadata) {
116
            throw new NotFoundHttpException('SEO metadata not found!');
117
        }
118
119
        return new SingleResourceResponse($existingSeoMetadata, new ResponseContext(200));
120
    }
121
}
122