Link   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 136
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 6
dl 0
loc 136
ccs 0
cts 53
cp 0
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 13 1
A findAllTypes() 0 8 2
A findType() 0 7 1
A registerTypes() 0 15 1
A resolveTypes() 0 12 3
A t() 0 4 1
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://raw.githubusercontent.com/flipboxfactory/craft-link/master/LICENSE
6
 * @link       https://github.com/flipboxfactory/craft-link
7
 */
8
9
namespace flipbox\craft\link;
10
11
use craft\base\Plugin;
12
use craft\events\RegisterComponentTypesEvent;
13
use craft\helpers\ArrayHelper;
14
use craft\services\Fields;
15
use flipbox\craft\link\events\RegisterLinkTypes;
16
use flipbox\craft\link\fields\Link as LinkField;
17
use flipbox\craft\link\types\Asset;
18
use flipbox\craft\link\types\Category;
19
use flipbox\craft\link\types\Email;
20
use flipbox\craft\link\types\Entry;
21
use flipbox\craft\link\types\TypeInterface;
22
use flipbox\craft\link\types\Url;
23
use flipbox\craft\link\types\User;
24
use yii\base\Event;
25
26
/**
27
 * @author Flipbox Factory <[email protected]>
28
 * @since 1.0.0
29
 */
30
class Link extends Plugin
31
{
32
    /**
33
     * The event name
34
     */
35
    const EVENT_REGISTER_TYPES = 'registerTypes';
36
37
    /**
38
     * The first party link types
39
     */
40
    const FIRST_PARTY_TYPES = [
41
        Asset::class,
42
        Category::class,
43
        Email::class,
44
        Entry::class,
45
        Url::class,
46
        User::class
47
    ];
48
49
    /**
50
     * @var TypeInterface[]
51
     */
52
    private $types;
53
54
    /**
55
     * @inheritdoc
56
     */
57
    public function init()
58
    {
59
        parent::init();
60
61
        // Register our fields
62
        Event::on(
63
            Fields::class,
64
            Fields::EVENT_REGISTER_FIELD_TYPES,
65
            function (RegisterComponentTypesEvent $event) {
66
                $event->types[] = LinkField::class;
67
            }
68
        );
69
    }
70
71
    /**
72
     * @return array
73
     */
74
    public function findAllTypes()
75
    {
76
        if ($this->types === null) {
77
            $this->types = $this->registerTypes();
78
        }
79
80
        return $this->types;
81
    }
82
83
    /**
84
     * @param string $class
85
     * @return TypeInterface|null
86
     */
87
    public function findType(string $class)
88
    {
89
        return ArrayHelper::getValue(
90
            $this->findAllTypes(),
91
            $class
92
        );
93
    }
94
95
    /**
96
     * @return array
97
     */
98
    protected function registerTypes()
99
    {
100
        $event = new RegisterLinkTypes(
101
            [
102
            'types' => static::FIRST_PARTY_TYPES
103
            ]
104
        );
105
106
        $this->trigger(
107
            self::EVENT_REGISTER_TYPES,
108
            $event
109
        );
110
111
        return $this->resolveTypes($event->types);
112
    }
113
114
    /**
115
     * @param array $types
116
     * @return array
117
     */
118
    private function resolveTypes(array $types)
119
    {
120
        $validTypes = [];
121
        foreach ($types as $type) {
122
            if (!$type instanceof TypeInterface) {
123
                $type = new $type();
124
            }
125
            $validTypes[get_class($type)] = $type;
126
        }
127
128
        return $validTypes;
129
    }
130
131
132
    /**
133
     * Translates a message to the specified language.
134
     *
135
     * This is a shortcut method of [[\Craft::t()]].
136
     *
137
     * The translation will be conducted according to the message category and the target language will be used.
138
     *
139
     * You can add parameters to a translation message that will be substituted with the corresponding value after
140
     * translation. The format for this is to use curly brackets around the parameter name as you can see in the
141
     * following example:
142
     *
143
     * ```php
144
     * $username = 'Alexander';
145
     * echo \flipbox\craft\link\Link::t('Hello, {username}!', ['username' => $username]);
146
     * ```
147
     *
148
     * Further formatting of message parameters is supported using the [PHP
149
     * intl...craft-link/src/web/assets/settings/dist/LinkConfiguration.min.jsgit add *
150
     * extensions](http://www.php.net/manual/en/intro.intl.php)
151
     * message formatter. See [[\Craft::t()]] for more details.
152
     *
153
     * @param  string $message  the message to be translated.
154
     * @param  array  $params   the parameters that will be used to replace the corresponding
155
     * placeholders in...craft-link/src/web/assets/settings/dist/LinkConfiguration.min.js
156
     * the message.
157
     * @param  string $language the language code (e.g. `en-US`, `en`). If this is null, the current
158
     * [[\yii\base\Application::language|application language]] will be used.
159
     * @return string the translated message.
160
     */
161
    public static function t($message, $params = [], $language = null)
162
    {
163
        return \Craft::t('link', $message, $params, $language);
164
    }
165
}
166