HealthCheckItemRunner::turnPagesIntoArray()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 8
c 2
b 0
f 0
dl 0
loc 14
rs 10
cc 3
nc 3
nop 1
1
<?php
2
3
namespace Sunnysideup\HealthCheckProvider\Checks;
4
5
use SilverStripe\Core\ClassInfo;
6
use SilverStripe\Core\Config\Configurable;
7
use SilverStripe\Core\Extensible;
8
use SilverStripe\Core\Injector\Injectable;
9
use SilverStripe\ORM\SS_List;
10
use Sunnysideup\HealthCheckProvider\Model\HealthCheckItemProvider;
11
12
class HealthCheckItemRunner
13
{
14
    use Extensible;
15
    use Injectable;
16
    use Configurable;
17
18
    protected $healthCheckItemProvider;
19
20
    public function __construct(HealthCheckItemProvider $healthCheckItemProvider)
21
    {
22
        $this->healthCheckItemProvider = $healthCheckItemProvider;
23
    }
24
25
    public function IsInstalled(): bool
26
    {
27
        foreach ($this->nameSpacesRequired() as $nameSpace) {
28
            if (! $this->nameSpaceExists($nameSpace)) {
29
                return false;
30
            }
31
        }
32
        return true;
33
    }
34
35
    public function IsEnabled(): bool
36
    {
37
        return $this->IsInstalled();
38
    }
39
40
    /**
41
     * @return mixed
42
     */
43
    public function getCalculatedAnswer()
44
    {
45
        return '';
46
    }
47
48
    protected function nameSpacesRequired(): array
49
    {
50
        return [];
51
    }
52
53
    protected function nameSpaceExists(string $nameSpace): bool
54
    {
55
        $array = ClassInfo::allClasses();
56
        $nameSpace = rtrim($nameSpace, '\\') . '\\';
57
        foreach ($array as $className) {
58
            if (stripos($className, $nameSpace) === 0) {
59
                return true;
60
            }
61
        }
62
        return false;
63
    }
64
65
    protected function turnPagesIntoArray(SS_List $pages): array
66
    {
67
        $array = [];
68
        foreach ($pages as $page) {
69
            if ($page->IsPublished()) {
70
                $array[$page->ID] = [
71
                    'MenuTitle' => $page->MenuTitle,
72
                    'CMSEditLink' => $page->CMSEditLink(),
73
                    'Link' => $page->Link(),
74
                ];
75
            }
76
        }
77
78
        return $array;
79
    }
80
81
    protected function checkPassword(string $pwd): bool
82
    {
83
        if (strlen($pwd) < 16) {
84
            return false;
85
        }
86
87
        if (! preg_match('#\d+#', $pwd)) {
88
            return false;
89
        }
90
        return (bool) preg_match('#[a-zA-Z]+#', $pwd);
91
    }
92
}
93