VideosAction   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Importance

Changes 0
Metric Value
dl 0
loc 60
rs 10
c 0
b 0
f 0
wmc 5
lcom 0
cbo 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A __invoke() 0 20 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Web\Action;
6
7
use Article\Entity\ArticleType;
8
use Article\Service\VideoService;
9
use Category\Service\CategoryService;
10
use Psr\Http\Message\ResponseInterface as Response;
11
use Psr\Http\Message\ServerRequestInterface as Request;
12
use Zend\Diactoros\Response\HtmlResponse;
13
use Zend\Expressive\Template\TemplateRendererInterface as Template;
14
15
/**
16
 * Class VideosAction.
17
 */
18
class VideosAction
19
{
20
    /** @var Template */
21
    private $template;
22
23
    /** @var VideoService */
24
    private $videoService;
25
26
    /** @var CategoryService */
27
    private $categoryService;
28
29
    /**
30
     * VideosAction constructor.
31
     *
32
     * @param Template        $template
33
     * @param VideoService    $videoService
34
     * @param CategoryService $categoryService
35
     */
36
    public function __construct(
37
        Template $template,
38
        VideoService $videoService,
39
        CategoryService $categoryService
40
    ) {
41
        $this->template = $template;
42
        $this->videoService = $videoService;
43
        $this->categoryService = $categoryService;
44
    }
45
46
    /**
47
     * Executed when action is invoked.
48
     *
49
     * @param Request       $request
50
     * @param Response      $response
51
     * @param callable|null $next
52
     *
53
     * @throws \Exception
54
     *
55
     * @return HtmlResponse
56
     */
57
    public function __invoke(
58
        Request $request,
59
        Response $response,
60
        callable $next = null
61
    ) {
62
        $params = $request->getQueryParams();
63
        $page = isset($params['page']) ? $params['page'] : 1;
64
        $urlSlug = $request->getAttribute('category');
65
        $category = $this->categoryService->getCategoryBySlug($urlSlug);
66
67
        if (!$category || $category->type != ArticleType::VIDEO) {
68
            return $next($request, $response);
69
        }
70
71
        return new HtmlResponse($this->template->render('web::videos', [
72
            'layout'   => 'layout/web',
73
            'videos'   => $this->videoService->fetchWebArticles($page, 5),
74
            'category' => $category,
75
        ]));
76
    }
77
}
78