Widget::checkIdTemplateFileExist()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.7
c 0
b 0
f 0
cc 2
nc 2
nop 3
1
<?php
2
declare(strict_types=1);
3
4
namespace Selami\View\Twig\Functions;
5
6
use Selami\View\FunctionInterface;
7
use Selami\Stdlib\CaseConverter;
8
use BadMethodCallException;
9
use InvalidArgumentException;
10
11
use Twig\Environment;
12
13
class Widget implements FunctionInterface
14
{
15
    private $twig;
16
    private $config;
17
    private $templateFile;
18
    private $widgetData;
19
20
    public function __construct(
21
        Environment $twig,
22
        array $config,
23
        string $widgetNameStr,
24
        string $widgetActionStr,
25
        array $args
26
    ) {
27
        $this->twig = $twig;
28
        $this->config = $config;
29
        $widgetAction = CaseConverter::toPascalCase($widgetActionStr);
30
        $widgetName =  CaseConverter::toPascalCase($widgetNameStr);
31
        $widget = '\\' . $this->config['app_namespace'] . '\\Widget\\' . $widgetName;
32
        $this->checkClassIfExists($widget, $widgetName, $widgetAction);
33
        $widgetInstance = new $widget($args);
34
        $templateFileBasename = $args['template'] ?? CaseConverter::toSnakeCase($widgetActionStr) . '.twig';
35
        $this->checkIdTemplateFileExist($templateFileBasename, $widgetNameStr, $widgetActionStr);
36
        $this->templateFile =  '_widgets/'
37
            . CaseConverter::toSnakeCase($widgetNameStr) . '/' . $templateFileBasename;
38
        $this->widgetData = $widgetInstance->{$widgetAction}();
39
    }
40
41
    private function checkClassIfExists($widget, $widgetName, $widgetAction)
42
    {
43
        if (!class_exists($widget)) {
44
            $message = 'Widget ' . $widgetName . '_' . $widgetAction . ' has not class name as ' . $widget;
45
            throw new BadMethodCallException($message);
46
        }
47
        if (!method_exists($widget, $widgetAction)) {
48
            $message = 'Widget ' . $widget . ' has not method name as ' . $widgetAction;
49
            throw new BadMethodCallException($message);
50
        }
51
    }
52
53
    private function checkIdTemplateFileExist(
54
        string $templateFileBasename,
55
        string $widgetNameStr,
56
        string $widgetActionStr
57
    ) {
58
        $templateFullPath = $this->config['templates_path'] . '/_widgets/'
59
            . CaseConverter::toSnakeCase($widgetNameStr) . '/' . $templateFileBasename;
60
        if (!file_exists($templateFullPath)) {
61
            $message = sprintf(
62
                '%s  template file not found! %s needs a main template file at: %s',
63
                $templateFileBasename,
64
                $widgetNameStr . '_' . $widgetActionStr,
65
                $templateFullPath
66
            );
67
            throw new InvalidArgumentException($message);
68
        }
69
    }
70
71
    public function run() : string
72
    {
73
        return $this->twig->render($this->templateFile, $this->widgetData);
74
    }
75
}
76