Passed
Push — master ( 81f334...c86e8c )
by Mihail
03:35
created

ActionDelete   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 10

Importance

Changes 0
Metric Value
dl 0
loc 56
rs 10
c 0
b 0
f 0
wmc 12
lcom 1
cbo 10

1 Method

Rating   Name   Duplication   Size   Complexity  
C delete() 0 45 12
1
<?php
2
3
namespace Apps\Controller\Admin\Comments;
4
5
use Apps\ActiveRecord\CommentAnswer;
6
use Apps\ActiveRecord\CommentPost;
7
use Apps\Model\Admin\Comments\FormCommentDelete;
8
use Ffcms\Core\App;
9
use Ffcms\Core\Arch\View;
10
use Ffcms\Core\Exception\NotFoundException;
11
use Ffcms\Core\Helper\Type\Any;
12
use Ffcms\Core\Helper\Type\Arr;
13
use Ffcms\Core\Network\Request;
14
use Ffcms\Core\Network\Response;
15
16
/**
17
 * Trait ActionDelete
18
 * @package Apps\Controller\Admin\Comments
19
 * @property Request $request
20
 * @property Response $response
21
 * @property View $view
22
 */
23
trait ActionDelete
24
{
25
    /**
26
     * Delete comments and answers single or multiply items
27
     * @param string $type
28
     * @param string $id
29
     * @return string
30
     * @throws NotFoundException
31
     * @throws \Ffcms\Core\Exception\SyntaxException
32
     */
33
    public function delete(string $type, ?string $id = null): ?string
34
    {
35
        // sounds like a multiply delete definition
36
        if ($id === null || (int)$id < 1) {
37
            $ids = $this->request->query->get('selected');
38
            if (!Any::isArray($ids) || !Arr::onlyNumericValues($ids)) {
39
                throw new NotFoundException('Bad conditions');
40
            }
41
42
            $id = $ids;
43
        } else {
44
            $id = [$id];
45
        }
46
47
        // prepare query to db
48
        $query = null;
49
        switch ($type) {
50
            case self::TYPE_COMMENT:
0 ignored issues
show
Coding Style introduced by
case statements should be defined using a colon.

As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next break.

There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.

switch ($expr) {
    case "A": { //wrong
        doSomething();
        break;
    }
    case "B"; //wrong
        doSomething();
        break;
    case "C": //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
51
                $query = CommentPost::whereIn('id', $id);
52
                break;
53
            case self::TYPE_ANSWER:
54
                $query = CommentAnswer::whereIn('id', $id);
55
                break;
56
        }
57
58
        // check if result is not empty
59
        if ($query === null || $query->count() < 1) {
60
            throw new NotFoundException(__('No comments found for this condition'));
61
        }
62
63
        // initialize model
64
        $model = new FormCommentDelete($query, $type);
0 ignored issues
show
Documentation introduced by
$query is of type object<Ffcms\Core\Arch\ActiveModel>, but the function expects a array<integer,object<App...iveRecord\CommentPost>>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
65
66
        // check if delete is submited
67
        if ($model->send() && $model->validate()) {
68
            $model->make();
69
            App::$Session->getFlashBag()->add('success', __('Comments or answers are successful deleted!'));
70
            $this->response->redirect('comments/' . ($type === 'answer' ? 'answerlist' : 'index'));
71
        }
72
73
        // render view
74
        return $this->view->render('delete', [
75
            'model' => $model
76
        ]);
77
    }
78
}
79