Passed
Push — master ( f510e9...9822a2 )
by Paul
10:29
created

Hooks::runIntegrations()   B

Complexity

Conditions 7
Paths 5

Size

Total Lines 18
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 0
Metric Value
eloc 14
dl 0
loc 18
ccs 0
cts 15
cp 0
rs 8.8333
c 0
b 0
f 0
cc 7
nc 5
nop 0
crap 56
1
<?php
2
3
namespace GeminiLabs\SiteReviews;
4
5
use GeminiLabs\SiteReviews\Contracts\HooksContract;
6
7
class Hooks implements HooksContract
8
{
9
    /**
10
     * @return void
11
     */
12
    public function run()
13
    {
14
        $dir = glsr()->path('plugin/Hooks');
15
        if (!is_dir($dir)) {
16
            return;
17
        }
18
        $iterator = new \DirectoryIterator($dir);
19
        foreach ($iterator as $fileinfo) {
20
            if (!$fileinfo->isFile()) {
21
                continue;
22
            }
23
            try {
24
                $hooks = '\GeminiLabs\SiteReviews\Hooks\\'.$fileinfo->getBasename('.php');
25
                $reflect = new \ReflectionClass($hooks);
26
                if ($reflect->isInstantiable()) {
27
                    glsr()->singleton($hooks);
28
                    glsr($hooks)->run();
29
                }
30
            } catch (\ReflectionException $e) {
31
                glsr_log()->error($e->getMessage());
32
            }
33
        }
34
        add_action('plugins_loaded', [$this, 'runIntegrations'], 100); // run after all add-ons have loaded
35
    }
36
37
    /**
38
     * @return void
39
     */
40
    public function runIntegrations()
41
    {
42
        $dir = glsr()->path('plugin/Integrations');
43
        if (!is_dir($dir)) {
44
            return;
45
        }
46
        $iterator = new \DirectoryIterator($dir);
47
        foreach ($iterator as $fileinfo) {
48
            if (!$fileinfo->isDir() || $fileinfo->isDot()) {
49
                continue;
50
            }
51
            $basename = 'GeminiLabs\SiteReviews\Integrations\\'.$fileinfo->getBasename();
52
            $controller = $basename.'\Controller';
53
            $hooks = $basename.'\Hooks';
54
            if (class_exists($controller) && class_exists($hooks)) {
55
                glsr()->singleton($controller);
56
                glsr()->singleton($hooks);
57
                glsr($hooks)->run();
58
            }
59
        }
60
    }
61
}
62