AbstractViewableAction::setDefaultOptions()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 7
rs 9.4285
cc 1
eloc 4
nc 1
nop 1
1
<?php
2
3
namespace Pim\Bundle\CustomEntityBundle\Action;
4
5
use Pim\Bundle\CustomEntityBundle\Event\ActionEventManager;
6
use Pim\Bundle\CustomEntityBundle\Manager\Registry as ManagerRegistry;
7
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
8
use Symfony\Component\HttpFoundation\Request;
9
use Symfony\Component\HttpFoundation\Response;
10
use Symfony\Component\OptionsResolver\OptionsResolver;
11
use Symfony\Component\Routing\RouterInterface;
12
use Symfony\Component\Translation\TranslatorInterface;
13
14
/**
15
 * @author    Antoine Guigan <[email protected]>
16
 * @copyright 2013 Akeneo SAS (http://www.akeneo.com)
17
 * @license   http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
18
 */
19
abstract class AbstractViewableAction extends AbstractAction
20
{
21
    /**
22
     * @var EngineInterface
23
     */
24
    protected $templating;
25
26
    /**
27
     * @param ActionFactory       $actionFactory
28
     * @param ActionEventManager  $eventManager
29
     * @param ManagerRegistry     $managerRegistry
30
     * @param RouterInterface     $router
31
     * @param TranslatorInterface $translator
32
     * @param EngineInterface     $templating
33
     */
34
    public function __construct(
35
        ActionFactory $actionFactory,
36
        ActionEventManager $eventManager,
37
        ManagerRegistry $managerRegistry,
38
        RouterInterface $router,
39
        TranslatorInterface $translator,
40
        EngineInterface $templating
41
    ) {
42
        parent::__construct($actionFactory, $eventManager, $managerRegistry, $router, $translator);
43
44
        $this->templating = $templating;
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50
    public function doExecute(Request $request)
51
    {
52
        return $this->renderResponse(
53
            [
54
                'object' => $this->findEntity($request)
55
            ]
56
        );
57
    }
58
59
    /**
60
     * @param array $templateVars
61
     *
62
     * @return Response
63
     */
64
    public function renderResponse(array $templateVars = [])
65
    {
66
        list($template, $templateVars) = $this->eventManager->dispatchPreRenderEvent(
67
            $this,
68
            $this->options['template'],
69
            $templateVars + $this->getDefaultTemplateVars()
70
        );
71
72
        return $this->templating->renderResponse($template, $templateVars);
73
    }
74
75
    /**
76
     * Returns the default template vars
77
     *
78
     * @return array
79
     */
80
    protected function getDefaultTemplateVars()
81
    {
82
        $vars = [
83
            'customEntityName' => $this->configuration->getName(),
84
            'baseTemplate'     => $this->options['base_template']
85
        ];
86
87
        if ($this->configuration->hasAction('index')) {
88
            $vars['indexUrl'] = $this->getActionUrl('index');
89
        }
90
91
        return $vars;
92
    }
93
94
    /**
95
     * {@inheritdoc}
96
     */
97
    protected function setDefaultOptions(OptionsResolver $resolver)
98
    {
99
        parent::setDefaultOptions($resolver);
100
101
        $resolver->setRequired(['template']);
102
        $resolver->setDefaults(['base_template' => 'PimEnrichBundle::layout.html.twig']);
103
    }
104
}
105