WebsiteCommentController   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 126
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 60.42%

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 4
dl 0
loc 126
ccs 29
cts 48
cp 0.6042
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
B cgetCommentsAction() 0 33 4
B postCommentsAction() 0 38 6
A getThreadIdParts() 0 6 1
A getTemplate() 0 11 2
1
<?php
2
3
/*
4
 * This file is part of Sulu.
5
 *
6
 * (c) MASSIVE ART WebServices GmbH
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace Sulu\Bundle\CommentBundle\Controller;
13
14
use FOS\RestBundle\Controller\Annotations\RouteResource;
15
use FOS\RestBundle\Routing\ClassResourceInterface;
16
use Sulu\Component\Rest\RestController;
17
use Symfony\Component\HttpFoundation\Request;
18
use Symfony\Component\HttpFoundation\Response;
19
20
/**
21
 * @RouteResource("thread")
22
 *
23
 * Provides a website-api for comments.
24
 */
25
class WebsiteCommentController extends RestController implements ClassResourceInterface
26
{
27
    /**
28
     * Returns list of comments for given thread.
29
     *
30
     * @param string $threadId
31
     * @param Request $request
32
     *
33
     * @return Response
34
     */
35 1
    public function cgetCommentsAction($threadId, Request $request)
36
    {
37 1
        list($type, $entityId) = $this->getThreadIdParts($threadId);
38
39 1
        $page = $request->get('page');
40
41 1
        $commentManager = $this->get('sulu_comment.manager');
42 1
        $comments = $commentManager->findPublishedComments(
43
            $type,
44
            $entityId,
45 1
            $page ?: 1,
46 1
            $page ? 20 : null
47
        );
48
49 1
        if ('json' === $request->getRequestFormat()) {
50 1
            return $this->handleView($this->view($comments));
0 ignored issues
show
Documentation introduced by
$this->view($comments) is of type this<Sulu\Bundle\Comment...bsiteCommentController>, but the function expects a object<FOS\RestBundle\View\View>.

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...
51
        }
52
53
        $response = new Response();
54
        $response->setPrivate();
55
        $response->setMaxAge(0);
56
        $response->setSharedMaxAge(0);
57
58
        return $this->render(
59
            $this->getTemplate($type, 'comments'),
60
            [
61
                'template' => $this->getTemplate($type, 'comment'),
62
                'comments' => $comments,
63
                'threadId' => $threadId,
64
            ],
65
            $response
66
        );
67
    }
68
69
    /**
70
     * Create new comment for given thread.
71
     * If the thread does not exists a new will be created.
72
     *
73
     * @param string $threadId
74
     * @param Request $request
75
     *
76
     * @return Response
77
     */
78 9
    public function postCommentsAction($threadId, Request $request)
79
    {
80 9
        $data = $request->request->all();
81 9
        if (array_key_exists('created', $data)
82 8
            || array_key_exists('creator', $data)
83 7
            || array_key_exists('changed', $data)
84 9
            || array_key_exists('changer', $data)
85
        ) {
86 4
            return new Response(null, 400);
87
        }
88
89 5
        list($type, $entityId) = $this->getThreadIdParts($threadId);
90
91
        // deserialize comment
92 5
        $serializer = $this->get('serializer');
93 5
        $comment = $serializer->deserialize(
94
            json_encode($data),
95 5
            $this->getParameter('sulu.model.comment.class'),
96 5
            'json'
97
        );
98
99 5
        $commentManager = $this->get('sulu_comment.manager');
100 5
        $commentManager->addComment($type, $entityId, $comment, $request->get('threadTitle'));
101
102 5
        $this->get('doctrine.orm.entity_manager')->flush();
103
104 5
        if ('json' === $request->getRequestFormat()) {
105 5
            return $this->handleView($this->view($comment));
0 ignored issues
show
Documentation introduced by
$this->view($comment) is of type this<Sulu\Bundle\Comment...bsiteCommentController>, but the function expects a object<FOS\RestBundle\View\View>.

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...
106
        }
107
108
        return $this->render(
109
            $this->getTemplate($type, 'comment'),
110
            [
111
                'comment' => $comment,
112
                'threadId' => $threadId,
113
            ]
114
        );
115
    }
116
117
    /**
118
     * Splits the thread-id into type and entity-id.
119
     *
120
     * @param string $threadId
121
     *
122
     * @return array list($type, $entityId)
123
     */
124 5
    private function getThreadIdParts($threadId)
125
    {
126 5
        $pos = strpos($threadId, '-');
127
128 5
        return [substr($threadId, 0, $pos), substr($threadId, $pos + 1)];
129
    }
130
131
    /**
132
     * Returns template by type.
133
     *
134
     * @param string $type
135
     * @param string $templateType comment or comments
136
     *
137
     * @return string
138
     */
139
    private function getTemplate($type, $templateType)
140
    {
141
        $types = $this->getParameter('sulu_comment.types');
142
        $defaults = $this->getParameter('sulu_comment.default_templates');
143
144
        if (array_key_exists($type, $types)) {
145
            return $types[$type]['templates'][$templateType];
146
        }
147
148
        return $defaults[$templateType];
149
    }
150
}
151