Test Failed
Push — v5 ( ec28e0...e66c40 )
by Andrew
54:30 queued 27:51
created

SitemapController::actionGenerate()   C

Complexity

Conditions 13
Paths 48

Size

Total Lines 62
Code Lines 44

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 44
c 1
b 1
f 0
dl 0
loc 62
rs 6.6166
cc 13
nc 48
nop 0

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
 * SEOmatic plugin for Craft CMS
4
 *
5
 * @link      https://nystudio107.com/
6
 * @copyright Copyright (c) 2017 nystudio107
7
 * @license   https://nystudio107.com/license
8
 */
9
10
namespace nystudio107\seomatic\console\controllers;
11
12
use Craft;
13
use craft\helpers\App;
14
use nystudio107\seomatic\models\MetaBundle;
15
use nystudio107\seomatic\models\SitemapTemplate;
16
use nystudio107\seomatic\Seomatic;
17
use yii\console\Controller;
18
19
/**
20
 * SEOmatic Sitemap command
21
 *
22
 * @author    nystudio107
23
 * @package   Seomatic
24
 * @since     3.0.0
25
 */
26
class SitemapController extends Controller
27
{
28
    // Public Properties
29
    // =========================================================================
30
31
    /**
32
     * @var null|string The handle of the section to generate a sitemap for
33
     */
34
    public $handle;
35
36
    /**
37
     * @var null|int The siteId to generate a sitemap for
38
     */
39
    public $siteId;
40
41
    // Protected Properties
42
    // =========================================================================
43
44
    /**
45
     * @var    bool|array
46
     */
47
    protected array|bool|int $allowAnonymous = [
48
    ];
49
50
    // Public Methods
51
    // =========================================================================
52
53
    /**
54
     * @param string $actionID
55
     *
56
     * @return array|string[]
57
     */
58
    public function options($actionID): array
59
    {
60
        return [
61
            'handle',
62
            'siteId',
63
        ];
64
    }
65
66
    /**
67
     * Generate a sitemap. You can pass in a --handle and/or a --siteId
68
     */
69
    public function actionGenerate()
70
    {
71
        echo 'Generating sitemap' . PHP_EOL;
72
        if ($this->siteId !== null) {
73
            $siteIds[] = $this->siteId;
0 ignored issues
show
Comprehensibility Best Practice introduced by
$siteIds was never initialized. Although not strictly required by PHP, it is generally a good practice to add $siteIds = array(); before regardless.
Loading history...
74
        } else {
75
            $siteIds = Craft::$app->getSites()->getAllSiteIds();
76
            if (!\is_array($siteIds)) {
77
                $siteIds = [$siteIds];
78
            }
79
        }
80
        // This might take a while
81
        App::maxPowerCaptain();
82
        // Process the sitemap generation
83
        foreach ($siteIds as $siteId) {
84
            $metaBundles = Seomatic::$plugin->metaBundles->getContentMetaBundlesForSiteId($siteId);
85
            Seomatic::$plugin->metaBundles->pruneVestigialMetaBundles($metaBundles);
86
87
            /** @var MetaBundle $metaBundle */
88
            foreach ($metaBundles as $metaBundle) {
89
                $process = false;
90
                if ($this->handle === null || $this->handle === $metaBundle->sourceHandle) {
91
                    $process = true;
92
                }
93
                if ($metaBundle->metaSitemapVars->sitemapUrls && $process) {
94
                    echo 'Generating sitemap for '
95
                        . $metaBundle->sourceType
96
                        . ' '
97
                        . $metaBundle->sourceName
98
                        . ', siteId '
99
                        . $siteId
100
                        . PHP_EOL;
101
102
                    $seoElement = Seomatic::$plugin->seoElements->getSeoElementByMetaBundleType($metaBundle->sourceBundleType);
103
                    $elementQuery = $seoElement::sitemapElementsQuery($metaBundle);
104
                    $pageSize = (int) $metaBundle->metaSitemapVars->sitemapPageSize;
105
                    $sitemapLimit = (int) $metaBundle->metaSitemapVars->sitemapLimit;
106
107
                    if (!empty($pageSize)) {
108
                        $total = empty($sitemapLimit) ? $elementQuery->count() : min($elementQuery->count(), $sitemapLimit);
109
                        $pageCount = ceil($total / $pageSize);
110
                    } else {
111
                        $pageCount = 1;
112
                    }
113
114
                    $site = Craft::$app->getSites()->getSiteById($metaBundle->sourceSiteId);
115
                    $sitemap = SitemapTemplate::create();
116
                    if ($site) {
117
                        for ($pageNum = 1; $pageNum <= $pageCount; $pageNum++) {
118
                            echo sprintf('Generating page %d of %d' . PHP_EOL, $pageNum, $pageCount);
119
                            $sitemap->render([
120
                                'groupId' => $site->groupId,
121
                                'siteId' => $metaBundle->sourceSiteId,
122
                                'handle' => $metaBundle->sourceHandle,
123
                                'type' => $metaBundle->sourceBundleType,
124
                                'page' => $pageNum,
125
                            ]);
126
                        }
127
                        // Generate the sitemap so it is in the cache
128
                    }
129
130
                    echo '---' . PHP_EOL;
131
                }
132
            }
133
        }
134
    }
135
136
    // Protected Methods
137
    // =========================================================================
138
}
139