SitemapController   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

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

6 Methods

Rating   Name   Duplication   Size   Complexity  
A indexAction() 0 13 2
A overviewAction() 0 8 1
A getUriBuilder() 0 5 1
A getBasePages() 0 8 1
A prepareAndAssignNodes() 0 9 2
A removeDoublicates() 0 16 4
1
<?php
2
3
/**
4
 * Sitemap controller
5
 *
6
 * @author Tim Lochmüller
7
 */
8
9
namespace FRUIT\GoogleServices\Controller;
10
11
use FRUIT\GoogleServices\Domain\Model\Node;
12
use FRUIT\GoogleServices\Service\SitemapProvider;
13
use TYPO3\CMS\Core\Utility\GeneralUtility;
14
use TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder;
15
16
/**
17
 * Sitemap Controller
18
 */
19
class SitemapController extends AbstractController
20
{
21
22
    /**
23
     * Overview About Sitemaps
24
     */
25
    public function indexAction()
26
    {
27
        $pages = $this->getBasePages();
28
        $providers = GeneralUtility::trimExplode(',', $this->settings['provider'], true);
29
        $nodes = [];
30
31
        foreach ($providers as $provider) {
32
            $provider = SitemapProvider::getProvider($provider);
33
            $providerNodes = $provider->getRecords(intval($this->settings['startpoint']), $pages, $this);
34
            $nodes = array_merge($nodes, $providerNodes);
35
        }
36
        $this->prepareAndAssignNodes($nodes);
37
    }
38
39
    /**
40
     * Overview about Sitemaps
41
     */
42
    public function overviewAction()
43
    {
44
        $pages = $this->getBasePages();
45
        $provider = SitemapProvider::getProvider(SitemapProvider\Sitemap::class);
46
        $nodes = $provider->getRecords(intval($this->settings['startpoint']), $pages, $this);
47
48
        $this->prepareAndAssignNodes($nodes);
49
    }
50
51
    /**
52
     * Return a aboslute uri Builder (for Providers)
53
     *
54
     * @return UriBuilder
55
     */
56
    public function getUriBuilder()
57
    {
58
        return $this->uriBuilder->reset()
59
            ->setCreateAbsoluteUri(true);
60
    }
61
62
    /**
63
     * Get the base pages
64
     *
65
     * @return array
66
     */
67
    protected function getBasePages()
68
    {
69
        $startPage = intval($this->settings['startpoint']);
70
        $depth = intval($this->settings['depth']);
71
        $pages = $this->configurationManager->getContentObject()
72
            ->getTreeList($startPage, $depth, 0, true);
73
        return GeneralUtility::trimExplode(',', $startPage . ',' . $pages, true);
74
    }
75
76
    /**
77
     * Prepare the Nodes for the Sitemap
78
     *
79
     * @param array $nodes
80
     */
81
    protected function prepareAndAssignNodes($nodes)
82
    {
83
        if (!is_array($nodes)) {
84
            $nodes = [$nodes];
85
        }
86
87
        $nodes = $this->removeDoublicates($nodes);
88
        $this->view->assign('nodes', $nodes);
89
    }
90
91
    /**
92
     * Remove doublicates
93
     *
94
     * @param array $nodes
95
     *
96
     * @return array
97
     */
98
    protected function removeDoublicates(array $nodes)
99
    {
100
        $double = [];
101
        foreach ($nodes as $key => $value) {
102
            if ($value instanceof Node) {
103
                if (in_array($value->getLoc(), $double)) {
104
                    unset($nodes[$key]);
105
                    continue;
106
                }
107
                $double[] = $value->getLoc();
108
            } else {
109
                unset($nodes[$key]);
110
            }
111
        }
112
        return $nodes;
113
    }
114
}
115