Passed
Push — master ( 73d14c...8a9029 )
by Mihail
04:02
created

ActionFieldDelete   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 29
rs 10
c 0
b 0
f 0
wmc 5

1 Method

Rating   Name   Duplication   Size   Complexity  
A profileFieldDelete() 0 21 5
1
<?php
2
3
namespace Apps\Controller\Admin\Profile;
4
5
6
use Apps\ActiveRecord\ProfileField;
7
use Apps\Model\Admin\Profile\FormFieldUpdate;
8
use Ffcms\Core\Arch\View;
9
use Ffcms\Core\Exception\ForbiddenException;
10
use Ffcms\Core\Helper\Type\Any;
11
use Ffcms\Core\Network\Request;
12
use Ffcms\Core\Network\Response;
13
14
/**
15
 * Trait ActionFieldDelete
16
 * @package Apps\Controller\Admin\Profile
17
 * @property Request $request
18
 * @property Response $response
19
 * @property View $view
20
 */
21
trait ActionFieldDelete
22
{
23
    /**
24
     * Delete custom profile field
25
     * @param string $id
26
     * @return string
27
     * @throws ForbiddenException
28
     */
29
    public function profileFieldDelete($id): ?string
30
    {
31
        if (!Any::isInt($id) || $id < 1) {
32
            throw new ForbiddenException();
33
        }
34
35
        // check if record with $id is exist
36
        $record = ProfileField::find($id);
37
        if (!$record) {
38
            throw new ForbiddenException();
39
        }
40
41
        $model = new FormFieldUpdate($record);
42
        // if delete is submited - lets remove this record
43
        if ($model->send()) {
44
            $model->delete();
45
            $this->response->redirect('profile/fieldlist');
46
        }
47
48
        return $this->view->render('profile/field_delete', [
49
            'model' => $model
50
        ]);
51
    }
52
}