Completed
Push — master ( 4c5b82...ece630 )
by Jeroen
19s
created

SearchViewRenderer   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 43.75%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 7
dl 0
loc 67
ccs 14
cts 32
cp 0.4375
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A renderDefaultSearchView() 0 11 1
A renderCustomSearchView() 0 17 3
A removeHtml() 0 20 3
1
<?php
2
3
namespace Kunstmaan\NodeSearchBundle\Services;
4
5
use Kunstmaan\NodeBundle\Entity\NodeTranslation;
6
use Kunstmaan\NodeBundle\Entity\PageInterface;
7
use Kunstmaan\NodeBundle\Helper\RenderContext;
8
use Kunstmaan\NodeSearchBundle\Helper\IndexablePagePartsService;
9
use Kunstmaan\NodeSearchBundle\Helper\SearchViewTemplateInterface;
10
use Kunstmaan\PagePartBundle\Helper\HasPagePartsInterface;
11
use Symfony\Component\DependencyInjection\ContainerInterface;
12
use Symfony\Component\DomCrawler\Crawler;
13
use Symfony\Component\HttpFoundation\RequestStack;
14
use Twig\Environment;
15
16
class SearchViewRenderer
17
{
18
    /** @var Environment */
19
    private $twig;
20
    /** @var IndexablePagePartsService */
21
    private $indexablePagePartsService;
22
    /** @var RequestStack */
23
    private $requestStack;
24
25 4
    public function __construct(Environment $twig, IndexablePagePartsService $indexablePagePartsService, RequestStack $requestStack)
26
    {
27 4
        $this->twig = $twig;
28 4
        $this->indexablePagePartsService = $indexablePagePartsService;
29 4
        $this->requestStack = $requestStack;
30 4
    }
31
32
    public function renderDefaultSearchView(NodeTranslation $nodeTranslation, HasPagePartsInterface $page, string $defaultView = '@KunstmaanNodeSearch/PagePart/view.html.twig')
33
    {
34
        $html = $this->twig->render($defaultView, [
35
            'locale' => $nodeTranslation->getLang(),
36
            'page' => $page,
37
            'pageparts' => $this->indexablePagePartsService->getIndexablePageParts($page),
38
            'indexMode' => true,
39
        ]);
40
41
        return $this->removeHtml($html);
42
    }
43
44
    public function renderCustomSearchView(NodeTranslation $nodeTranslation, SearchViewTemplateInterface $page, ContainerInterface $container = null)
45
    {
46
        $renderContext = new RenderContext([
47
            'locale' => $nodeTranslation->getLang(),
48
            'page' => $page,
49
            'indexMode' => true,
50
            'nodetranslation' => $nodeTranslation,
51
        ]);
52
53
        if ($page instanceof PageInterface && null !== $container) {
54
            $page->service($container, $this->requestStack->getCurrentRequest(), $renderContext);
55
        }
56
57
        $html = $this->twig->render($page->getSearchView(), $renderContext->getArrayCopy());
58
59
        return $this->removeHtml($html);
60
    }
61
62 4
    public function removeHtml(string $text): string
63
    {
64 4
        if (empty(trim($text))) {
65
            return '';
66
        }
67
68 4
        $crawler = new Crawler();
69 4
        $crawler->addHtmlContent($text);
70
        $crawler->filter('style, script')->each(function (Crawler $crawler) {
71
            foreach ($crawler as $node) {
72
                $node->parentNode->removeChild($node);
73
            }
74 4
        });
75 4
        $text = $crawler->html();
76
77 4
        $result = strip_tags($text);
78 4
        $result = trim(html_entity_decode($result, ENT_QUOTES));
79
80 4
        return $result;
81
    }
82
}
83