Passed
Push — master ( 7d50f1...2e6260 )
by Nicolaas
02:11
created

FlushReceiver::available()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 13
rs 9.6111
cc 5
nc 4
nop 0
1
<?php
2
3
namespace Sunnysideup\FlushFrontEnd\Control;
4
5
use Exception;
6
use SilverStripe\Control\Controller;
7
use SilverStripe\Control\Director;
8
use SilverStripe\Control\Middleware\HTTPCacheControlMiddleware;
9
use SilverStripe\Core\ClassInfo;
10
use SilverStripe\Core\Flushable;
11
use SilverStripe\Core\Manifest\ClassLoader;
12
use SilverStripe\Core\Resettable;
13
use SilverStripe\ORM\FieldType\DBField;
14
use SilverStripe\Security\Permission;
15
use Sunnysideup\FlushFrontEnd\Model\FlushRecord;
16
17
class FlushReceiver extends Controller
18
{
19
    private static $allowed_actions = [
20
        'do' => true,
21
    ];
22
23
    public static function my_url_segment(): string
24
    {
25
        return 'flush-front-end';
26
    }
27
28
    public function Link($action = '')
29
    {
30
        return '/'.self::join_links(self::my_url_segment(), $action);
31
    }
32
33
    public function completed()
34
    {
35
        if (Director::is_cli() || Permission::check('ADMIN')) {
36
            $objects = FlushRecord::get()->filter(['Done' => false]);
37
            foreach($objects as $obj) {
38
                echo DBField::create_field('DateTime', $obj->LastEdited)->ago().' - '.$obj->Code;
39
                if(! Director::is_cli()) {
40
                    echo PHP_EOL;
41
                } else {
42
                    echo '<br />';
43
                }
44
            }
45
        } else {
46
            die('This needs to be run from the command line or you need to be logged in as ADMIN.');
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
47
        }
48
    }
49
50
    public function available()
51
    {
52
        if (Director::is_cli() || Permission::check('ADMIN')) {
53
            $objects = FlushRecord::get()->filter(['Done' => false]);
54
            foreach($objects as $obj) {
55
                if(Director::is_cli()) {
56
                    echo $this->Link('do/'.$obj->Code).PHP_EOL;
57
                } else {
58
                    echo '<a href="'.$this->Link('do/'.$obj->Code).'">'.$obj->Code.'</a><br />';
59
                }
60
            }
61
        } else {
62
            die('This needs to be run from the command line or you need to be logged in as ADMIN.');
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
63
        }
64
    }
65
66
    public function do($request)
67
    {
68
        if (Director::is_cli()) {
69
            die('This needs to be run from the front-end.');
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
70
        }
71
        $code = $request->param('ID');
72
        $obj = $this->getFlushRecord($code);
73
        if ($obj) {
0 ignored issues
show
introduced by
$obj is of type Sunnysideup\FlushFrontEnd\Model\FlushRecord, thus it always evaluated to true.
Loading history...
74
            // mark as done first
75
            $obj->Done = true;
0 ignored issues
show
Bug Best Practice introduced by
The property Done does not exist on Sunnysideup\FlushFrontEnd\Model\FlushRecord. Since you implemented __set, consider adding a @property annotation.
Loading history...
76
            $obj->write();
77
78
            $this->doFlush();
79
            echo 'FRONT-END FLUSHED';
80
        } else {
81
            echo '<br />ERROR';
82
        }
83
    }
84
85
    protected function doFlush()
86
    {
87
        if (file_exists(TEMP_PATH)) {
88
            $this->deleteFolderContents(TEMP_PATH);
89
        }
90
91
        HTTPCacheControlMiddleware::singleton()->disableCache(true);
92
        ClassLoader::inst()->getManifest()->regenerate(false);
93
        // Reset all resettables
94
        /** @var Resettable $resettable */
95
        foreach (ClassInfo::implementorsOf(Resettable::class) as $resettable) {
96
            $resettable::reset();
97
        }
98
99
        /** @var Flushable $class */
100
        foreach (ClassInfo::implementorsOf(Flushable::class) as $class) {
101
            $class::flush();
102
        }
103
    }
104
105
    protected function getFlushRecord(string $code): ?FlushRecord
106
    {
107
        /** @var FlushRecord $obj */
108
        $obj = FlushRecord::get()->filter(['Done' => false, 'Code' => $code])->first();
109
        if ($obj) {
0 ignored issues
show
introduced by
$obj is of type Sunnysideup\FlushFrontEnd\Model\FlushRecord, thus it always evaluated to true.
Loading history...
110
            return $obj;
111
        }
112
113
        echo 'object not found';
114
115
        return null;
116
    }
117
118
    // Function to delete files and folders recursively
119
    private function deleteFolderContents(string $folderPath)
120
    {
121
        if (! is_dir($folderPath)) {
122
            return;
123
        }
124
125
        $files = array_diff(scandir($folderPath), ['.', '..']);
126
127
        foreach ($files as $file) {
128
            $filePath = $folderPath . '/' . $file;
129
130
            if (is_dir($filePath)) {
131
                $this->deleteFolderContents((string) $filePath); // Recursively delete sub-folders
132
            } else {
133
                try {
134
                    unlink($filePath); // Delete files
135
                } catch (Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
136
                }
137
            }
138
        }
139
    }
140
}
141