Passed
Push — master ( c8b9d6...4facea )
by Nicolaas
02:05
created

PruneAllVersionedRecordsReviewTemplates::run()   B

Complexity

Conditions 8
Paths 9

Size

Total Lines 26
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 0 Features 0
Metric Value
cc 8
eloc 22
c 5
b 0
f 0
nc 9
nop 1
dl 0
loc 26
rs 8.4444
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
    /**
23
     * @var string
24
     */
25
    private static $segment = 'prune-all-versioned-records-review-templates';
26
27
    /**
28
     * Prune all published DataObjects which are published according to config.
29
     *
30
     * @param mixed $request
31
     */
32
    public function run($request)
33
    {
34
        $allClasses = ClassInfo::subclassesFor(DataObject::class, false);
35
        $runner = RunForOneObject::inst();
36
        Versioned::set_stage(Versioned::DRAFT);
37
        foreach ($allClasses as $className) {
38
            $name = Injector::inst()->get($className)->i18n_singular_name();
39
            $count = $this->getObjectCountPerClassName($className);
40
            $versionCount = $this->getObjectCountPerClassName($className);
41
            if ($count) {
42
                $object = DataObject::get_one($className);
43
                if ($object) {
44
                    $array = $runner->getTemplatesDescription($object);
45
                    if (count($array)) {
46
                        DB::alteration_message('-----------------------------------');
47
                        DB::alteration_message($name . ' (' . $count . ' records, '.$versionCount.' version records) ' . $className);
48
                        DB::alteration_message('... ' . $className);
49
                        foreach ($array as $string) {
50
                            DB::alteration_message('... ... ' . $string);
51
                        }
52
                    }
53
                    $array = $runner->getTableSizes($object);
54
                    if(! empty($array)) {
55
                        DB::alteration_message('... Tables');
56
                        foreach($array as $table => $size) {
57
                            DB::alteration_message('... ... ' . $table.': '. number_format($size));
58
                        }
59
                    }
60
                }
61
            }
62
        }
63
    }
64
65
    protected function getObjectCountPerClassName(string $className): int
66
    {
67
        return $className::get()->limit(100000)->count();
68
    }
69
70
    protected function getObjectCountForVersionsPerClassName(string $className): int
71
    {
72
        $tableName = Config::inst()->get($className, 'table_name');
73
        return DB::query('SELECT COUNT("ID") FROM "'.$tableName.'_Versions";')->value();
0 ignored issues
show
Bug Best Practice introduced by
The expression return SilverStripe\ORM\...'_Versions";')->value() returns the type string which is incompatible with the type-hinted return integer.
Loading history...
74
    }
75
}
76