Passed
Push — master ( f9d5fb...222667 )
by Nicolaas
19:00 queued 09:25
created

getAllVersionedDataClassesBase()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

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