TemplatingHelper   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 5

Importance

Changes 0
Metric Value
wmc 11
lcom 2
cbo 5
dl 0
loc 82
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A render() 0 4 1
A renderTemplateString() 0 6 1
A renderControllerResponse() 0 6 1
A resolveControllerTemplate() 0 8 1
A getControllerLogicalName() 0 7 1
A getBundleName() 0 6 1
A getBundleForClass() 0 14 4
1
<?php
2
/*
3
 * WellCommerce Open-Source E-Commerce Platform
4
 * 
5
 * This file is part of the WellCommerce package.
6
 *
7
 * (c) Adam Piotrowski <[email protected]>
8
 * 
9
 * For the full copyright and license information,
10
 * please view the LICENSE file that was distributed with this source code.
11
 */
12
13
namespace WellCommerce\Bundle\CoreBundle\Helper\Templating;
14
15
use ReflectionClass;
16
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
17
use Symfony\Component\HttpFoundation\Response;
18
use Symfony\Component\HttpKernel\Bundle\BundleInterface;
19
use Symfony\Component\HttpKernel\KernelInterface;
20
use Twig_Environment as Environment;
21
use WellCommerce\Bundle\CoreBundle\Controller\ControllerInterface;
22
23
/**
24
 * Class TemplatingHelper
25
 *
26
 * @author  Adam Piotrowski <[email protected]>
27
 */
28
final class TemplatingHelper implements TemplatingHelperInterface
29
{
30
    /**
31
     * @var EngineInterface
32
     */
33
    protected $engine;
34
    
35
    /**
36
     * @var Environment
37
     */
38
    protected $environment;
39
    
40
    /**
41
     * @var KernelInterface
42
     */
43
    protected $kernel;
44
    
45
    public function __construct(EngineInterface $engine, KernelInterface $kernel, Environment $environment)
46
    {
47
        $this->engine      = $engine;
48
        $this->environment = $environment;
49
        $this->kernel      = $kernel;
50
    }
51
    
52
    public function render(string $name, array $parameters = []): string
53
    {
54
        return $this->engine->render($name, $parameters);
55
    }
56
    
57
    public function renderTemplateString(string $template, array $parameters = []): string
58
    {
59
        $template = $this->environment->createTemplate($template);
60
        
61
        return $template->render($parameters);
62
    }
63
    
64
    public function renderControllerResponse(ControllerInterface $controller, string $templateName, array $parameters = []): Response
65
    {
66
        $template = $this->resolveControllerTemplate($controller, $templateName);
67
        
68
        return $this->engine->renderResponse($template, $parameters);
69
    }
70
    
71
    public function resolveControllerTemplate(ControllerInterface $controller, string $templateName): string
72
    {
73
        $reflectionClass = new ReflectionClass($controller);
74
        $controllerName  = $this->getControllerLogicalName($reflectionClass);
75
        $bundleName      = $this->getBundleName($reflectionClass);
76
        
77
        return sprintf('%s:%s:%s.html.twig', $bundleName, $controllerName, $templateName);
78
    }
79
    
80
    protected function getControllerLogicalName(ReflectionClass $reflectionClass): string
81
    {
82
        $className = $reflectionClass->getName();
0 ignored issues
show
Bug introduced by
Consider using $reflectionClass->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
83
        preg_match('/Controller\\\(.+)Controller$/', $className, $matchController);
84
        
85
        return $matchController[1];
86
    }
87
    
88
    protected function getBundleName(ReflectionClass $reflectionClass): string
89
    {
90
        $currentBundle = $this->getBundleForClass($reflectionClass);
91
        
92
        return $currentBundle->getName();
93
    }
94
    
95
    protected function getBundleForClass(ReflectionClass $reflectionClass): BundleInterface
96
    {
97
        $bundles = $this->kernel->getBundles();
98
        
99
        do {
100
            $namespace = $reflectionClass->getNamespaceName();
101
            foreach ($bundles as $bundle) {
102
                if (0 === strpos($namespace, $bundle->getNamespace())) {
103
                    return $bundle;
104
                }
105
            }
106
            $reflectionClass = $reflectionClass->getParentClass();
107
        } while ($reflectionClass);
108
    }
109
}
110