Passed
Push — master ( c1fc2f...23f085 )
by Mihail
04:43
created

FormInstall::rules()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
rs 9.4286
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
3
namespace Apps\Model\Admin\Application;
4
5
use Ffcms\Core\Arch\Model;
6
use Ffcms\Core\Exception\SyntaxException;
7
use Ffcms\Core\Helper\Type\Arr;
8
use Ffcms\Core\Helper\Type\Str;
9
use Apps\ActiveRecord\App as AppRecord;
10
11
class FormInstall extends Model
12
{
13
    public $sysname;
14
15
    private $_apps;
16
    private $_type;
17
    private $_definedControllers = [];
18
19
    /**
20
     * FormInstall constructor. Pass applications object from controller
21
     * @param $apps
22
     * @param string $type
23
     * @throws SyntaxException
24
     */
25
    public function __construct($apps, $type)
26
    {
27
        $this->_apps = $apps;
28
        // check if passed type is allowed to use
29
        if (!Arr::in($type, ['app', 'widget'])) {
30
            throw new SyntaxException('The type of extension is not defined!');
31
        }
32
        $this->_type = $type;
33
        parent::__construct();
34
    }
35
36
    /**
37
     * Insert applications defined controllers to local var
38
     */
39
    public function before()
40
    {
41
        foreach ($this->_apps as $app) {
42
            $this->_definedControllers[] = (string)$app->sys_name;
43
        }
44
45
        parent::before();
46
    }
47
48
    /**
49
     * Label form
50
     * @return array
51
     */
52
    public function labels()
53
    {
54
        return [
55
            'sysname' => __('System name')
56
        ];
57
    }
58
59
    /**
60
    * Validation rules
61
    */
62
    public function rules()
63
    {
64
        return [
65
            ['sysname', 'required'],
66
            ['sysname', 'notin', $this->_definedControllers]
67
        ];
68
    }
69
70
    public function make()
71
    {
72
        $cName = ucfirst(Str::lowerCase($this->sysname));
73
        $cPath = 'Apps\Controller\Admin\\' . $cName;
74
        // if object class is not loaded - prevent install
75
        if (!class_exists($cPath) || !defined($cPath . '::VERSION')) {
76
            return false;
77
        }
78
79
        // get ext version
80
        $cVersion = (float)constant($cPath . '::VERSION');
81
        if ($cVersion < 0.1) {
82
            $cVersion = 0.1;
83
        }
84
85
        // save row to db
86
        $record = new AppRecord();
87
        $record->type = $this->_type;
0 ignored issues
show
Documentation introduced by
The property type does not exist on object<Apps\ActiveRecord\App>. 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...
88
        $record->sys_name = $cName;
0 ignored issues
show
Documentation introduced by
The property sys_name does not exist on object<Apps\ActiveRecord\App>. 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...
89
        $record->name = '';
0 ignored issues
show
Documentation introduced by
The property name does not exist on object<Apps\ActiveRecord\App>. 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...
90
        $record->disabled = 1;
0 ignored issues
show
Documentation introduced by
The property disabled does not exist on object<Apps\ActiveRecord\App>. 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...
91
        $record->version = $cVersion;
0 ignored issues
show
Documentation introduced by
The property version does not exist on object<Apps\ActiveRecord\App>. 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...
92
        $record->save();
93
94
        // callback to install method in extension
95
        if (method_exists($cPath, 'install')) {
96
            call_user_func($cPath . '::install');
97
        }
98
99
        return true;
100
    }
101
}