Completed
Push — master ( d67f41...79042b )
by Adam
07:27
created

LayoutBoxExtension::isVisible()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.2
c 0
b 0
f 0
cc 4
eloc 10
nc 4
nop 1
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\AppBundle\Twig;
14
15
use WellCommerce\Bundle\AppBundle\Entity\Client;
16
use WellCommerce\Bundle\AppBundle\Entity\LayoutBox;
17
use WellCommerce\Bundle\CoreBundle\Doctrine\Repository\RepositoryInterface;
18
use WellCommerce\Bundle\CoreBundle\Helper\Security\SecurityHelperInterface;
19
use WellCommerce\Component\Layout\Renderer\LayoutBoxRendererInterface;
20
21
/**
22
 * Class LayoutBoxExtension
23
 *
24
 * @author  Adam Piotrowski <[email protected]>
25
 */
26
final class LayoutBoxExtension extends \Twig_Extension
27
{
28
    /**
29
     * @var LayoutBoxRendererInterface
30
     */
31
    private $renderer;
32
    
33
    /**
34
     * @var RepositoryInterface
35
     */
36
    private $repository;
37
    
38
    /**
39
     * @var SecurityHelperInterface
40
     */
41
    private $security;
42
    
43
    public function __construct(LayoutBoxRendererInterface $renderer, RepositoryInterface $repository, SecurityHelperInterface $security)
44
    {
45
        $this->renderer   = $renderer;
46
        $this->repository = $repository;
47
        $this->security   = $security;
48
    }
49
    
50
    public function getFunctions()
51
    {
52
        return [
53
            new \Twig_SimpleFunction('layout_box', [$this, 'getLayoutBoxContent'], ['is_safe' => ['html']]),
54
        ];
55
    }
56
    
57
    public function getLayoutBoxContent($identifier, $params = []): string
58
    {
59
        $layoutBox = $this->repository->findOneBy(['identifier' => $identifier]);
60
        
61
        if ($layoutBox instanceof LayoutBox) {
62
            if ($this->isVisible($layoutBox)) {
63
                return $this->renderer->render($layoutBox, $params);
64
            }
65
        }
66
        
67
        return '';
68
    }
69
    
70
    public function getName()
71
    {
72
        return 'layout_box';
73
    }
74
    
75
    private function isVisible(LayoutBox $layoutBox): bool
76
    {
77
        $clientGroups = $layoutBox->getClientGroups();
78
        $client       = $this->security->getCurrentClient();
79
        
80
        if ($clientGroups->count()) {
81
            if ($client instanceof Client) {
82
                $clientGroup = $client->getClientGroup();
83
                if ($clientGroups->contains($clientGroup)) {
84
                    return true;
85
                }
86
            }
87
    
88
            return false;
89
        }
90
        
91
        return true;
92
    }
93
}
94