PruneAllVersionedRecordsReviewTemplates   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Importance

Changes 12
Bugs 0 Features 0
Metric Value
eloc 44
c 12
b 0
f 0
dl 0
loc 98
rs 10
wmc 17

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getAllVersionedDataClassesBase() 0 20 5
A getObjectCountForVersionsPerClassName() 0 8 2
A getObjectCountPerClassName() 0 7 2
B run() 0 26 8
1
<?php
2
3
namespace Sunnysideup\VersionPruner\Api;
4
5
use SilverStripe\Core\ClassInfo;
6
use SilverStripe\Core\Config\Config;
7
use SilverStripe\Core\Injector\Injector;
8
use SilverStripe\Dev\BuildTask;
9
use SilverStripe\ORM\DataObject;
10
use SilverStripe\ORM\DB;
11
use SilverStripe\Versioned\Versioned;
0 ignored issues
show
Bug introduced by
The type SilverStripe\Versioned\Versioned 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...
12
13
class PruneAllVersionedRecordsReviewTemplates extends BuildTask
14
{
15
    /**
16
     * @var string
17
     */
18
    protected $title = 'Prune all versioned records - review templates for each dataobject';
19
20
    protected $description = 'Go through all dataobjects and shows the pruning schedule.';
21
22
    protected $objectCountPerClassNameCache = [];
23
24
    protected $objectCountForVersionsPerClassNameCache = [];
25
26
    /**
27
     * @var string
28
     */
29
    private static $segment = 'prune-all-versioned-records-review-templates';
30
31
    /**
32
     * Prune all published DataObjects which are published according to config.
33
     *
34
     * @param mixed $request
35
     */
36
    public function run($request)
37
    {
38
        $allClasses = ClassInfo::subclassesFor(DataObject::class, false);
39
        $runner = RunForOneObject::inst();
40
        Versioned::set_stage(Versioned::DRAFT);
41
        foreach ($allClasses as $className) {
42
            $name = Injector::inst()->get($className)->i18n_singular_name();
43
            $count = $this->getObjectCountPerClassName($className);
44
            if ($count !== 0) {
45
                $object = DataObject::get_one($className);
46
                if ($object) {
47
                    $array = $runner->getTemplatesDescription($object);
48
                    if (count($array) > 0) {
49
                        DB::alteration_message($name . ' (' . $count . ' records) ' . $className);
50
                        DB::alteration_message('... ' . $className);
51
                        foreach ($array as $string) {
52
                            DB::alteration_message('... ... ' . $string);
53
                        }
54
                    }
55
                    // DB::alteration_message('No data for: '.$className);
56
57
                    $array = $runner->getTableSizes($object, true);
58
                    if (! empty($array)) {
59
                        DB::alteration_message('... Version Records');
60
                        foreach ($array as $table => $size) {
61
                            DB::alteration_message('... ... ' . $table . ': ' . number_format($size));
62
                        }
63
                    }
64
                }
65
            }
66
        }
67
    }
68
69
    protected function getObjectCountPerClassName(string $className): int
70
    {
71
        if (! isset($this->objectCountPerClassNameCache[$className])) {
72
            $this->objectCountPerClassNameCache[$className] = $className::get()->count();
73
        }
74
75
        return $this->objectCountPerClassNameCache[$className];
76
    }
77
78
    protected function getObjectCountForVersionsPerClassName(string $className): int
79
    {
80
        if (! isset($this->objectCountForVersionsPerClassNameCache[$className])) {
81
            $tableName = Config::inst()->get($className, 'table_name');
82
            $this->objectCountForVersionsPerClassNameCache[$className] = (int) DB::query('SELECT COUNT("ID") FROM "' . $tableName . '_Versions";')->value();
83
        }
84
85
        return $this->objectCountForVersionsPerClassNameCache[$className];
86
    }
87
88
    /**
89
     * Get all versioned database classes.
90
     */
91
    protected function getAllVersionedDataClassesBase(): array
92
    {
93
        $allClasses = ClassInfo::subclassesFor(DataObject::class);
94
        $versionedClasses = [];
95
        foreach ($allClasses as $className) {
96
            if (DataObject::has_extension($className, Versioned::class)) {
97
                $ancestors = ClassInfo::ancestry($className);
98
                foreach ($ancestors as $classNameInner) {
99
                    if (DataObject::has_extension($classNameInner, Versioned::class)) {
100
                        $versionedClasses[$classNameInner] = $classNameInner;
101
102
                        continue 2;
103
                    }
104
                }
105
106
                $versionedClasses[$className] = $className;
107
            }
108
        }
109
110
        return $versionedClasses;
111
    }
112
}
113