FalImages::getRecords()   B
last analyzed

Complexity

Conditions 6
Paths 8

Size

Total Lines 54

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 54
rs 8.3814
c 0
b 0
f 0
cc 6
nc 8
nop 3

How to fix   Long Method   

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
 * SitemapProvider Images
4
 *
5
 * @author     Ercüment Topal <[email protected]>
6
 */
7
8
namespace FRUIT\GoogleServices\Service\SitemapProvider;
9
10
use FRUIT\GoogleServices\Controller\SitemapController;
11
use FRUIT\GoogleServices\Domain\Model\Node;
12
use FRUIT\GoogleServices\Domain\Model\Node\ImageNode;
13
use FRUIT\GoogleServices\Service\SitemapDataService;
14
use TYPO3\CMS\Backend\Utility\BackendUtility;
15
use TYPO3\CMS\Core\Utility\GeneralUtility;
16
17
/**
18
 * Class Images
19
 *
20
 * @author     Ercüment Topal <[email protected]>
21
 */
22
class FalImages extends Pages
23
{
24
25
    /**
26
     * Resource factory to build objects
27
     *
28
     * @var \TYPO3\CMS\Core\Resource\ResourceFactory
29
     * @inject
30
     */
31
    protected $resourceFactory;
32
33
    /**
34
     * Page repository for manipulate the SQL queries
35
     *
36
     * @var \TYPO3\CMS\Frontend\Page\PageRepository
37
     * @inject
38
     */
39
    protected $pageRepository;
40
41
    /**
42
     * Get the records
43
     *
44
     * @param int               $startPage
45
     * @param array             $basePages
46
     * @param SitemapController $obj
47
     *
48
     * @return array
49
     */
50
    public function getRecords($startPage, $basePages, SitemapController $obj): array
51
    {
52
        $nodes = [];
53
        foreach ($basePages as $uid) {
54
            $images = $this->getImagesByPages([$uid]);
55
            if (!sizeof($images)) {
56
                continue;
57
            }
58
            $imageNodes = [];
59
            foreach ($images as $imageReference) {
60
                /** @var $imageReference \TYPO3\CMS\Core\Resource\FileReference */
61
                $url = GeneralUtility::getIndpEnv('TYPO3_REQUEST_HOST') . '/' . $imageReference->getOriginalFile()
62
                        ->getPublicUrl();
63
64
                // Build Node
65
                $nodeImage = new ImageNode();
66
                $nodeImage->setLoc($url);
67
                $nodeImage->setTitle($imageReference->getTitle());
68
                $nodeImage->setCaption($imageReference->getDescription());
69
                $imageNodes[] = $nodeImage;
70
            }
71
72
            // Build URL
73
            $url = $obj->getUriBuilder()
74
                ->setTargetPageUid($uid)
75
                ->build();
76
77
            // can't generate a valid url
78
            if (!strlen($url)) {
79
                continue;
80
            }
81
82
            // Get Record
83
            $record = BackendUtility::getRecord('pages', $uid);
84
85
            // exclude Doctypes
86
            if (in_array($record['doktype'], [4])) {
87
                continue;
88
            }
89
90
            // Build Node
91
            $node = new Node();
92
            $node->setLoc($url);
93
            $node->setPriority($this->getPriority($startPage, $record));
94
            $node->setChangefreq(SitemapDataService::mapTimeout2Period($record['cache_timeout']));
95
            $node->setLastmod($this->getModifiedDate($record));
96
            $node->setImages($imageNodes);
97
98
99
            $nodes[] = $node;
100
        }
101
102
        return $nodes;
103
    }
104
105
    /**
106
     * Get alle images on the given pages
107
     *
108
     * @param array $pages
109
     *
110
     * @return array
111
     */
112
    protected function getImagesByPages(array $pages)
113
    {
114
        $images = [];
115
116
        if (!sizeof($pages)) {
117
            return $images;
118
        }
119
120
        $enabledFields = $this->pageRepository->enableFields('sys_file_reference');
121
        $enabledFields .= $this->pageRepository->enableFields('tt_content');
122
        $enabledFields .= $this->pageRepository->enableFields('pages');
123
124
        $database = $this->getDatabaseConnection();
125
        $rows = $database->exec_SELECTgetRows(
126
            'sys_file_reference.*',
127
            'sys_file_reference, tt_content, pages',
128
            'sys_file_reference.tablenames=' . $database->fullQuoteStr(
129
                'tt_content',
130
                'sys_file_reference'
131
            ) . ' AND sys_file_reference.fieldname=' . $database->fullQuoteStr(
132
                'image',
133
                'sys_file_reference'
134
            ) . ' AND sys_file_reference.uid_foreign=tt_content.uid AND tt_content.pid=pages.uid AND pages.uid IN (' . implode(
135
                ',',
136
                $pages
137
            ) . ') ' . $enabledFields
138
        );
139
140
        foreach ($rows as $row) {
0 ignored issues
show
Bug introduced by
The expression $rows of type null|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
141
            $images[] = $this->resourceFactory->getFileReferenceObject($row['uid'], $row);
142
        }
143
        return $images;
144
    }
145
146
    /**
147
     * Get the database connection
148
     *
149
     * @return \TYPO3\CMS\Core\Database\DatabaseConnection
150
     */
151
    protected function getDatabaseConnection()
152
    {
153
        return $GLOBALS['TYPO3_DB'];
154
    }
155
}
156