Passed
Push — master ( bec2a0...9c6a4b )
by Nicolaas
02:11
created

AllLinksDataObjects   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 29
c 1
b 0
f 0
dl 0
loc 55
rs 10
wmc 13

1 Method

Rating   Name   Duplication   Size   Complexity  
C getAllLinksInner() 0 45 13
1
<?php
2
3
namespace Sunnysideup\TemplateOverview\Api\Providers;
4
use Sunnysideup\TemplateOverview\Api\AllLinksProviderBase;
5
use Sunnysideup\TemplateOverview\Api\AllLinks;
6
use ReflectionClass;
7
use ReflectionMethod;
8
9
10
11
12
use SilverStripe\Admin\LeftAndMain;
13
use SilverStripe\CMS\Controllers\ContentController;
14
use SilverStripe\Control\Controller;
15
use SilverStripe\Control\Director;
16
use SilverStripe\Core\ClassInfo;
17
use SilverStripe\Core\Config\Config;
18
use SilverStripe\Core\Injector\Injector;
19
20
use SilverStripe\ORM\DataObject;
21
use SilverStripe\ORM\DB;
22
23
class AllLinksDataObjects extends AllLinksProviderBase
24
{
25
26
27
28
    /**
29
     * @param bool $inCMS
30
     *
31
     * @return array
32
     */
33
    public function getAllLinksInner(bool $inCMS)
34
    {
35
        //first() will return null or the object
36
        $return = [];
37
        $list = ClassInfo::subclassesFor(DataObject::class);
38
        $exceptForArray = array_merge($this->getListOfAllSiteTreeClasses(), [DataObject::class]);
39
        foreach ($list as $class) {
40
            if (! in_array($class, $exceptForArray, true)) {
41
                if ($this->isValidClass($class)) {
42
                    for ($i = 0; $i < $this->getNumberOfExamples(); $i++) {
43
                        $obj = DataObject::get_one(
44
                            $class,
45
                            ['ClassName' => $class],
46
                            null,
47
                            DB::get_conn()->random() . ' ASC'
48
                        );
49
                        if ($obj) {
50
                            if ($inCMS) {
51
                                if ($obj->hasMethod('CMSEditLink')) {
52
                                    $return[] = $obj->CMSEditLink();
53
                                }
54
                                if ($obj->hasMethod('CMSAddLink')) {
55
                                    $return[] = $obj->CMSAddLink();
56
                                }
57
                                if ($obj->hasMethod('CMSListLink')) {
58
                                    $return[] = $obj->CMSListLink();
59
                                }
60
                                if ($obj->hasMethod('PreviewLink')) {
61
                                    $return[] = $obj->PreviewLink();
62
                                }
63
                            } else {
64
                                if ($obj->hasMethod('Link')) {
65
                                    $return[] = $obj->Link();
66
                                }
67
                                if ($obj->hasMethod('getLink')) {
68
                                    $return[] = $obj->getLink();
69
                                }
70
                            }
71
                        }
72
                    }
73
                }
74
            }
75
        }
76
77
        return $return;
78
    }
79
}
80