User::settings()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 0
cts 9
cp 0
rs 9.9666
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
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\types;
10
11
use Craft;
12
use craft\elements\User as UserElement;
13
use flipbox\craft\link\Link;
14
15
/**
16
 * @author Flipbox Factory <[email protected]>
17
 * @since 1.0.0
18
 *
19
 * @method UserElement getElement()
20
 */
21
class User extends AbstractElement
22
{
23
    /**
24
     * The base template path to the field type templates
25
     */
26
    const BASE_TEMPLATE_PATH = 'link/_components/fieldtypes/Link/types/element/user';
27
28
    /**
29
     * The settings template path
30
     */
31
    const SETTINGS_TEMPLATE_PATH = self::BASE_TEMPLATE_PATH . '/settings';
32
33
    /**
34
     * @var string
35
     */
36
    public $uri = 'mailto:{email}';
37
38
    /**
39
     * @inheritdoc
40
     */
41
    public static function displayName(): string
42
    {
43
        return Link::t('User');
44
    }
45
46
    /**
47
     * @inheritdoc
48
     */
49
    protected static function elementType(): string
50
    {
51
        return UserElement::class;
52
    }
53
54
    /**
55
     * @inheritdoc
56
     */
57
    public function settings(): array
58
    {
59
        return array_merge(
60
            parent::settings(),
61
            [
62
                'uri'
63
            ]
64
        );
65
    }
66
67
    /**
68
     * @inheritdoc
69
     */
70
    public function getElementText(): string
71
    {
72
        /** @var \craft\elements\User $element */
73
        if (!$element = $this->getElement()) {
74
            return '';
75
        }
76
        return (string) $element->getFullName();
77
    }
78
79
    /**
80
     * @inheritdoc
81
     * @throws \Throwable
82
     * @throws \yii\base\Exception
83
     */
84
    public function getUrl(): string
85
    {
86
        if (!$element = $this->getElement()) {
87
            return '';
88
        }
89
90
        return Craft::$app->getView()->renderObjectTemplate(
91
            $this->uri,
92
            $element
93
        );
94
    }
95
96
    /**
97
     * @inheritdoc
98
     */
99
    public function rules()
100
    {
101
        return array_merge(
102
            parent::rules(),
103
            [
104
                [
105
                    [
106
                        'uri'
107
                    ],
108
                    'required',
109
                    'on' => [
110
                        self::SCENARIO_INPUT
111
                    ]
112
                ],
113
                [
114
                    [
115
                        'uri'
116
                    ],
117
                    'safe',
118
                    'on' => [
119
                        self::SCENARIO_DEFAULT
120
                    ]
121
                ]
122
            ]
123
        );
124
    }
125
126
    /**
127
     * @param $uri
128
     * @return $this
129
     *
130
     * @deprecated Handling legacy setting attribute
131
     */
132
    public function setUriPath($uri)
133
    {
134
        $this->uri = $uri;
135
        return $this;
136
    }
137
}
138