LanguagePages::getRecords()   C
last analyzed

Complexity

Conditions 14
Paths 22

Size

Total Lines 71

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 71
rs 5.646
c 0
b 0
f 0
cc 14
nc 22
nop 3

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * LanguagePages
5
 */
6
7
namespace FRUIT\GoogleServices\Service\SitemapProvider;
8
9
use FRUIT\GoogleServices\Controller\SitemapController;
10
use FRUIT\GoogleServices\Domain\Model\Node;
11
use FRUIT\GoogleServices\Service\SitemapDataService;
12
use TYPO3\CMS\Backend\Utility\BackendUtility;
13
use TYPO3\CMS\Core\Database\DatabaseConnection;
14
use TYPO3\CMS\Core\Utility\GeneralUtility;
15
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
16
17
/**
18
 * LanguagePages
19
 */
20
class LanguagePages extends Pages
21
{
22
23
    /**
24
     * Current language UID
25
     *
26
     * @var int
27
     */
28
    protected $currentLanguageUid;
29
30
    /**
31
     * Database
32
     *
33
     * @var DatabaseConnection $database
34
     */
35
    protected $database;
36
37
    /**
38
     * Content object
39
     *
40
     * @var \TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer $cObject
41
     */
42
    protected $cObject;
43
44
    /**
45
     * Build up the object
46
     */
47
    public function __construct()
48
    {
49
        $this->currentLanguageUid = intval($GLOBALS['TSFE']->sys_language_uid);
50
        $this->database = $GLOBALS['TYPO3_DB'];
51
        $this->cObject = GeneralUtility::makeInstance(ContentObjectRenderer::class);
52
    }
53
54
    /**
55
     * Get the record
56
     *
57
     * @param int $startPage
58
     * @param array $basePages
59
     * @param SitemapController $obj
60
     *
61
     * @return array
62
     */
63
    public function getRecords($startPage, $basePages, SitemapController $obj): array
64
    {
65
        $nodes = [];
66
        foreach ($basePages as $uid) {
67
            if ($this->currentLanguageUid) {
68
                $fields = $this->cObject->enableFields('pages_language_overlay');
69
                $overlay = $this->database->exec_SELECTgetSingleRow(
70
                    'uid',
71
                    'pages_language_overlay',
72
                    ' pid=' . intval($uid) . ' AND sys_language_uid=' . $this->currentLanguageUid . $fields
73
                );
74
                if (!is_array($overlay)) {
75
                    continue;
76
                }
77
            }
78
79
            // Build URL
80
            $url = $obj->getUriBuilder()
81
                ->setTargetPageUid($uid)
82
                ->build();
83
84
            // can't generate a valid url
85
            if (!strlen($url)) {
86
                continue;
87
            }
88
89
            // Get Record
90
            $record = BackendUtility::getRecord('pages', $uid);
91
92
            // exclude Doctypes
93
            if (in_array($record['doktype'], [4])) {
94
                continue;
95
            }
96
97
            // Check FE Access
98
            if ($record['fe_group'] != 0 || $record['no_search'] != 0) {
99
                continue;
100
            }
101
102
            if ($record['exclude_sitemap']) {
103
                continue;
104
            }
105
106
            $rootLineList = $GLOBALS['TSFE']->sys_page->getRootLine($record['uid']);
107
            $addToNode = true;
108
            foreach ($rootLineList as $rootPage) {
109
                if ($rootPage['extendToSubpages'] == 1 && ($rootPage['fe_group'] != 0 || $record['no_search'] != 0)) {
110
                    $addToNode = false;
111
                    break;
112
                }
113
            }
114
            if ($addToNode === false) {
115
                continue;
116
            }
117
118
            // Build Node
119
            $node = new Node();
120
            $node->setLoc($url);
121
            $node->setPriority($this->getPriority($startPage, $record));
122
            $node->setChangefreq(SitemapDataService::mapTimeout2Period($record['cache_timeout']));
123
            $node->setLastmod($this->getModifiedDate($record));
124
125
            #$geo = new Geo();
126
            #$geo->setFormat('kml');
127
            #$node->setGeo($geo);
128
129
            $nodes[] = $node;
130
        }
131
132
        return $nodes;
133
    }
134
}
135