PrepopulateUuidExtension::onAfterBuild()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 7
c 2
b 0
f 0
dl 0
loc 10
rs 10
cc 3
nc 3
nop 0
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