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

SeoCategory::getRequiredPluginHandle()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
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\Category;
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 SeoCategory implements SeoElementInterface
27
{
28
29
    // Constants
30
    // =========================================================================
31
32
    const META_BUNDLE_TYPE = 'categorygroup';
33
    const REQUIRED_PLUGIN_HANDLE = null;
34
35
    // Static Methods
36
    // =========================================================================
37
38
    /**
39
     * Return the sourceBundleType for that this SeoElement handles
40
     *
41
     * @return string
42
     */
43
    public static function getMetaBundleType(): string
44
    {
45
        return self::META_BUNDLE_TYPE;
46
    }
47
48
    /**
49
     * Return the handle to a required plugin for this SeoElement type
50
     *
51
     * @return null|string
52
     */
53
    public static function getRequiredPluginHandle()
54
    {
55
        return self::REQUIRED_PLUGIN_HANDLE;
56
    }
57
58
    /**
59
     * Return an ElementQuery for the sitemap elements for the given MetaBundle
60
     *
61
     * @param MetaBundle $metaBundle
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
62
     *
63
     * @return ElementQueryInterface
64
     */
65
    public static function sitemapElementsQuery(MetaBundle $metaBundle): ElementQueryInterface
66
    {
67
        $query = Category::find()
68
            ->group($metaBundle->sourceHandle)
69
            ->siteId($metaBundle->sourceSiteId)
70
            ->limit($metaBundle->metaSitemapVars->sitemapLimit)
71
            ->enabledForSite(true);
72
        if (!empty($metaBundle->metaSitemapVars->structureDepth)) {
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 Category::find()
0 ignored issues
show
Bug Best Practice introduced by
The expression return craft\elements\Ca...ledForSite(true)->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
            ->id($elementId)
96
            ->siteId($siteId)
97
            ->limit(1)
98
            ->enabledForSite(true)
99
            ->one();
100
    }
101
}
102