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

ActionPublish::publish()   C

Complexity

Conditions 11
Paths 19

Size

Total Lines 43
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
eloc 25
nc 19
nop 2
dl 0
loc 43
rs 5.2653
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
     * @throws \Ffcms\Core\Exception\SyntaxException
32
     */
33
    public function publish(string $type, ?string $id = null): ?string
34
    {
35
        // check if it multiple accept ids
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
            $id = $ids;
42
        } else {
43
            $id = [$id];
44
        }
45
46
        // build query
47
        $query = null;
48
        switch ($type) {
49
            case static::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...
50
                $query = CommentPost::whereIn('id', $id)->where('moderate', '=', 1);
51
                break;
52
            case static::TYPE_ANSWER:
53
                $query = CommentAnswer::whereIn('id', $id)->where('moderate', '=', 1);
54
                break;
55
        }
56
57
        // check if result is not empty
58
        if ($query === null || $query->count() < 1) {
59
            throw new NotFoundException(__('No comments found for this condition'));
60
        }
61
62
        // initialize moderation model
63
        $model = new FormCommentModerate($query, $type);
64
65
        // check if form is submited
66
        if ($model->send()) {
67
            $model->make();
68
            App::$Session->getFlashBag()->add('success', __('Comments or answers are successful published'));
69
            $this->response->redirect('comments/' . ($type === 'answer' ? 'answerlist' : 'index'));
70
        }
71
72
        return $this->view->render('publish', [
73
            'model' => $model
74
        ]);
75
    }
76
}
77