Passed
Push — master ( 6a1b66...1520a2 )
by Przemysław eRIZ
04:33
created

PageController::setPageResourceResolver()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file was created by developers working at BitBag
5
 * Do you need more information about us and what we do? Visit our https://bitbag.io website!
6
 * We are hiring developers from all over the world. Join us and start your new, exciting adventure and become part of us: https://bitbag.io/career
7
*/
8
9
declare(strict_types=1);
10
11
namespace BitBag\SyliusCmsPlugin\Controller;
12
13
use BitBag\SyliusCmsPlugin\Entity\PageInterface;
14
use BitBag\SyliusCmsPlugin\Entity\PageTranslationInterface;
15
use BitBag\SyliusCmsPlugin\Resolver\PageResourceResolverInterface;
16
use FOS\RestBundle\View\View;
17
use Sylius\Bundle\ResourceBundle\Controller\ResourceController;
18
use Sylius\Component\Resource\ResourceActions;
19
use Symfony\Component\HttpFoundation\Request;
20
use Symfony\Component\HttpFoundation\Response;
21
use Webmozart\Assert\Assert;
22
23
final class PageController extends ResourceController
24
{
25
    use ResourceDataProcessingTrait;
26
27
    use MediaPageControllersCommonDependencyInjectionsTrait;
28
29
    /** @var PageResourceResolverInterface */
30
    private $pageResourceResolver;
31
32
    public const FILTER = 'sylius_admin_product_original';
33
34
    public function renderLinkAction(Request $request): Response
35
    {
36
        $configuration = $this->getRequestConfiguration($request);
37
38
        $this->isGrantedOr403($configuration, ResourceActions::SHOW);
39
40
        $code = $request->get('code');
41
42
        $page = $this->pageResourceResolver->findOrLog($code);
0 ignored issues
show
Bug introduced by
It seems like $code can also be of type null; however, parameter $code of BitBag\SyliusCmsPlugin\R...rInterface::findOrLog() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

42
        $page = $this->pageResourceResolver->findOrLog(/** @scrutinizer ignore-type */ $code);
Loading history...
43
44
        if (null === $page) {
45
            return new Response();
46
        }
47
48
        $this->eventDispatcher->dispatch(ResourceActions::SHOW, $configuration, $page);
49
50
        if ($configuration->isHtmlRequest()) {
51
            return $this->render($configuration->getTemplate(ResourceActions::SHOW . '.html'), [
52
                'configuration' => $configuration,
53
                'metadata' => $this->metadata,
54
                'resource' => $page,
55
                $this->metadata->getName() => $page,
56
            ]);
57
        }
58
59
        Assert::true(null !== $this->viewHandler);
60
61
        return $this->viewHandler->handle($configuration, View::create($page));
0 ignored issues
show
Bug introduced by
The method handle() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

61
        return $this->viewHandler->/** @scrutinizer ignore-call */ handle($configuration, View::create($page));

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
62
    }
63
64
    public function previewAction(Request $request): Response
65
    {
66
        $configuration = $this->getRequestConfiguration($request);
67
68
        $this->isGrantedOr403($configuration, ResourceActions::CREATE);
69
70
        /** @var PageInterface $page */
71
        $page = $this->getResourceInterface($request);
72
        $form = $this->getFormForResource($configuration, $page);
73
        $defaultLocale = $this->getParameter('locale');
74
75
        $form->handleRequest($request);
76
77
        $page->setFallbackLocale($request->get('_locale', $defaultLocale));
78
        $page->setCurrentLocale($request->get('_locale', $defaultLocale));
79
80
        $this->resolveImage($page);
81
82
        $this->formErrorsFlashHelper->addFlashErrors($form);
83
84
        if (!$configuration->isHtmlRequest()) {
85
            Assert::true(null !== $this->viewHandler);
86
            $this->viewHandler->handle($configuration, View::create($page));
87
        }
88
89
        return $this->render($configuration->getTemplate(ResourceActions::CREATE . '.html'), [
90
            'resource' => $page,
91
            'preview' => true,
92
            $this->metadata->getName() => $page,
93
        ]);
94
    }
95
96
    private function resolveImage(PageInterface $page): void
97
    {
98
        /** @var PageTranslationInterface $translation */
99
        $translation = $page->getTranslation();
100
101
        $image = $translation->getImage();
102
103
        if (null === $image || null === $image->getPath()) {
104
            return;
105
        }
106
        $this->setResourceMediaPath($image);
107
        /** @var PageTranslationInterface $pageTranslationInterface */
108
        $pageTranslationInterface = $page->getTranslation();
109
        $pageTranslationInterface->setImage($image);
110
    }
111
112
    public function setPageResourceResolver(PageResourceResolverInterface $pageResourceResolver): void
113
    {
114
        $this->pageResourceResolver = $pageResourceResolver;
115
    }
116
}
117