CommentController   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 3
dl 0
loc 96
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getCommentAction() 0 4 1
A __construct() 0 4 1
A deleteCommentAction() 0 12 2
A getComment() 0 10 2
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Sonata Project package.
7
 *
8
 * (c) Thomas Rabaix <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Sonata\NewsBundle\Controller\Api;
15
16
use FOS\RestBundle\Controller\Annotations as REST;
17
use FOS\RestBundle\View\View;
18
use Nelmio\ApiDocBundle\Annotation\ApiDoc;
19
use Sonata\NewsBundle\Model\Comment;
20
use Sonata\NewsBundle\Model\CommentManagerInterface;
21
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
22
23
/**
24
 * @author Hugo Briand <[email protected]>
25
 */
26
class CommentController
27
{
28
    /**
29
     * @var CommentManagerInterface
30
     */
31
    protected $commentManager;
32
33
    /**
34
     * @param CommentManagerInterface $commentManager Comment manager
35
     */
36
    public function __construct(CommentManagerInterface $commentManager)
37
    {
38
        $this->commentManager = $commentManager;
39
    }
40
41
    /**
42
     * Retrieves a specific comment.
43
     *
44
     * @ApiDoc(
45
     *  resource=true,
46
     *  requirements={
47
     *      {"name"="id", "dataType"="integer", "requirement"="\d+", "description"="Comment identifier"}
48
     *  },
49
     *  output={"class"="Sonata\NewsBundle\Model\Comment", "groups"={"sonata_api_read"}},
50
     *  statusCodes={
51
     *      200="Returned when successful",
52
     *      404="Returned when comment is not found"
53
     *  }
54
     * )
55
     *
56
     * @REST\View(serializerGroups={"sonata_api_read"}, serializerEnableMaxDepthChecks=true)
57
     *
58
     * @param int $id Comment identifier
59
     *
60
     * @throws NotFoundHttpException
61
     *
62
     * @return Comment
63
     */
64
    public function getCommentAction($id)
65
    {
66
        return $this->getComment($id);
67
    }
68
69
    /**
70
     * Deletes a comment.
71
     *
72
     * @ApiDoc(
73
     *  requirements={
74
     *      {"name"="id", "dataType"="integer", "requirement"="\d+", "description"="Comment identifier"}
75
     *  },
76
     *  statusCodes={
77
     *      200="Returned when comment is successfully deleted",
78
     *      400="Returned when an error has occurred while comment deletion",
79
     *      404="Returned when unable to find comment"
80
     *  }
81
     * )
82
     *
83
     * @param int $id Comment identifier
84
     *
85
     * @throws NotFoundHttpException
86
     *
87
     * @return View
88
     */
89
    public function deleteCommentAction($id)
90
    {
91
        $comment = $this->getComment($id);
92
93
        try {
94
            $this->commentManager->delete($comment);
95
        } catch (\Exception $e) {
96
            return View::create(['error' => $e->getMessage()], 400);
97
        }
98
99
        return ['deleted' => true];
0 ignored issues
show
Bug Best Practice introduced by
The return type of return array('deleted' => true); (array<string,boolean>) is incompatible with the return type documented by Sonata\NewsBundle\Contro...er::deleteCommentAction of type FOS\RestBundle\View\View.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
100
    }
101
102
    /**
103
     * Returns a comment entity instance.
104
     *
105
     * @param int $id Comment identifier
106
     *
107
     * @throws NotFoundHttpException
108
     *
109
     * @return Comment
110
     */
111
    protected function getComment($id)
112
    {
113
        $comment = $this->commentManager->find($id);
114
115
        if (null === $comment) {
116
            throw new NotFoundHttpException(sprintf('Comment (%d) not found', $id));
117
        }
118
119
        return $comment;
120
    }
121
}
122