Passed
Push — master ( affe5c...28600f )
by Nicolaas
02:36
created

getObjectCountPerClassName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Sunnysideup\VersionPruner\Api;
4
5
use SilverStripe\Core\ClassInfo;
6
use SilverStripe\Core\Config\Config;
7
8
use SilverStripe\Core\Injector\Injector;
9
use SilverStripe\Dev\BuildTask;
10
use SilverStripe\ORM\DataList;
11
use SilverStripe\ORM\DataObject;
12
use SilverStripe\ORM\DB;
13
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...
14
15
class PruneAllVersionedRecordsReviewTemplates extends BuildTask
16
{
17
18
    /**
19
     * @var string
20
     */
21
    protected $title = 'Prune all versioned records - review templates for each dataobject';
22
23
    protected $description = 'Go through all dataobjects and shows the pruning schedule.';
24
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) {
45
                $object = DataObject::get_one($className);
46
                if($object) {
47
                    $array = $runner->getTemplatesDescription($object);
48
                    if(count($array)) {
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
                }
56
            }
57
        }
58
    }
59
60
    protected function getObjectCountPerClassName(string $className): int
61
    {
62
        return $className::get()->limit(100000)->count();
63
    }
64
65
66
67
68
69
}
70