1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Web\Action; |
6
|
|
|
|
7
|
|
|
use Article\Entity\ArticleType; |
8
|
|
|
use Article\Service\EventService; |
9
|
|
|
use Category\Service\CategoryService; |
10
|
|
|
use Meetup\MeetupApiService; |
11
|
|
|
use Psr\Http\Message\ResponseInterface as Response; |
12
|
|
|
use Psr\Http\Message\ServerRequestInterface as Request; |
13
|
|
|
use Zend\Diactoros\Response\HtmlResponse; |
14
|
|
|
use Zend\Expressive\Template\TemplateRendererInterface as Template; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class EventAction. |
18
|
|
|
*/ |
19
|
|
|
class EventAction |
20
|
|
|
{ |
21
|
|
|
/** @var Template */ |
22
|
|
|
private $template; |
23
|
|
|
|
24
|
|
|
/** @var EventService */ |
25
|
|
|
private $eventService; |
26
|
|
|
|
27
|
|
|
/** @var CategoryService */ |
28
|
|
|
private $categoryService; |
29
|
|
|
|
30
|
|
|
/** @var MeetupApiService */ |
31
|
|
|
private $meetupService; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* EventAction constructor. |
35
|
|
|
* |
36
|
|
|
* @param Template $template |
37
|
|
|
* @param EventService $eventService |
38
|
|
|
* @param CategoryService $categoryService |
39
|
|
|
* @param MeetupApiService $meetupService |
40
|
|
|
*/ |
41
|
|
|
public function __construct( |
42
|
|
|
Template $template, |
43
|
|
|
EventService $eventService, |
44
|
|
|
CategoryService $categoryService, |
45
|
|
|
MeetupApiService $meetupService |
46
|
|
|
) { |
47
|
|
|
$this->template = $template; |
48
|
|
|
$this->eventService = $eventService; |
49
|
|
|
$this->categoryService = $categoryService; |
50
|
|
|
$this->meetupService = $meetupService; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Executed when action is invoked. |
55
|
|
|
* |
56
|
|
|
* @param Request $request |
57
|
|
|
* @param Response $response |
58
|
|
|
* @param callable|null $next |
59
|
|
|
* |
60
|
|
|
* @throws \Exception |
61
|
|
|
* |
62
|
|
|
* @return HtmlResponse |
63
|
|
|
*/ |
64
|
|
View Code Duplication |
public function __invoke( |
|
|
|
|
65
|
|
|
Request $request, |
66
|
|
|
Response $response, |
67
|
|
|
callable $next = null |
68
|
|
|
) { |
69
|
|
|
$categorySlug = $request->getAttribute('segment_1'); |
|
|
|
|
70
|
|
|
$eventSlug = $request->getAttribute('segment_2'); |
71
|
|
|
$event = $this->eventService->fetchEventBySlug($eventSlug); |
72
|
|
|
|
73
|
|
|
if (!$event || $event->type != ArticleType::EVENT) { |
74
|
|
|
return $next($request, $response); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
// Fetch going ppl |
78
|
|
|
$attendees = $this->meetupService->getAttendees($event->event_url); |
79
|
|
|
|
80
|
|
|
return new HtmlResponse($this->template->render('web::event', [ |
81
|
|
|
'layout' => 'layout/web', |
82
|
|
|
'event' => $event, |
83
|
|
|
'attendees' => $attendees, |
84
|
|
|
])); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
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.