Passed
Push — master ( fa3542...0a3787 )
by Mihail
05:11
created

Apps/Controller/Admin/Comments/ActionDelete.php (1 issue)

Labels
Severity
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:
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
$query of type Ffcms\Core\Arch\ActiveModel is incompatible with the type Apps\ActiveRecord\CommentPost[] expected by parameter $record of Apps\Model\Admin\Comment...ntDelete::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

64
        $model = new FormCommentDelete(/** @scrutinizer ignore-type */ $query, $type);
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