Passed
Push — develop ( 7c77c8...ea4b85 )
by Andrew
05:55
created

SeoEntry::sitemapElementsQuery()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 9
nc 2
nop 1
dl 0
loc 13
rs 9.9666
c 0
b 0
f 0
1
<?php
2
/**
3
 * SEOmatic plugin for Craft CMS 3.x
4
 *
5
 * A turnkey SEO implementation for Craft CMS that is comprehensive, powerful,
6
 * and flexible
7
 *
8
 * @link      https://nystudio107.com
9
 * @copyright Copyright (c) 2017 nystudio107
10
 */
11
12
namespace nystudio107\seomatic\seoelements;
13
14
use nystudio107\seomatic\base\SeoElementInterface;
15
use nystudio107\seomatic\models\MetaBundle;
16
17
use craft\base\ElementInterface;
18
use craft\elements\db\ElementQueryInterface;
19
use craft\elements\Entry;
20
21
/**
22
 * @author    nystudio107
0 ignored issues
show
Coding Style introduced by
The tag in position 1 should be the @package tag
Loading history...
Coding Style introduced by
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
23
 * @package   Seomatic
24
 * @since     3.0.0
0 ignored issues
show
Coding Style introduced by
The tag in position 3 should be the @author tag
Loading history...
25
 */
0 ignored issues
show
Coding Style introduced by
Missing @license tag in class comment
Loading history...
26
class SeoEntry implements SeoElementInterface
27
{
28
    // Constants
29
    // =========================================================================
30
31
    const META_BUNDLE_TYPE = 'section';
32
    const REQUIRED_PLUGIN_HANDLE = null;
33
34
    // Static Methods
35
    // =========================================================================
36
37
    /**
38
     * Return the sourceBundleType for that this SeoElement handles
39
     *
40
     * @return string
41
     */
42
    public static function getMetaBundleType(): string
43
    {
44
        return self::META_BUNDLE_TYPE;
45
    }
46
47
    /**
48
     * Return the handle to a required plugin for this SeoElement type
49
     *
50
     * @return null|string
51
     */
52
    public static function getRequiredPluginHandle()
53
    {
54
        return self::REQUIRED_PLUGIN_HANDLE;
55
    }
56
57
    /**
58
     * Return an ElementQuery for the sitemap elements for the given MetaBundle
59
     *
60
     * @param MetaBundle $metaBundle
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
61
     *
62
     * @return ElementQueryInterface
63
     */
64
    public static function sitemapElementsQuery(MetaBundle $metaBundle): ElementQueryInterface
65
    {
66
        $query = Entry::find()
67
            ->section($metaBundle->sourceHandle)
68
            ->siteId($metaBundle->sourceSiteId)
69
            ->enabledForSite(true)
70
            ->limit($metaBundle->metaSitemapVars->sitemapLimit);
71
        if ($metaBundle->sourceType === 'structure'
72
            && !empty($metaBundle->metaSitemapVars->structureDepth)) {
0 ignored issues
show
Coding Style introduced by
Closing parenthesis of a multi-line IF statement must be on a new line
Loading history...
73
            $query->level($metaBundle->metaSitemapVars->structureDepth.'<=');
74
        }
75
76
        return $query;
77
    }
78
79
    /**
80
     * Return an ElementInterface for the sitemap alt element for the given MetaBundle
81
     * and Element ID
82
     *
83
     * @param MetaBundle $metaBundle
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
84
     * @param int        $elementId
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
85
     * @param int        $siteId
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
86
     *
87
     * @return null|ElementInterface
88
     */
89
    public static function sitemapAltElement(
90
        MetaBundle $metaBundle,
91
        int $elementId,
92
        int $siteId
93
    ): ElementInterface {
94
        return Entry::find()
0 ignored issues
show
Bug Best Practice introduced by
The expression return craft\elements\En...(true)->limit(1)->one() could return the type array|null which is incompatible with the type-hinted return craft\base\ElementInterface. Consider adding an additional type-check to rule them out.
Loading history...
95
            ->section($metaBundle->sourceHandle)
96
            ->id($elementId)
97
            ->siteId($siteId)
98
            ->enabledForSite(true)
99
            ->limit(1)
100
            ->one();
101
    }
102
}
103