Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
89.47% covered (warning)
89.47%
17 / 19
80.00% covered (warning)
80.00%
4 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
TemplateBased
89.47% covered (warning)
89.47%
17 / 19
80.00% covered (warning)
80.00%
4 / 5
8.07
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setConfig
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getContext
83.33% covered (warning)
83.33%
10 / 12
0.00% covered (danger)
0.00%
0 / 1
4.07
 getContextForMd
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 getContextForPhtml
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3/**
4 * Class TemplateBased
5 *
6 * PHP version 8
7 *
8 * Copyright (C) Moravian Library 2020.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2,
12 * as published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
22 *
23 * @category VuFind
24 * @package  ContentBlock
25 * @author   Josef Moravec <moravec@mzk.cz>
26 * @license  https://opensource.org/licenses/gpl-2.0.php GNU General Public License
27 * @link     https://vufind.org/wiki/development Wiki
28 */
29
30namespace VuFind\ContentBlock;
31
32use function is_callable;
33
34/**
35 * Class TemplateBased
36 *
37 * @category VuFind
38 * @package  VuFind\ContentBlock
39 * @author   Josef Moravec <moravec@mzk.cz>
40 * @license  https://opensource.org/licenses/gpl-2.0.php GNU General Public License
41 * @link     https://vufind.org/wiki/development Wiki
42 */
43class TemplateBased implements ContentBlockInterface
44{
45    /**
46     * Name of template for rendering
47     *
48     * @var string
49     */
50    protected $templateName;
51
52    /**
53     * Page content
54     *
55     * @var \VuFind\Content\PageLocator
56     */
57    protected $pageLocator;
58
59    /**
60     * TemplateBased constructor.
61     *
62     * @param \VuFind\Content\PageLocator $pageLocator Content page locator service
63     */
64    public function __construct(\VuFind\Content\PageLocator $pageLocator)
65    {
66        $this->pageLocator = $pageLocator;
67    }
68
69    /**
70     * Store the configuration of the content block.
71     *
72     * @param string $settings Settings from searches.ini.
73     *
74     * @return void
75     */
76    public function setConfig($settings)
77    {
78        $this->templateName = $settings;
79    }
80
81    /**
82     * Return context variables used for rendering the block's template.
83     *
84     * @param string $pathPrefix Subdirectory where the template should be located
85     * @param string $page       Template name (defaults to config value if unset)
86     * @param string $pattern    Filesystem pattern (see PageLocator)
87     *
88     * @return array
89     */
90    public function getContext(
91        $pathPrefix = 'templates/ContentBlock/TemplateBased/',
92        $page = null,
93        $pattern = null
94    ) {
95        $data = $this->pageLocator->determineTemplateAndRenderer(
96            $pathPrefix,
97            $page ?? $this->templateName,
98            $pattern
99        );
100
101        $method = isset($data) ? 'getContextFor' . ucwords($data['renderer'])
102            : false;
103
104        $context = $method && is_callable([$this, $method])
105            ? $this->$method($data['relativePath'], $data['path'])
106            : [];
107        $context['pageLocatorDetails'] = $data;
108        return $context;
109    }
110
111    /**
112     * Return context array for markdown
113     *
114     * @param string $relativePath Relative path to template
115     * @param string $path         Full path of template file
116     *
117     * @return array
118     *
119     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
120     */
121    protected function getContextForMd(string $relativePath, string $path): array
122    {
123        return [
124            'template' => 'ContentBlock/TemplateBased/markdown',
125            'data' => file_get_contents($path),
126        ];
127    }
128
129    /**
130     * Return context array of phtml
131     *
132     * @param string $relativePath Relative path to template
133     * @param string $path         Full path of template file
134     *
135     * @return array
136     *
137     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
138     */
139    protected function getContextForPhtml(string $relativePath, string $path): array
140    {
141        return ['template' => $relativePath];
142    }
143}