Entry   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 10
lcom 2
cbo 5
dl 0
loc 78
ccs 0
cts 41
cp 0
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 6 1
A displayName() 0 4 1
A getElementText() 0 7 2
A getUrl() 0 7 2
A lookupElementById() 0 4 1
A settings() 0 10 1
A inputTemplateVariables() 0 7 2
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://flipboxfactory.com/software/link/license
6
 * @link       https://www.flipboxfactory.com/software/link/
7
 */
8
9
namespace flipbox\link\types;
10
11
use Craft;
12
use craft\base\ElementInterface;
13
use craft\elements\Entry as EntryElement;
14
use craft\fields\Entries;
15
use craft\helpers\ArrayHelper;
16
17
/**
18
 * @author Flipbox Factory <[email protected]>
19
 * @since 1.0.0
20
 *
21
 * @method EntryElement findElement()
22
 */
23
class Entry extends Entries implements TypeInterface
24
{
25
26
    use traits\Element;
27
28
    /**
29
     * @inheritdoc
30
     */
31
    public function init()
32
    {
33
        parent::init();
34
        $this->identifier = 'entry';
35
        $this->applyDefaultProperties();
36
    }
37
38
    /**
39
     * @inheritdoc
40
     */
41
    public static function displayName(): string
42
    {
43
        return Craft::t('link', 'Entry');
44
    }
45
46
    /**
47
     * @inheritdoc
48
     */
49
    public function getElementText(): string
50
    {
51
        if (!$element = $this->findElement()) {
52
            return '';
53
        }
54
        return $element->title;
55
    }
56
57
    /**
58
     * @inheritdoc
59
     */
60
    public function getUrl(): string
61
    {
62
        if (!$element = $this->findElement()) {
63
            return '';
64
        }
65
        return (string) $element->getUrl();
66
    }
67
68
    /**
69
     * @inheritdoc
70
     */
71
    protected function lookupElementById(int $id)
72
    {
73
        return Craft::$app->getEntries()->getEntryById($id);
74
    }
75
76
    /**
77
     * @inheritdoc
78
     */
79
    public function settings(): array
80
    {
81
        // Get setting attributes from component
82
        $settings = $this->settingsAttributes();
83
84
        // Remove the public 'text' attribute (it's not a setting)
85
        ArrayHelper::removeValue($settings, 'text');
86
87
        return $settings;
88
    }
89
90
    /**
91
     * @inheritdoc
92
     */
93
    protected function inputTemplateVariables($value = null, ElementInterface $element = null): array
94
    {
95
        return parent::inputTemplateVariables(
96
            $this->findElement() ? [$this->findElement()] : null,
97
            $element
98
        );
99
    }
100
}
101