PrepopulateUuidExtension   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 18
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 3
eloc 8
c 2
b 0
f 0
dl 0
loc 18
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A onAfterBuild() 0 10 3
1
<?php
2
3
namespace LeKoala\Uuid;
4
5
use SilverStripe\Core\Extension;
6
use SilverStripe\ORM\DataObject;
7
use SilverStripe\ORM\DB;
8
9
/**
10
 * Add this extension to any class requiring pre-population of Uuids on dev/build
11
 */
12
class PrepopulateUuidExtension extends Extension
13
{
14
    /**
15
     * Invoked after every database build is complete (including after table creation and
16
     * default record population).
17
     *
18
     * See {@link DatabaseAdmin::doBuild()} for context.
19
     */
20
    public function onAfterBuild()
21
    {
22
        $emptyUuidItems = DataObject::get($this->owner->getClassName())->filter('Uuid', null);
23
        if ($emptyUuidCount = $emptyUuidItems->count()) {
24
            foreach ($emptyUuidItems as $item) {
25
                $item->UuidSegment();
26
            }
27
            DB::alteration_message(
28
                "{$this->owner->getClassName()}: {$emptyUuidCount} empty Uuids prepopulated",
29
                'changed'
30
            );
31
        }
32
    }
33
}
34