Passed
Push — master ( 30647e...9551d0 )
by Nicolaas
08:43
created

FlushRecord   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Importance

Changes 12
Bugs 0 Features 1
Metric Value
wmc 10
eloc 63
c 12
b 0
f 1
dl 0
loc 108
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
B flush() 0 38 7
A getCMSFields() 0 26 1
A onBeforeWrite() 0 8 1
A run_flush() 0 1 1
1
<?php
2
3
namespace Sunnysideup\FlushFrontEnd\Model;
4
5
use SilverStripe\Control\Controller;
6
use SilverStripe\Control\Director;
7
use SilverStripe\Core\Environment;
8
use SilverStripe\Core\Flushable;
9
use SilverStripe\Forms\CheckboxField;
10
use SilverStripe\Forms\ReadonlyField;
11
use SilverStripe\ORM\DataObject;
12
use SilverStripe\ORM\DB;
13
use SilverStripe\Security\Security;
14
use Sunnysideup\FlushFrontEnd\Control\FlushReceiver;
15
16
/**
17
 * Class \Sunnysideup\FlushFrontEnd\Model\FlushRecord
18
 *
19
 * @property string $Code
20
 * @property string $Response
21
 * @property bool $Done
22
 */
23
class FlushRecord extends DataObject implements Flushable
24
{
25
    protected static $done = false;
26
27
    private static $table_name = 'FlushRecord';
28
29
    private static $db = [
30
        'Code' => 'Varchar',
31
        'Response' => 'Varchar',
32
        'Done' => 'Boolean',
33
    ];
34
35
    private static $summary_fields = [
36
        'Created.Nice' => 'When',
37
        'Code' => 'Code',
38
        'Done.Nice' => 'Done',
39
    ];
40
41
    private static $indexes = [
42
        'ID' => true,
43
        'Done' => true,
44
        'Code' => true,
45
    ];
46
47
    private static $default_sort = [
48
        'ID' => 'DESC',
49
    ];
50
51
    public function getCMSFields()
52
    {
53
        $fields = parent::getCMSFields();
54
        $fields->addFieldsToTab(
55
            'Root.Main',
56
            [
57
                ReadonlyField::create(
58
                    'Created',
59
                    'When'
60
                ),
61
                ReadonlyField::create(
62
                    'Code',
63
                    'Code'
64
                ),
65
                ReadonlyField::create(
66
                    'Response',
67
                    'Response'
68
                ),
69
                CheckboxField::create(
70
                    'Done',
71
                    'Done'
72
                ),
73
            ]
74
        );
75
76
        return $fields;
77
    }
78
79
    public static function flush()
80
    {
81
        if (Security::database_is_ready() && Director::is_cli() && false === self::$done) {
82
            self::$done = true;
83
            register_shutdown_function(function () {
84
                $obj = \Sunnysideup\FlushFrontEnd\Model\FlushRecord::create();
85
                $obj->write();
86
                sleep(2);
87
                $code = $obj->Code;
88
                $url = Director::absoluteURL(
89
                    Controller::join_links(FlushReceiver::my_url_segment(), 'do', $code)
90
                );
91
                if (!Director::isLive()) {
92
                    $user = Environment::getEnv('SS_BASIC_AUTH_USER');
93
                    $pass = Environment::getEnv('SS_BASIC_AUTH_PASSWORD');
94
                    if ($user && $pass) {
95
                        $url = str_replace('://', '://' . $user . ':' . $pass . '@', $url);
96
                    }
97
                }
98
                DB::alteration_message('Creating flush link: ' . $url);
99
                // Create a new cURL resource
100
                $ch = curl_init();
101
102
                // Set the file URL to fetch through cURL
103
                curl_setopt($ch, CURLOPT_URL, $url);
104
105
                // Do not check the SSL certificates
106
                curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
107
108
                // Return the actual result of the curl result instead of success code
109
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
110
                curl_setopt($ch, CURLOPT_HEADER, 0);
111
                $data = curl_exec($ch);
112
                curl_close($ch);
113
114
                $obj->Response = $data;
115
                $obj->write();
116
                echo $data;
0 ignored issues
show
Bug introduced by
Are you sure $data of type string|true can be used in echo? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

116
                echo /** @scrutinizer ignore-type */ $data;
Loading history...
117
            });
118
        }
119
    }
120
121
    public static function run_flush($url) {}
0 ignored issues
show
Unused Code introduced by
The parameter $url is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

121
    public static function run_flush(/** @scrutinizer ignore-unused */ $url) {}

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
122
123
    protected function onBeforeWrite()
124
    {
125
        parent::onBeforeWrite();
126
        $hex = bin2hex(random_bytes(18));
127
        $code = serialize($hex);
128
        $code = preg_replace('#[^a-zA-Z0-9]+#', '', $code);
129
130
        $this->Code = $code;
131
    }
132
}
133