DatabasebackupLog::cycleDatabaseBackupFiles()   F
last analyzed

Complexity

Conditions 17
Paths 364

Size

Total Lines 68

Duplication

Lines 34
Ratio 50 %

Importance

Changes 0
Metric Value
dl 34
loc 68
rs 2.3333
c 0
b 0
f 0
cc 17
nc 364
nop 1

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * keeps a record for every database backup made...
5
 *
6
 *
7
 *
8
 *
9
 */
10
11
12
class DatabasebackupLog extends DataObject
13
{
14
    private static $singular_name = "Database Backup";
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
15
16
    private static $plural_name = "Database Backups";
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
17
18
    private static $db = array(
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
19
        "Title" => "Varchar(255)",
20
        "Notes" => "Text",
21
        "FullLocation" => "Varchar(255)",
22
        "SizeInBytes" => "Int",
23
        "DebugMessage" => "Text"
24
    );
25
26
    private static $indexes = array(
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
27
        "FullLocation" => true
28
    );
29
30
    private static $default_sort = "Created DESC";
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
31
32
    private static $casting = array(
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
33
        "SizeInMegabytes" => "Int"
34
    );
35
36
    private static $summary_fields = array(
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
37
        "Title" => "Title",
38
        "Created" => "Created",
39
        "SizeInMegabytes" => "Size (in Mb.)",
40
    );
41
42
    /**
43
     * location for backup file e.g. /var/backups/db.sql
44
     * @var String
45
     */
46
    private static $full_location_for_db_backup_file = "";
47
48
    /**
49
     * number of cycles before the database backups get deleted forgood...
50
     * @var Int
51
     */
52
    private static $max_db_copies = 3;
53
54
    /**
55
     * at the moment only the gzip compression is supported!
56
     * @var String
57
     */
58
    private static $compression = "";
59
60
    /**
61
     * for security reasons we set this to false ...
62
     * @var Boolean
63
     */
64
    private static $allow_restores_in_live_environment = false;
65
66
    public function canCreate($member = null)
67
    {
68
        return Permission::check("ADMIN");
69
    }
70
71
    public function canDelete($member = null)
72
    {
73
        return Permission::check("ADMIN");
74
    }
75
76
    public function canEdit($member = null)
77
    {
78
        return Permission::check("ADMIN");
79
    }
80
81
    public function getCMSFields()
82
    {
83
        $fields = parent::getCMSFields();
84
        $fields->addFieldToTab("Root.Main", new ReadonlyField("Created"));
85
        $fields->addFieldToTab("Root.Main", new ReadonlyField("FullLocation"));
86
        $fields->addFieldToTab("Root.Main", new ReadonlyField("SizeInBytes"));
87
        $fields->addFieldToTab("Root.Main", new ReadonlyField("SizeInMegabytes"));
88
        $fields->removeFieldFromTab("Root.Main", "DebugMessage");
89
        $fields->addFieldToTab("Root.Main", new LiteralField("DownloadLink", "<h2><a href=\"/backupsystem/download/".$this->ID."/\">download now</a></h2>"));
90
        return $fields;
91
    }
92
93
    /**
94
     * @casting
95
     * @return Int
0 ignored issues
show
Documentation introduced by
Should the return type not be double|string?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
96
     */
97
    public function getSizeInMegabytes()
98
    {
99
        if($this->SizeInBytes) {
0 ignored issues
show
Documentation introduced by
The property SizeInBytes does not exist on object<DatabasebackupLog>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
100
            return round($this->SizeInBytes / 1024 / 1024, 2);
0 ignored issues
show
Documentation introduced by
The property SizeInBytes does not exist on object<DatabasebackupLog>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
101
        } else {
102
            return 'n/a';
103
        }
104
    }
105
106
    /**
107
     * Adds a button the Site Config page of the CMS to rebuild the Lucene search index.
108
     */
109
    public function getCMSActions()
110
    {
111
        $actions = parent::getCMSActions();
112
        if (Permission::check("ADMIN")) {
113
            if ($fileLocation = $this->getFullLocationWithExtension()) {
114
                clearstatcache();
115
                if ($this->FullLocation && file_exists($this->FullLocation)) {
0 ignored issues
show
Documentation introduced by
The property FullLocation does not exist on object<DatabasebackupLog>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
116
                    //do nothing
117
                } else {
118
                    $lastChanged = _t('Databasebackup.NO_BACKUP_IS_AVAILABLE', 'This Backup is NOT Available ... Once created, you can access it here: '.$fileLocation);
119
                }
120
                if (!$this->exists()) {
121
                    $actions->push(
122
                        new FormAction(
123
                            'doMakeDatabaseBackup',
124
                            _t('Databasebackup.MAKE_DATABASE_BACKUP', 'Make Database Backup')."; ".$lastChanged
0 ignored issues
show
Bug introduced by
The variable $lastChanged does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
125
                        )
126
                    );
127
                } else {
128
                    if ($this->FullLocation && file_exists($this->FullLocation)) {
0 ignored issues
show
Documentation introduced by
The property FullLocation does not exist on object<DatabasebackupLog>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
129
                        if (!Director::IsLive() || $this->Config()->get("allow_restores_in_live_environment")) {
130
                            $actions->push(
131
                                new FormAction(
132
                                    'doRestoreDatabaseBackup',
133
                                    _t('Databasebackup.RESTORE_DB_BACKUP_NOW', 'Restore This Database (override current one)')
134
                                )
135
                            );
136
                        }
137
                    }
138
                }
139
            }
140
            $this->extend('updateCMSActions', $actions);
141
        }
142
        return $actions;
143
    }
144
145
    /**
146
     * if backup does not exist then make it ...
147
     * set size
148
     */
149
    public function onBeforeWrite()
150
    {
151
        parent::onBeforeWrite();
152
        clearstatcache();
153
        if (!$this->exists()) {
154
            if (!$this->FullLocation) {
0 ignored issues
show
Documentation introduced by
The property FullLocation does not exist on object<DatabasebackupLog>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
155
                if ($fileLocation = $this->getFullLocationWithExtension()) {
156
                    $fileLocation = $this->cycleDatabaseBackupFiles($fileLocation);
157
                    global $databaseConfig;
158
                    $compression = $this->Config()->get("compression");
159 View Code Duplication
                    if ($compression == "gzip") {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
160
                        $command = "mysqldump -u ".$databaseConfig["username"]." -p".$databaseConfig["password"]." -h ".$databaseConfig["server"]." ".$databaseConfig["database"]."  | gzip >  ".$fileLocation;
161
                    } else {
162
                        $command = "mysqldump -u ".$databaseConfig["username"]." -p".$databaseConfig["password"]." -h ".$databaseConfig["server"]." ".$databaseConfig["database"]." >  ".$fileLocation;
163
                    }
164
                    $this->DebugMessage = exec($command);
0 ignored issues
show
Documentation introduced by
The property DebugMessage does not exist on object<DatabasebackupLog>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
165
                    $this->FullLocation = $fileLocation;
0 ignored issues
show
Documentation introduced by
The property FullLocation does not exist on object<DatabasebackupLog>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
166
                    clearstatcache();
167
                    $this->SizeInBytes = filesize($this->FullLocation);
0 ignored issues
show
Documentation introduced by
The property SizeInBytes does not exist on object<DatabasebackupLog>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
Documentation introduced by
The property FullLocation does not exist on object<DatabasebackupLog>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
168
                }
169
            }
170
        }
171
        //just in case, we do this everytime...
172
        if (!$this->SizeInBytes && file_exists($this->FullLocation)) {
0 ignored issues
show
Documentation introduced by
The property SizeInBytes does not exist on object<DatabasebackupLog>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
Documentation introduced by
The property FullLocation does not exist on object<DatabasebackupLog>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
173
            $this->SizeInBytes = filesize($this->FullLocation);
0 ignored issues
show
Documentation introduced by
The property SizeInBytes does not exist on object<DatabasebackupLog>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
Documentation introduced by
The property FullLocation does not exist on object<DatabasebackupLog>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
174
        }
175
        if (!$this->Title) {
0 ignored issues
show
Documentation introduced by
The property Title does not exist on object<DatabasebackupLog>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
176
            $this->Title = $this->FullLocation." (" .$this->getSizeInMegabytes()."mb.)";
0 ignored issues
show
Documentation introduced by
The property Title does not exist on object<DatabasebackupLog>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
Documentation introduced by
The property FullLocation does not exist on object<DatabasebackupLog>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
177
        }
178
    }
179
180
    /**
181
     * delete me if file does not exist
182
     */
183
    public function onAfterWrite()
184
    {
185
        parent::onAfterWrite();
186
        clearstatcache();
187
        if (!file_exists($this->FullLocation)) {
0 ignored issues
show
Documentation introduced by
The property FullLocation does not exist on object<DatabasebackupLog>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
188
            $this->delete();
189
        }
190
    }
191
192
193
    /**
194
     * delete file if I get deleted
195
     */
196
    public function onBeforeDelete()
197
    {
198
        parent::onBeforeDelete();
199
        clearstatcache();
200
        if (file_exists($this->FullLocation)) {
0 ignored issues
show
Documentation introduced by
The property FullLocation does not exist on object<DatabasebackupLog>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
201
            unlink($this->FullLocation);
0 ignored issues
show
Documentation introduced by
The property FullLocation does not exist on object<DatabasebackupLog>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
202
        }
203
    }
204
205
    /**
206
     *
207
     * @return Boolean
208
     */
209
    public function restoreDatabaseBackup()
210
    {
211
        if (!Director::IsLive() || $this->Config()->get("allow_restores_in_live_environment")) {
212
            $fileLocation = $this->FullLocation;
0 ignored issues
show
Documentation introduced by
The property FullLocation does not exist on object<DatabasebackupLog>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
213
            if (file_exists($fileLocation)) {
214
                $this->saveToSession();
215
                global $databaseConfig;
216
                $compression = $this->Config()->get("compression");
217 View Code Duplication
                if ($compression == "gzip") {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
218
                    $command = "gunzip <  ".$fileLocation. " | mysql -u ".$databaseConfig["username"]." -p".$databaseConfig["password"]." -h ".$databaseConfig["server"]."  ".$databaseConfig["database"]." ";
219
                } else {
220
                    $command = "mysql -u ".$databaseConfig["username"]." -p".$databaseConfig["password"]." -h ".$databaseConfig["server"]."  ".$databaseConfig["database"]." <  ".$fileLocation;
221
                }
222
                exec($command);
223
                //reset list of backups ...
224
                $this->requireDefaultRecords();
225
                $this->retrieveFromSession();
226
                Controller::curr()->redirect("/admin/databasebackuplog/");
227
                return true;
228
            }
229
        }
230
        return false;
231
    }
232
233
    /**
234
     * we have this so that when we restore a database
235
     * we dont loose the backup information...
236
     * saves all the logs to session
237
     */
238
    protected function saveToSession()
239
    {
240
        foreach (DatabasebackupLog::get() as $object) {
241
            $array[$object->ID] = array(
0 ignored issues
show
Coding Style Comprehensibility introduced by
$array was never initialized. Although not strictly required by PHP, it is generally a good practice to add $array = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
242
                "FullLocation" => $object->FullLocation,
243
                "Title" => $object->Title,
244
                "Notes" => $object->Notes,
245
                "Created" => $object->Created
246
            );
247
            Session::set("DatabasebackupLogs", serialize($array));
248
        }
249
    }
250
251
    /**
252
     *
253
     * retrieves and updates all the logs from session
254
     */
255
    protected function retrieveFromSession()
256
    {
257
        $array = unserialize(Session::get("DatabasebackupLogs"));
258
        foreach ($array as $id => $values) {
259
            $obj = DatabasebackupLog::get()->filter(array("FullLocation" => $values["FullLocation"]))->first();
260
            if ($obj) {
261
                $obj->Title = convert::raw2sql($values["Title"]);
262
                $obj->Notes = convert::raw2sql($values["Notes"]);
263
                $obj->Created = convert::raw2sql($values["Created"]);
264
                $obj->write();
265
            }
266
        }
267
    }
268
269
    /**
270
     * move all of the database copies up one,
271
     * deleting the upper one.
272
     *
273
     * Returns the name of the file name freed up ... (by moving all of them one up...)
274
     *
275
     * @param string $fileLocation
276
     *
277
     * @return string File Location
278
     *
279
     */
280
    protected function cycleDatabaseBackupFiles($fileLocation)
281
    {
282
        $copyFileLocation = $fileLocation;
0 ignored issues
show
Unused Code introduced by
$copyFileLocation is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
283
        $max = $this->Config()->get("max_db_copies");
284
        for ($i = $max; $i > -1; $i--) {
285
            $lowerFileLocation = $this->olderBackupFileName($fileLocation, $i);
286
            if ($i == $max) {
287
                //delete the top one ...
288
                clearstatcache();
289 View Code Duplication
                if (file_exists($lowerFileLocation)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
290
                    clearstatcache();
291
                    $obj = DatabasebackupLog::get()->filter(array("FullLocation" => $lowerFileLocation))->First();
292
                    if ($obj) {
293
                        $obj->delete();
294
                    }
295
                }
296
            } else {
297
                $j = $i + 1;
298
                $higherFileLocation = $fileLocation.".".$j.".bak";
299
                clearstatcache();
300
                if (file_exists($lowerFileLocation)) {
301
                    //double-check the top one ...
302 View Code Duplication
                    if (file_exists($higherFileLocation)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
303
                        clearstatcache();
304
                        $obj = DatabasebackupLog::get()->filter(array("FullLocation" => $higherFileLocation))->First();
305
                        if ($obj) {
306
                            $obj->delete();
307
                        }
308
                    }
309
                    clearstatcache();
310 View Code Duplication
                    if (rename($lowerFileLocation, $higherFileLocation)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
311
                        $obj = DatabasebackupLog::get()->filter(array("FullLocation" => $lowerFileLocation))->First();
312
                        if ($obj) {
313
                            $obj->FullLocation = $higherFileLocation;
314
                            $obj->write();
315
                        }
316
                    }
317
                }
318
            }
319
        }
320
        //just in case there were NO DBes to cycle....
321
        if (!isset($lowerFileLocation)) {
322
            $lowerFileLocation = $this->olderBackupFileName($fileLocation, ".0.bak");
323
        }
324
325
        clearstatcache();
326
        if (file_exists($fileLocation)) {
327
            if (file_exists($lowerFileLocation)) {
328
                unlink($lowerFileLocation);
329
                clearstatcache();
330 View Code Duplication
                if (!file_exists($lowerFileLocation)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
331
                    $obj = DatabasebackupLog::get()->filter(array("FullLocation" => $lowerFileLocation))->First();
332
                    if ($obj) {
333
                        $obj->delete();
334
                    }
335
                }
336
            }
337
            clearstatcache();
338 View Code Duplication
            if (rename($fileLocation, $lowerFileLocation)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
339
                $obj = DatabasebackupLog::get()->filter(array("FullLocation" => $fileLocation))->First();
340
                if ($obj) {
341
                    $obj->FullLocation = $lowerFileLocation;
342
                    $obj->write();
343
                }
344
            }
345
        }
346
        return $fileLocation;
347
    }
348
349
    /**
350
     * returns best file location with compression extension...
351
     *
352
     * @return String | Null
353
     */
354
    protected function getFullLocationWithExtension()
355
    {
356
        $fileLocation = $this->Config()->get("full_location_for_db_backup_file");
357
        if ($fileLocation) {
358
            $compression = $this->Config()->get("compression");
359
            if ($compression == "gzip") {
360
                $fileLocation .= ".gz";
361
            }
362
            return $fileLocation;
363
        }
364
        return null;
365
    }
366
367
    /**
368
     * returns file name for older back up file (cycled one)
369
     *
370
     * @param String $fileLocation
371
     * @param Int $position
372
     *
373
     * @return String
374
     */
375
    protected function olderBackupFileName($fileLocation, $position)
376
    {
377
        return $fileLocation.".".$position.".bak";
378
    }
379
380
    /**
381
     * check for existing backups
382
     *
383
     */
384
    public function requireDefaultRecords()
385
    {
386
        parent::requireDefaultRecords();
387
        $array = array($this->getFullLocationWithExtension());
388
        $arrayOfIDs = DatabasebackupLog::get()->map("ID", "ID")->toArray();
389
        for ($i = 0; $i < 100; $i++) {
390
            $array[] = $this->olderBackupFileName($array[0], $i);
391
        }
392
        foreach ($array as $fileLocation) {
393
            clearstatcache();
394
            if (file_exists($fileLocation)) {
395
                $obj = DatabasebackupLog::get()->filter(array("FullLocation" => $fileLocation))->First();
396
                if ($obj) {
397
                    //do nothing
398
                } else {
399
                    $className = $this->class;
400
                    $obj = new $className;
401
                    //make sure it has a full file location!
402
                    $obj->FullLocation = $fileLocation;
403
                    $obj->write();
404
                }
405
                unset($arrayOfIDs[$obj->ID]);
406
            }
407
        }
408
        $objects = DatabasebackupLog::get()->filter(array("ID" => $arrayOfIDs));
409
        foreach ($objects as $obj) {
410
            $obj->delete();
411
        }
412
    }
413
}
414