Completed
Push — master ( 9c742a...1858b1 )
by Fèvre
14s
created

src/Shell/MaintenanceShell.php (4 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
namespace App\Shell;
3
4
use Cake\Cache\Cache;
5
use Cake\Console\ConsoleOptionParser;
6
use Cake\Console\Shell;
7
use Cake\Utility\Inflector;
8
9
class MaintenanceShell extends Shell
10
{
11
    /**
12
     * The setting key to modify.
13
     *
14
     * @var string
15
     */
16
    protected $settingKey = 'Site.maintenance';
17
18
    /**
19
     * Initialize method.
20
     *
21
     * @return void
22
     */
23
    public function initialize()
24
    {
25
        parent::initialize();
26
        $this->loadModel('Settings');
27
    }
28
29
    /**
30
     * Display help for this console.
31
     *
32
     * @return ConsoleOptionParser
33
     */
34
    public function getOptionParser()
35
    {
36
        $parser = parent::getOptionParser();
37
        $parser->description(
38
            'This shell is used with the maintenance mode in the application.'
39
        )->addSubcommand('down', [
40
            'help' => 'Put the website in maintenance mode.'
41
        ])->addSubcommand('up', [
42
            'help' => 'Remove the website from the maintenance mode.'
43
        ]);
44
45
        return $parser;
46
    }
47
48
    /**
49
     * Put the application in maintenance mode.
50
     *
51
     * @return bool
52
     */
53
    public function down()
54
    {
55
        return $this->handleMaintenance('down', '1');
56
    }
57
58
    /**
59
     * Remove the application from the maintenance mode.
60
     *
61
     * @return bool
62
     */
63
    public function up()
64
    {
65
        return $this->handleMaintenance('up', '0');
66
    }
67
68
    /**
69
     * Handle the maintenance.
70
     *
71
     * @param string $type The type of the maintenance; up, down.
72
     * @param string $value The value of the setting key.
73
     *
74
     * @return bool
75
     */
76
    protected function handleMaintenance($type, $value)
77
    {
78
        $setting = $this->Settings->find()
0 ignored issues
show
The property Settings does not exist on object<App\Shell\MaintenanceShell>. 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...
79
            ->where(['name' => $this->settingKey])
80
            ->first();
81
82
        if ($this->checkKey($setting) === false) {
83
            return false;
84
        }
85
86
        $data = [
87
            'value_int' => null,
88
            'value_str' => null,
89
            'value_bool' => $value
90
        ];
91
92
        $this->Settings->patchEntity($setting, $data);
0 ignored issues
show
The property Settings does not exist on object<App\Shell\MaintenanceShell>. 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...
93
94
        if ($this->Settings->save($setting)) {
0 ignored issues
show
The property Settings does not exist on object<App\Shell\MaintenanceShell>. 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...
95
            Cache::clear(false, 'database');
96
            $this->out('<success>The</success> "<info>maintenance ' . $type . '</info>" <success>command has been executed successfully !</success>', 2);
97
98
            return true;
99
        } else {
100
            $this->out('<error>The</error> "<info>maintenance ' . $type . '</info>" <error>has failed while saving the setting key !</error>', 2);
101
102
            return false;
103
        }
104
    }
105
106
    /**
107
     * Check if the setting key exist in the database.
108
     *
109
     * @param null|App\Model\Entity\Setting $setting The setting to check.
110
     *
111
     * @return bool
112
     */
113
    protected function checkKey($setting)
114
    {
115
        if (is_null($setting)) {
116
            $msg = sprintf(
117
                'The setting key "%s" can not be found in the database, ' .
118
                'please add it to use this command shell.',
119
                $this->settingKey
120
            );
121
            $this->error($msg);
0 ignored issues
show
Deprecated Code introduced by
The method Cake\Console\Shell::error() has been deprecated with message: Since 3.2.0. Use Shell::abort() instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
122
123
            return false;
124
        }
125
126
        return true;
127
    }
128
}
129