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

Apps/Controller/Admin/Comments/ActionDelete.php (3 issues)

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 || (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
        /** @var CommentPost|CommentAnswer $query */
49
        $query = null;
50
        switch ($type) {
51
            case self::TYPE_COMMENT:
0 ignored issues
show
The constant Apps\Controller\Admin\Co...ionDelete::TYPE_COMMENT was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
52
                $query = CommentPost::whereIn('id', $id);
53
                break;
54
            case self::TYPE_ANSWER:
0 ignored issues
show
The constant Apps\Controller\Admin\Co...tionDelete::TYPE_ANSWER was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
55
                $query = CommentAnswer::whereIn('id', $id);
56
                break;
57
        }
58
59
        // check if result is not empty
60
        if (!$query || $query->count() < 1) {
61
            throw new NotFoundException(__('No comments found for this condition'));
62
        }
63
64
        // initialize model
65
        $model = new FormCommentDelete($query, $type);
0 ignored issues
show
$query of type Apps\ActiveRecord\Commen...s\Core\Arch\ActiveModel is incompatible with the type Apps\ActiveRecord\Commen...nate\Support\Collection 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

65
        $model = new FormCommentDelete(/** @scrutinizer ignore-type */ $query, $type);
Loading history...
66
67
        // check if delete is submited
68
        if ($model->send() && $model->validate()) {
69
            $model->make();
70
            App::$Session->getFlashBag()->add('success', __('Comments or answers are successful deleted!'));
71
            $this->response->redirect('comments/' . ($type === self::TYPE_ANSWER ? 'answerlist' : 'index'));
72
        }
73
74
        // render view
75
        return $this->view->render('comments/delete', [
76
            'model' => $model
77
        ]);
78
    }
79
}
80