Test Setup Failed
Pull Request — master (#4522)
by Craig
08:26 queued 03:47
created

BlocksRuntime::showBlock()   B

Complexity

Conditions 7
Paths 5

Size

Total Lines 29
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 19
c 1
b 0
f 0
nc 5
nop 2
dl 0
loc 29
rs 8.8333
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Zikula package.
7
 *
8
 * Copyright Zikula - https://ziku.la/
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Zikula\BlocksModule\Twig\Runtime;
15
16
use RuntimeException;
17
use Twig\Extension\RuntimeExtensionInterface;
18
use Zikula\BlocksModule\Api\ApiInterface\BlockApiInterface;
19
use Zikula\BlocksModule\Api\ApiInterface\BlockFilterApiInterface;
20
use Zikula\BlocksModule\Entity\BlockEntity;
21
use Zikula\Bundle\CoreBundle\HttpKernel\ZikulaHttpKernelInterface;
22
use Zikula\ThemeModule\Engine\Engine;
23
24
class BlocksRuntime implements RuntimeExtensionInterface
25
{
26
    /**
27
     * @var BlockApiInterface
28
     */
29
    private $blockApi;
30
31
    /**
32
     * @var BlockFilterApiInterface
33
     */
34
    private $blockFilter;
35
36
    /**
37
     * @var Engine
38
     */
39
    private $themeEngine;
40
41
    /**
42
     * @var ZikulaHttpKernelInterface
43
     */
44
    private $kernel;
45
46
    public function __construct(
47
        BlockApiInterface $blockApi,
48
        BlockFilterApiInterface $blockFilterApi,
49
        Engine $themeEngine,
50
        ZikulaHttpKernelInterface $kernel
51
    ) {
52
        $this->blockApi = $blockApi;
53
        $this->blockFilter = $blockFilterApi;
54
        $this->themeEngine = $themeEngine;
55
        $this->kernel = $kernel;
56
    }
57
58
    /**
59
     * Show all the blocks in a position by name.
60
     *
61
     * @return array|string
62
     */
63
    public function showBlockPosition(string $positionName, bool $implode = true)
64
    {
65
        $instance = $this->kernel->getModule('ZikulaBlocksModule');
66
        if (!isset($instance)) {
67
            return 'Blocks not currently available.';
68
        }
69
        $blocks = $this->blockApi->getBlocksByPosition($positionName);
70
        foreach ($blocks as $key => $block) {
71
            $blocks[$key] = $this->showBlock($block, $positionName);
72
        }
73
74
        return $implode ? implode("\n", $blocks) : $blocks;
75
    }
76
77
    /**
78
     * Display one block.
79
     */
80
    public function showBlock(BlockEntity $block, string $positionName = ''): string
81
    {
82
        $blocksModuleInstance = $this->kernel->getModule('ZikulaBlocksModule');
83
        if (!isset($blocksModuleInstance)) {
84
            return 'Blocks not currently available.';
85
        }
86
        // Check if providing module not available, if block is inactive, if block filter prevents display.
87
        $bundleName = $block->getModule()->getName();
88
        $moduleInstance = $this->kernel->getModule($bundleName);
89
        if (!isset($moduleInstance) || !$block->getActive() || !$this->blockFilter->isDisplayable($block)) {
90
            return '';
91
        }
92
93
        try {
94
            $blockInstance = $this->blockApi->createInstanceFromBKey($block->getBkey());
95
        } catch (RuntimeException $exception) {
96
            return '';
97
        }
98
        $blockProperties = $block->getProperties();
99
        $blockProperties['bid'] = $block->getBid();
100
        $blockProperties['title'] = $block->getTitle();
101
        $blockProperties['position'] = $positionName;
102
        $content = $blockInstance->display($blockProperties);
103
        if (isset($moduleInstance)) {
104
            // add module stylesheet to page
105
            $moduleInstance->addStylesheet();
106
        }
107
108
        return $this->themeEngine->wrapBlockContentInTheme($content, $block->getTitle(), $block->getBlocktype(), $block->getBid(), $positionName);
109
    }
110
111
    public function isPositionAvailable(string $name): bool
112
    {
113
        return $this->themeEngine->positionIsAvailableInTheme($name);
114
    }
115
}
116