Passed
Push — master ( dab3e3...c8b9d6 )
by Nicolaas
02:03
created

getObjectCountForVersionsPerClassName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 4
rs 10
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
                }
54
            }
55
        }
56
    }
57
58
    protected function getObjectCountPerClassName(string $className): int
59
    {
60
        return $className::get()->limit(100000)->count();
61
    }
62
63
    protected function getObjectCountForVersionsPerClassName(string $className): int
64
    {
65
        $tableName = Config::inst()->get($className, 'table_name');
66
        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...
67
    }
68
}
69