PiwikTrackerBlockService::execute()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 1
eloc 6
nc 1
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * (c) Christian Gripp <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Core23\PiwikBundle\Block\Service;
13
14
use Sonata\AdminBundle\Form\FormMapper;
15
use Sonata\BlockBundle\Block\BlockContextInterface;
16
use Sonata\BlockBundle\Block\Service\AbstractAdminBlockService;
17
use Sonata\BlockBundle\Model\BlockInterface;
18
use Sonata\CoreBundle\Form\Type\ImmutableArrayType;
19
use Sonata\CoreBundle\Model\Metadata;
20
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
21
use Symfony\Component\Form\Extension\Core\Type\NumberType;
22
use Symfony\Component\Form\Extension\Core\Type\TextType;
23
use Symfony\Component\HttpFoundation\Response;
24
use Symfony\Component\OptionsResolver\OptionsResolver;
25
26
final class PiwikTrackerBlockService extends AbstractAdminBlockService
27
{
28
    /**
29
     * {@inheritdoc}
30
     */
31
    public function execute(BlockContextInterface $blockContext, Response $response = null)
32
    {
33
        return $this->renderResponse($blockContext->getTemplate(), [
34
            'context'  => $blockContext,
35
            'settings' => $blockContext->getSettings(),
36
            'block'    => $blockContext->getBlock(),
37
        ], $response);
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43
    public function buildEditForm(FormMapper $formMapper, BlockInterface $block): void
44
    {
45
        $formMapper->add('settings', ImmutableArrayType::class, [
46
            'keys' => [
47
                ['host', TextType::class, [
48
                    'required' => false,
49
                    'label'    => 'form.label_host',
50
                ]],
51
                ['site', NumberType::class, [
52
                    'label' => 'form.label_site',
53
                ]],
54
                ['domaintitle', CheckboxType::class, [
55
                    'label'    => 'form.label_domaintitle',
56
                    'required' => false,
57
                ]],
58
                ['nocookies', CheckboxType::class, [
59
                    'label'    => 'form.label_nocookies',
60
                    'required' => false,
61
                ]],
62
                ['donottrack', CheckboxType::class, [
63
                    'label'    => 'form.label_donottrack',
64
                    'required' => false,
65
                ]],
66
            ],
67
            'translation_domain' => 'Core23PiwikBundle',
68
        ]);
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74
    public function configureSettings(OptionsResolver $resolver): void
75
    {
76
        $resolver->setDefaults([
77
            'host'        => null,
78
            'site'        => null,
79
            'domaintitle' => false,
80
            'donottrack'  => false,
81
            'nocookies'   => false,
82
            'template'    => '@Core23Piwik/Block/block_piwik_tracker.html.twig',
83
        ]);
84
85
        $resolver->setRequired(['site', 'host']);
86
    }
87
88
    /**
89
     * {@inheritdoc}
90
     */
91
    public function getBlockMetadata($code = null)
92
    {
93
        return new Metadata($this->getName(), $code ?? $this->getName(), false, 'Core23PiwikBundle', [
94
            'class' => 'fa fa-code',
95
        ]);
96
    }
97
}
98