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]; |
|
|
|
|
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
|
|
|
|
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:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.