AllLinksProviderBase::arrayExcept()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 6
c 1
b 0
f 0
nc 3
nop 2
dl 0
loc 11
rs 10
1
<?php
2
3
namespace Sunnysideup\TemplateOverview\Api;
4
5
namespace Sunnysideup\TemplateOverview\Api;
6
7
use ReflectionClass;
8
use SilverStripe\CMS\Model\SiteTree;
9
use SilverStripe\Core\Config\Configurable;
10
use SilverStripe\Core\Extensible;
11
use SilverStripe\Core\Injector\Injectable;
12
use SilverStripe\Core\Injector\Injector;
13
use SilverStripe\ORM\DB;
14
15
abstract class AllLinksProviderBase
16
{
17
    use Extensible;
18
    use Injectable;
19
    use Configurable;
20
21
    protected $numberOfExamples = 1;
22
23
    private $listOfAllSiteTreeClassesCache = [];
24
25
    public function setNumberOfExamples($n): self
26
    {
27
        $this->numberOfExamples = $n;
28
29
        return $this;
30
    }
31
32
    public function getNumberOfExamples(): int
33
    {
34
        return $this->numberOfExamples;
35
    }
36
37
    /**
38
     * returns a list of all SiteTree Classes.
39
     *
40
     * @return array
41
     */
42
    public function getListOfAllClasses()
43
    {
44
        if (empty($this->listOfAllSiteTreeClassesCache)) {
45
            $siteTreeDetails = Injector::inst()->get(SiteTreeDetails::class);
46
            $list = $siteTreeDetails->ListOfAllClasses();
47
            foreach ($list as $page) {
48
                $this->listOfAllSiteTreeClassesCache[] = $page->ClassName;
49
            }
50
        }
51
52
        return $this->listOfAllSiteTreeClassesCache;
53
    }
54
55
    protected function isValidClass($class)
56
    {
57
        $obj = new ReflectionClass($class);
58
59
        return ! $obj->isAbstract();
60
    }
61
62
    /**
63
     * Takes an array, takes one item out, and returns new array.
64
     *
65
     * @param array  $array     array which will have an item taken out of it
66
     * @param string $exclusion Item to be taken out of $array
67
     *
68
     * @return array new array
69
     */
70
    protected function arrayExcept($array, $exclusion)
71
    {
72
        $newArray = $array;
73
        $count = count($newArray);
74
        for ($i = 0; $i < $count; ++$i) {
75
            if ($newArray[$i] === $exclusion) {
76
                unset($newArray[$i]);
77
            }
78
        }
79
80
        return $newArray;
81
    }
82
83
    protected function tableExists(string $table): bool
84
    {
85
        $tables = DB::table_list();
86
87
        return array_key_exists(strtolower($table), $tables);
88
    }
89
}
90