Passed
Push — main ( 467803...6a1b4e )
by Nicolaas
16:54 queued 08:31
created

UUIDExtension::updateCMSFields()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 2
b 0
f 0
nc 2
nop 1
dl 0
loc 5
rs 10
1
<?php
2
3
namespace Sunnysideup\UUDI\Extensions;
4
5
use SilverStripe\CMS\Model\SiteTree;
0 ignored issues
show
Bug introduced by
The type SilverStripe\CMS\Model\SiteTree was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use SilverStripe\Forms\CheckboxField;
7
use SilverStripe\Forms\FieldList;
8
use SilverStripe\Forms\ReadonlyField;
9
use SilverStripe\ORM\DataExtension;
10
use SilverStripe\ORM\DataObject;
11
use Sunnysideup\UUDI\Api\HashCreator;
12
13
/**
14
 * Class \Sunnysideup\UUDI\Extensions\UUIDExtension.
15
 *
16
 * @property string $UUID
17
 * @property string $PublicUUID
18
 */
19
class UUIDExtension extends DataExtension
20
{
21
    private static $db = [
22
        'RequiresUUID' => 'Boolean(0)', //32 + 1 + 32
23
        'UUID' => 'Varchar(65)', //32 + 1 + 32
24
        'PublicUUID' => 'Varchar(12)', //32 + 1 + 32
25
    ];
26
27
    private static $indexes = [
28
        'RequiresUUID' => true,
29
        'UUID' => false,
30
        'PublicUUID' => true,
31
    ];
32
33
    private $UUIDNeverAgainRaceCondition = false;
34
35
    public function onBeforeWrite()
36
    {
37
        $owner = $this->getOwner();
38
        if ($owner->RequiresUUID) {
39
            if(!$owner->UUID) {
40
                $owner->UUID = $this->getHashID();
41
            }
42
        } else {
43
            $owner->UUID = '';
44
        }
45
        if (! $owner->PublicUUID || 'ERROR' === $owner->PublicUUID) {
46
            $owner->PublicUUID = $this->calculatePublicUUID();
47
        }
48
    }
49
50
    public function onAfterWrite()
51
    {
52
        $owner = $this->getOwner();
53
        if (! $owner->UUID && false === ! $this->UUIDNeverAgainRaceCondition) {
54
            $this->UUIDNeverAgainRaceCondition = true;
55
            $owner->write();
56
        }
57
    }
58
59
    public function calculatePublicUUID(): string
60
    {
61
        return HashCreator::generate_hash(12);
62
    }
63
64
    public function updateCMSFields(FieldList $fields)
65
    {
66
        $owner = $this->owner;
67
        if (! ($owner instanceof SiteTree)) {
68
            $this->updateCMSFieldsForHashId($fields);
69
        }
70
    }
71
72
    public function updateSettingsFields(FieldList $fields)
73
    {
74
        $owner = $this->owner;
75
        if ($owner instanceof SiteTree) {
76
            $this->updateCMSFieldsForHashId($fields);
77
        }
78
    }
79
80
    public function updateCMSFieldsForHashId(FieldList $fields)
81
    {
82
        /** @var DataObject $owner */
83
        $owner = $this->owner;
84
        $fields->removeByName(
85
            [
86
                'RequiresUUID',
87
                'UUID',
88
                'PublicUUID',
89
            ]
90
        );
91
        if ($owner->hasMethod('ShowUUIDInCMS')) {
92
            $tab = 'Root.UUID';
93
            if ($owner->hasMethod('UUIDTabInCMS')) {
94
                $tab = $owner->UUIDTabInCMS();
95
            }
96
97
            $fields->addFieldsToTab(
98
                $tab,
99
                [
100
                    // ReadonlyField::create('MyUUID', 'Private UUID', $owner->UUID),
101
                    CheckboxField::create('RequiresUUID', 'Has UUID', $owner->PublicUUID)->performDisabledTransformation,
102
                    ReadonlyField::create('MyPublicUUID', 'Public UUID', $owner->PublicUUID),
103
                ]
104
            );
105
        }
106
    }
107
108
    /**
109
     * Gets a truly unique identifier to the classname and ID.
110
     */
111
    protected function getHashID(): ?string
112
    {
113
        $owner = $this->getOwner();
114
        if ($owner->ID) {
115
            return HashCreator::create_hash_id($owner->ClassName, $owner->ID) . '_' . HashCreator::generate_hash(32);
116
        }
117
118
        return '';
119
    }
120
}
121