Test Failed
Push — master ( e3c39f...fe570d )
by Mihail
07:20
created

Apps/Model/Admin/Content/FormCategoryUpdate.php (3 issues)

1
<?php
2
3
namespace Apps\Model\Admin\Content;
4
5
use Apps\ActiveRecord\ContentCategory;
6
use Ffcms\Core\App;
7
use Ffcms\Core\Arch\Model;
8
use Ffcms\Core\Exception\SyntaxException;
9
use Ffcms\Core\Helper\Type\Any;
10
use Ffcms\Core\Helper\Type\Str;
11
12
/**
13
 * Class FormCategoryUpdate. Category update business logic model
14
 * @package Apps\Model\Admin\Content
15
 */
16
class FormCategoryUpdate extends Model
17
{
18
    public $id;
19
    public $path;
20
    public $title = [];
21
    public $description = [];
22
    public $configs = [];
23
    public $dependId = 1;
24
25
    private $_pathNested;
26
27
    private $_tmpDependId = null;
28
    private $_record;
29
    private $_new = false;
30
31
    /**
32
     * FormCategoryUpdate constructor. Pass record object inside model
33
     * @param ContentCategory $record
34
     * @param int|null $dependId
35
     */
36
    public function __construct(ContentCategory $record, $dependId = null)
37
    {
38
        $this->_record = $record;
39
        $this->_tmpDependId = (int)$dependId;
40
        parent::__construct();
41
    }
42
43
    /**
44
     * Set model attributes from record object
45
     */
46
    public function before()
47
    {
48
        if ($this->_record->id === null) {
49
            $this->_new = true;
50
            // pass owner id category from construct model
51
            $this->dependId = $this->_tmpDependId < 1 ? 1 : $this->_tmpDependId;
52
        } else {
53
            // make tmp id for frontend form
54
            $this->id = $this->_record->id;
55
            $path = $this->_record->path;
56
            // nesting levels
57
            if (Str::contains('/', $path)) {
58
                $nestedPath = explode('/', $path);
59
                $this->path = array_pop($nestedPath);
60
                $this->_pathNested = implode('/', $nestedPath);
61
                // get owner category id by nesting path
62
                $owner = ContentCategory::getByPath($this->_pathNested);
63
                if ($owner !== null) {
64
                    $this->dependId = $owner->id;
0 ignored issues
show
Documentation Bug introduced by
It seems like $owner->id can also be of type Illuminate\Support\HigherOrderCollectionProxy. However, the property $id is declared as type integer. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
65
                }
66
            } else {
67
                $this->path = $path;
68
            }
69
70
            // set data from record
71
            $this->title = $this->_record->title;
72
            $this->description = $this->_record->description;
73
            if ($this->_record->configs !== null && !Any::isEmpty($this->_record->configs)) {
74
                $this->configs = $this->_record->configs;
75
            }
76
        }
77
    }
78
79
    /**
80
     * Form display labels
81
     * @return array
82
     */
83
    public function labels(): array
84
    {
85
        return [
86
            'title' => __('Title'),
87
            'description' => __('Description'),
88
            'path' => __('Path slug'),
89
            'dependId' => __('Owner category'),
90
            'configs.showDate' => __('Show date'),
91
            'configs.showRating' => __('Show rating'),
92
            'configs.showAuthor' => __('Show author'),
93
            'configs.showViews' => __('Show views'),
94
            'configs.showComments' => __('Show comments'),
95
            'configs.showPoster' => __('Show poster'),
96
            'configs.showCategory' => __('Show category'),
97
            'configs.showRss' => __('Show RSS'),
98
            'configs.showSimilar' => __("Show similar items"),
99
            'configs.showTags' => __('Keywords to tags')
100
        ];
101
    }
102
103
    /**
104
     * Validation rules
105
     * @return array
106
     */
107
    public function rules(): array
108
    {
109
        $rules = [
110
            [['title', 'description', 'configs'], 'used'],
111
            [['configs.showDate', 'configs.showRating', 'configs.showAuthor', 'configs.showViews', 'configs.showComments'], 'boolean'],
112
            [['configs.showPoster', 'configs.showCategory', 'configs.showRss', 'configs.showSimilar', 'configs.showTags'], 'boolean']
113
        ];
114
115
        // general category
116
        if (!$this->_new && (int)$this->_record->id === 1) {
117
            $rules[] = ['path', 'used'];
118
        } else {
119
            $rules[] = ['path', 'required'];
120
            $rules[] = ['dependId', 'required'];
121
            $rules[] = ['path', 'Apps\Model\Admin\Content\FormCategoryUpdate::pathIsFree'];
122
            $rules[] = ['path', 'reverse_match', '/[\/\'~`\!@#\$%\^&\*\(\)+=\{\}\[\]\|;:"\<\>,\?\\\]/'];
123
        }
124
125
        $rules[] = ['title.' . App::$Request->getLanguage(), 'required'];
126
127
128
        return $rules;
129
    }
130
131
    /**
132
     * Save changed data in db
133
     */
134
    public function save()
135
    {
136
        $this->_record->title = $this->title;
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->title of type array is incompatible with the declared type string of property $title.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
137
        $this->_record->description = $this->description;
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->description of type array is incompatible with the declared type string of property $description.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
138
        $savePath = trim($this->_pathNested . '/' . $this->path, '/');
139
        $this->_record->path = $savePath;
140
        $this->_record->configs = $this->configs;
141
        $this->_record->save();
142
    }
143
144
    /**
145
     * Get allowed category ids as array (string values for validation)
146
     * @return array|null
147
     */
148
    public function categoryList(): ?array
149
    {
150
        $response = ContentCategory::getSortedCategories();
151
        if ($this->id) {
152
            unset($response[$this->id]);
153
        }
154
        return $response;
155
    }
156
157
    /**
158
     * Validate pathway
159
     * @param string $path
160
     * @return bool
161
     * @throws SyntaxException
162
     */
163
    public function pathIsFree($path): bool
164
    {
165
        $owner = ContentCategory::getById($this->dependId);
166
        if (!$owner) {
167
            throw new SyntaxException(__('No owner category found'));
168
        }
169
170
        // build path with owner category
171
        $this->_pathNested = $owner->path;
172
        if (Str::length($this->_pathNested) > 0) {
173
            $path = $this->_pathNested . '/' . $path;
174
        }
175
176
        // make select for check
177
        $query = ContentCategory::where('path', $path);
178
        if (!$this->_new) {
179
            // exclude current category from checking path's
180
            $query->where('id', '!=', $this->_record->id);
181
        }
182
183
        return $query->count() === 0;
184
    }
185
}
186