ElementalPageExtension   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 30
c 1
b 1
f 0
dl 0
loc 57
rs 10
wmc 7

2 Methods

Rating   Name   Duplication   Size   Complexity  
A MetaTags() 0 13 3
A getElementsForSearch() 0 23 4
1
<?php
2
3
namespace DNADesign\Elemental\Extensions;
4
5
use Exception;
6
use DNADesign\Elemental\Models\ElementalArea;
7
use SilverStripe\Control\Controller;
8
use SilverStripe\View\Parsers\HTML4Value;
9
use SilverStripe\Core\Config\Config;
10
use SilverStripe\View\SSViewer;
11
12
/**
13
 * @method ElementalArea ElementalArea
14
 * @property ElementalArea ElementalArea
15
 */
16
class ElementalPageExtension extends ElementalAreasExtension
17
{
18
    private static $has_one = [
0 ignored issues
show
introduced by Will Rossiter
The private property $has_one is not used, and could be removed.
Loading history...
19
        'ElementalArea' => ElementalArea::class,
20
    ];
21
22
    private static $owns = [
0 ignored issues
show
introduced by Will Rossiter
The private property $owns is not used, and could be removed.
Loading history...
23
        'ElementalArea',
24
    ];
25
26
    private static $cascade_duplicates = [
0 ignored issues
show
introduced by Robbie Averill
The private property $cascade_duplicates is not used, and could be removed.
Loading history...
27
        'ElementalArea',
28
    ];
29
30
    /**
31
     * Returns the contents of each ElementalArea has_one's markup for use in Solr or Elastic search indexing
32
     *
33
     * @return string
34
     */
35
    public function getElementsForSearch()
36
    {
37
        $oldThemes = SSViewer::get_themes();
38
        SSViewer::set_themes(SSViewer::config()->get('themes'));
39
        try {
40
            $output = [];
41
            foreach ($this->owner->hasOne() as $key => $class) {
42
                if ($class !== ElementalArea::class) {
43
                    continue;
44
                }
45
                /** @var ElementalArea $area */
46
                $area = $this->owner->$key();
47
                if ($area) {
48
                    $output[] = strip_tags($area->forTemplate());
49
                }
50
            }
51
        } finally {
52
            // Reset theme if an exception occurs, if you don't have a
53
            // try / finally around code that might throw an Exception,
54
            // CMS layout can break on the response. (SilverStripe 4.1.1)
55
            SSViewer::set_themes($oldThemes);
56
        }
57
        return implode($output);
58
    }
59
60
    public function MetaTags(&$tags)
61
    {
62
        $controller = Controller::curr();
63
        $request = $controller->getRequest();
64
        if ($request->getVar('ElementalPreview') !== null) {
65
            $html = HTML4Value::create($tags);
66
            $xpath = "//meta[@name='x-page-id' or @name='x-cms-edit-link']";
67
            $removeTags = $html->query($xpath);
68
            $body = $html->getBody();
69
            foreach ($removeTags as $tag) {
70
                $body->removeChild($tag);
71
            }
72
            $tags = $html->getContent();
73
        }
74
    }
75
}
76