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

ActionPublish   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 25
dl 0
loc 50
rs 10
c 0
b 0
f 0
wmc 11

1 Method

Rating   Name   Duplication   Size   Complexity  
B publish() 0 41 11
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\FormCommentModerate;
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 ActionPublish
18
 * @package Apps\Controller\Admin\Comments
19
 * @property Request $request
20
 * @property Response $response
21
 * @property View $view
22
 */
23
trait ActionPublish
24
{
25
    /**
26
     * Moderate guest comments and answer - make it publish
27
     * @param string $type
28
     * @param string|null $id
29
     * @return string
30
     * @throws NotFoundException
31
     */
32
    public function publish(string $type, ?string $id = null): ?string
33
    {
34
        // check if it multiple accept ids
35
        if (!$id || (int)$id < 1) {
36
            $ids = $this->request->query->get('selected');
37
            if (!Any::isArray($ids) || !Arr::onlyNumericValues($ids)) {
38
                throw new NotFoundException('Bad conditions');
39
            }
40
            $id = $ids;
41
        } else {
42
            $id = [$id];
43
        }
44
45
        /** @var CommentPost|CommentAnswer $query */
46
        $query = null;
47
        switch ($type) {
48
            case static::TYPE_COMMENT:
0 ignored issues
show
Bug introduced by zenn
The constant Apps\Controller\Admin\Co...onPublish::TYPE_COMMENT was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
49
                $query = CommentPost::whereIn('id', $id)->where('moderate', true);
50
                break;
51
            case static::TYPE_ANSWER:
0 ignored issues
show
Bug introduced by zenn
The constant Apps\Controller\Admin\Co...ionPublish::TYPE_ANSWER was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
52
                $query = CommentAnswer::whereIn('id', $id)->where('moderate', true);
53
                break;
54
        }
55
56
        // check if result is not empty
57
        if (!$query || $query->count() < 1) {
58
            throw new NotFoundException(__('No comments found for this condition'));
59
        }
60
61
        // initialize moderation model
62
        $model = new FormCommentModerate($query, $type);
63
64
        // check if form is submited
65
        if ($model->send()) {
66
            $model->make();
67
            App::$Session->getFlashBag()->add('success', __('Comments or answers are successful published'));
68
            $this->response->redirect('comments/' . ($type === 'answer' ? 'answerlist' : 'index'));
69
        }
70
71
        return $this->view->render('comments/publish', [
72
            'model' => $model
73
        ]);
74
    }
75
}
76