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

SeoElements::getSeoElementByMetaBundleType()   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 1
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\services;
13
14
use nystudio107\seomatic\Seomatic;
15
use nystudio107\seomatic\base\SeoElementInterface;
16
use nystudio107\seomatic\seoelements\SeoCategory;
17
use nystudio107\seomatic\seoelements\SeoEntry;
18
use nystudio107\seomatic\seoelements\SeoProduct;
19
20
use Craft;
21
use craft\base\Component;
22
use craft\events\RegisterComponentTypesEvent;
23
24
/**
25
 * @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...
26
 * @package   Seomatic
27
 * @since     3.0.0
0 ignored issues
show
Coding Style introduced by
The tag in position 3 should be the @author tag
Loading history...
28
 */
0 ignored issues
show
Coding Style introduced by
Missing @license tag in class comment
Loading history...
29
class SeoElements extends Component
30
{
31
    // Constants
32
    // =========================================================================
33
34
    /**
35
     * @event RegisterComponentTypesEvent The event that is triggered when
36
     *        registering SeoElement types
37
     *
38
     * SeoElement types must implement [[SeoElementInterface]]
39
     *
40
     * ```php
41
     * use nystudio107\seomatic\services\SeoElements;
42
     * use craft\events\RegisterComponentTypesEvent;
43
     * use yii\base\Event;
44
     *
45
     * Event::on(SeoElements::class,
46
     *     SeoElements::EVENT_REGISTER_SEO_ELEMENT_TYPES,
47
     *     function(RegisterComponentTypesEvent $event) {
48
     *         $event->types[] = MySeoElement::class;
49
     *     }
50
     * );
51
     * ```
52
     */
53
    const EVENT_REGISTER_SEO_ELEMENT_TYPES = 'registerSeoElementTypes';
54
55
    const DEFAULT_SEO_ELEMENT_TYPES = [
56
        SeoCategory::class,
57
        SeoEntry::class,
58
        SeoProduct::class,
59
    ];
60
61
    // Protected Properties
62
    // =========================================================================
63
64
    /**
65
     * @var SeoElementInterface[] indexed by [sourceType]
66
     */
67
    protected $seoElements;
68
69
    // Public Methods
70
    // =========================================================================
71
72
    /**
73
     * @inheritDoc
74
     */
75
    public function init()
76
    {
77
        parent::init();
78
        $this->getAllSeoElementTypes();
79
    }
80
81
    /**
82
     * @param string $type
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
83
     *
84
     * @return SeoElementInterface|null
85
     */
86
    public function getSeoElementByMetaBundleType(string $type)
87
    {
88
        return $this->seoElements[$type] ?? null;
89
    }
90
91
    /**
92
     * Returns all available field type classes.
93
     *
94
     * @return string[] The available field type classes
95
     */
96
    public function getAllSeoElementTypes(): array
97
    {
98
        // Return the memoized version if available
99
        if (!empty($this->seoElements)) {
100
            return $this->seoElements;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->seoElements returns the type nystudio107\seomatic\base\SeoElementInterface[] which is incompatible with the documented return type string[].
Loading history...
101
        }
102
        // Merge in the built-in types with the types defined in the config.php
103
        $seoElementTypes = array_unique(array_merge(
104
            SeoMatic::$plugin->getSettings()->defaultSeoElementTypes ?? [],
105
            self::DEFAULT_SEO_ELEMENT_TYPES
106
        ), SORT_REGULAR);
107
        // Throw an event to allow other modules/plugins to define their own types
108
        $event = new RegisterComponentTypesEvent([
109
            'types' => $seoElementTypes,
110
        ]);
111
        $this->trigger(self::EVENT_REGISTER_SEO_ELEMENT_TYPES, $event);
112
        // Index the array by META_BUNDLE_TYPE
113
        /** @var SeoElementInterface $seoElement */
0 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
114
        foreach ($event->types as $seoElement) {
115
            $requiredPlugin = $seoElement::getRequiredPluginHandle();
116
            if ($requiredPlugin === null || Craft::$app->getPlugins()->getPlugin($requiredPlugin)) {
117
                $this->seoElements[$seoElement::getMetaBundleType()] = $seoElement;
118
            }
119
        }
120
121
        return $this->seoElements;
122
    }
123
}
124