|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Web\Action; |
|
6
|
|
|
|
|
7
|
|
|
use Article\Entity\ArticleType; |
|
8
|
|
|
use Article\Service\PostService; |
|
9
|
|
|
use Psr\Http\Message\ResponseInterface as Response; |
|
10
|
|
|
use Psr\Http\Message\ServerRequestInterface as Request; |
|
11
|
|
|
use Zend\Diactoros\Response\HtmlResponse; |
|
12
|
|
|
use Zend\Expressive\Template\TemplateRendererInterface as Template; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Class PostAction. |
|
16
|
|
|
*/ |
|
17
|
|
|
class PostAction |
|
18
|
|
|
{ |
|
19
|
|
|
/** @var Template */ |
|
20
|
|
|
private $template; |
|
21
|
|
|
|
|
22
|
|
|
/** @var PostService */ |
|
23
|
|
|
private $postService; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* PostAction constructor. |
|
27
|
|
|
* |
|
28
|
|
|
* @param Template $template |
|
29
|
|
|
*/ |
|
30
|
|
|
public function __construct(Template $template, PostService $postService) |
|
31
|
|
|
{ |
|
32
|
|
|
$this->template = $template; |
|
33
|
|
|
$this->postService = $postService; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Executed when action is invoked. |
|
38
|
|
|
* |
|
39
|
|
|
* @param Request $request |
|
40
|
|
|
* @param Response $response |
|
41
|
|
|
* @param callable|null $next |
|
42
|
|
|
* |
|
43
|
|
|
* @throws \Exception |
|
44
|
|
|
* |
|
45
|
|
|
* @return HtmlResponse |
|
46
|
|
|
*/ |
|
47
|
|
View Code Duplication |
public function __invoke( |
|
|
|
|
|
|
48
|
|
|
Request $request, |
|
49
|
|
|
Response $response, |
|
50
|
|
|
callable $next = null |
|
51
|
|
|
) { |
|
52
|
|
|
$categorySlug = $request->getAttribute('segment_1'); |
|
|
|
|
|
|
53
|
|
|
$postSlug = $request->getAttribute('segment_2'); |
|
54
|
|
|
$post = $this->postService->fetchSingleArticleBySlug($postSlug); |
|
55
|
|
|
|
|
56
|
|
|
if (!$post || $post->type != ArticleType::POST) { |
|
57
|
|
|
return $next($request, $response); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
list($previousPost, $nextPost) = $this->postService->fetchNearestArticle($post->published_at); |
|
61
|
|
|
|
|
62
|
|
|
return new HtmlResponse($this->template->render('web::post', [ |
|
63
|
|
|
'layout' => 'layout/web', |
|
64
|
|
|
'post' => $post, |
|
65
|
|
|
'previous' => $previousPost, |
|
66
|
|
|
'next' => $nextPost, |
|
67
|
|
|
])); |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.