1 | <?php |
||||
2 | |||||
3 | namespace Sunnysideup\UUDI\Extensions; |
||||
4 | |||||
5 | use SilverStripe\CMS\Model\SiteTree; |
||||
0 ignored issues
–
show
|
|||||
6 | use SilverStripe\Core\Config\Config; |
||||
7 | use SilverStripe\Forms\CheckboxField; |
||||
8 | use SilverStripe\Forms\FieldList; |
||||
9 | use SilverStripe\Forms\ReadonlyField; |
||||
10 | use SilverStripe\ORM\DataExtension; |
||||
11 | use SilverStripe\ORM\DataObject; |
||||
12 | use Sunnysideup\UUDI\Api\HashCreator; |
||||
13 | |||||
14 | /** |
||||
15 | * Class \Sunnysideup\UUDI\Extensions\UUIDExtension. |
||||
16 | * |
||||
17 | * @property string $UUID |
||||
18 | * @property string $PublicUUID |
||||
19 | */ |
||||
20 | class UUIDExtension extends DataExtension |
||||
21 | { |
||||
22 | private static $db = [ |
||||
23 | 'RequiresUUID' => 'Boolean(0)', |
||||
24 | 'UUID' => 'Varchar(65)', //32 + 1 + 32 |
||||
25 | 'PublicUUID' => 'Varchar(12)', |
||||
26 | ]; |
||||
27 | |||||
28 | private static $indexes = [ |
||||
29 | 'RequiresUUID' => true, |
||||
30 | 'UUID' => false, |
||||
31 | 'PublicUUID' => true, |
||||
32 | ]; |
||||
33 | |||||
34 | private $UUIDNeverAgainRaceCondition = false; |
||||
35 | |||||
36 | public function onBeforeWrite() |
||||
37 | { |
||||
38 | $owner = $this->getOwner(); |
||||
39 | if ($this->requiresUUID()) { |
||||
40 | if (! $owner->UUID) { |
||||
41 | $owner->UUID = $this->getHashID(); |
||||
42 | } |
||||
43 | } else { |
||||
44 | $this->UUIDNeverAgainRaceCondition = true; |
||||
45 | $owner->UUID = ''; |
||||
46 | } |
||||
47 | if (! $owner->PublicUUID || 'ERROR' === $owner->PublicUUID) { |
||||
48 | $owner->PublicUUID = $this->calculatePublicUUID(); |
||||
49 | } |
||||
50 | } |
||||
51 | |||||
52 | public function onAfterWrite() |
||||
53 | { |
||||
54 | $owner = $this->getOwner(); |
||||
55 | if (! $owner->PublicUUID && false === $this->UUIDNeverAgainRaceCondition) { |
||||
56 | $this->UUIDNeverAgainRaceCondition = true; |
||||
57 | $owner->write(); |
||||
58 | } |
||||
59 | } |
||||
60 | |||||
61 | public function calculatePublicUUID(?int $length = 12): string |
||||
62 | { |
||||
63 | return HashCreator::generate_hash($length); |
||||
0 ignored issues
–
show
It seems like
$length can also be of type null ; however, parameter $length of Sunnysideup\UUDI\Api\HashCreator::generate_hash() does only seem to accept integer , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
64 | } |
||||
65 | |||||
66 | public function updateCMSFields(FieldList $fields) |
||||
67 | { |
||||
68 | $owner = $this->owner; |
||||
69 | if (! ($owner instanceof SiteTree)) { |
||||
70 | $this->updateCMSFieldsForHashId($fields); |
||||
71 | } |
||||
72 | } |
||||
73 | |||||
74 | public function updateSettingsFields(FieldList $fields) |
||||
75 | { |
||||
76 | $owner = $this->owner; |
||||
77 | if ($owner instanceof SiteTree) { |
||||
78 | $this->updateCMSFieldsForHashId($fields); |
||||
79 | } |
||||
80 | } |
||||
81 | |||||
82 | public function updateCMSFieldsForHashId(FieldList $fields) |
||||
83 | { |
||||
84 | /** @var DataObject $owner */ |
||||
85 | $owner = $this->owner; |
||||
86 | $fields->removeByName( |
||||
87 | [ |
||||
88 | 'RequiresUUID', |
||||
89 | 'UUID', |
||||
90 | 'PublicUUID', |
||||
91 | ] |
||||
92 | ); |
||||
93 | if ($owner->hasMethod('ShowUUIDInCMS')) { |
||||
94 | $tab = 'Root.UUID'; |
||||
95 | if ($owner->hasMethod('UUIDTabInCMS')) { |
||||
96 | $tab = $owner->UUIDTabInCMS(); |
||||
97 | } |
||||
98 | |||||
99 | $fields->addFieldsToTab( |
||||
100 | $tab, |
||||
101 | [ |
||||
102 | // ReadonlyField::create('MyUUID', 'Private UUID', $owner->UUID), |
||||
103 | CheckboxField::create('RequiresUUID', _t('UUIDExtension.RequiresUUID', 'Hash Public UUID'), $owner->PublicUUID)->performDisabledTransformation, |
||||
104 | ReadonlyField::create('MyPublicUUID', _t('UUIDExtension.PublicUUID', 'Public UUID'), $owner->PublicUUID), |
||||
105 | ] |
||||
106 | ); |
||||
107 | } |
||||
108 | } |
||||
109 | |||||
110 | /** |
||||
111 | * Gets a truly unique identifier to the classname and ID. |
||||
112 | */ |
||||
113 | protected function getHashID(): ?string |
||||
114 | { |
||||
115 | |||||
116 | $owner = $this->getOwner(); |
||||
117 | if ($owner->ID) { |
||||
118 | return HashCreator::create_hash_id($owner->ClassName, $owner->ID) . '_' . HashCreator::generate_hash(32); |
||||
119 | } |
||||
120 | |||||
121 | return ''; |
||||
122 | } |
||||
123 | |||||
124 | protected function requiresUUID(): bool |
||||
125 | { |
||||
126 | $owner = $this->getOwner(); |
||||
127 | return $owner->RequiresUUID || |
||||
128 | Config::inst()->get(HashCreator::class, 'always_require_uuid') || |
||||
129 | Config::inst()->get($owner->ClassName, 'always_require_uuid'); |
||||
130 | } |
||||
131 | } |
||||
132 |
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:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths