Completed
Push — master ( 37876c...34238f )
by Marko
9s
created

TagPostArchiveAction   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
A __invoke() 0 33 4
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\Action;
15
16
use Sonata\ClassificationBundle\Model\TagManagerInterface;
17
use Sonata\NewsBundle\Model\BlogInterface;
18
use Sonata\NewsBundle\Model\PostManagerInterface;
19
use Symfony\Component\HttpFoundation\Request;
20
use Symfony\Component\HttpFoundation\Response;
21
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
22
use Symfony\Component\Translation\TranslatorInterface;
23
24
final class TagPostArchiveAction extends AbstractPostArchiveAction
25
{
26
    /**
27
     * @var TagManagerInterface
28
     */
29
    private $tagManager;
30
31
    public function __construct(
32
        BlogInterface $blog,
33
        PostManagerInterface $postManager,
34
        TranslatorInterface $translator,
35
        TagManagerInterface $tagManager
36
    ) {
37
        parent::__construct($blog, $postManager, $translator);
38
39
        $this->tagManager = $tagManager;
40
    }
41
42
    /**
43
     * @param string $tag
44
     *
45
     * @return Response
46
     */
47
    public function __invoke(Request $request, $tag)
48
    {
49
        $tag = $this->tagManager->findOneBy([
50
            'slug' => $tag,
51
            'enabled' => true,
52
        ]);
53
54
        if (!$tag || !$tag->getEnabled()) {
55
            throw new NotFoundHttpException('Unable to find the tag');
56
        }
57
58
        if ($seoPage = $this->getSeoPage()) {
59
            $seoPage
60
                ->setTitle($this->trans('archive_tag.meta_title', [
61
                    '%title%' => $this->getBlog()->getTitle(),
62
                    '%tag%' => $tag->getName(),
63
                ]))
64
                ->addMeta('property', 'og:title', $this->trans('archive_tag.meta_title', [
65
                    '%title%' => $this->getBlog()->getTitle(),
66
                    '%tag%' => $tag->getName(),
67
                ]))
68
                ->addMeta('name', 'description', $this->trans('archive_tag.meta_description', [
69
                    '%title%' => $this->getBlog()->getTitle(),
70
                    '%tag%' => $tag->getName(),
71
                ]))
72
                ->addMeta('property', 'og:description', $this->trans('archive_tag.meta_description', [
73
                    '%title%' => $this->getBlog()->getTitle(),
74
                    '%tag%' => $tag->getName(),
75
                ]));
76
        }
77
78
        return $this->renderArchive($request, ['tag' => $tag->getSlug()], ['tag' => $tag]);
79
    }
80
}
81