Completed
Pull Request — master (#465)
by Marko
27:33
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
/*
4
 * This file is part of the Sonata Project package.
5
 *
6
 * (c) Thomas Rabaix <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sonata\NewsBundle\Action;
13
14
use Sonata\ClassificationBundle\Model\TagManagerInterface;
15
use Sonata\NewsBundle\Model\BlogInterface;
16
use Sonata\NewsBundle\Model\PostManagerInterface;
17
use Symfony\Component\HttpFoundation\Request;
18
use Symfony\Component\HttpFoundation\Response;
19
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
20
use Symfony\Component\Translation\TranslatorInterface;
21
22
final class TagPostArchiveAction extends AbstractPostArchiveAction
23
{
24
    /**
25
     * @var TagManagerInterface
26
     */
27
    private $tagManager;
28
29
    public function __construct(
30
        BlogInterface $blog,
31
        PostManagerInterface $postManager,
32
        TranslatorInterface $translator,
33
        TagManagerInterface $tagManager
34
    ) {
35
        parent::__construct($blog, $postManager, $translator);
36
37
        $this->tagManager = $tagManager;
38
    }
39
40
    /**
41
     * @param string $tag
42
     *
43
     * @return Response
44
     */
45
    public function __invoke(Request $request, $tag)
46
    {
47
        $tag = $this->tagManager->findOneBy([
48
            'slug' => $tag,
49
            'enabled' => true,
50
        ]);
51
52
        if (!$tag || !$tag->getEnabled()) {
53
            throw new NotFoundHttpException('Unable to find the tag');
54
        }
55
56
        if ($seoPage = $this->getSeoPage()) {
57
            $seoPage
58
                ->setTitle($this->trans('archive_tag.meta_title', [
59
                    '%title%' => $this->getBlog()->getTitle(),
60
                    '%tag%' => $tag->getName(),
61
                ]))
62
                ->addMeta('property', 'og:title', $this->trans('archive_tag.meta_title', [
63
                    '%title%' => $this->getBlog()->getTitle(),
64
                    '%tag%' => $tag->getName(),
65
                ]))
66
                ->addMeta('name', 'description', $this->trans('archive_tag.meta_description', [
67
                    '%title%' => $this->getBlog()->getTitle(),
68
                    '%tag%' => $tag->getName(),
69
                ]))
70
                ->addMeta('property', 'og:description', $this->trans('archive_tag.meta_description', [
71
                    '%title%' => $this->getBlog()->getTitle(),
72
                    '%tag%' => $tag->getName(),
73
                ]));
74
        }
75
76
        return $this->renderArchive($request, ['tag' => $tag->getSlug()], ['tag' => $tag]);
77
    }
78
}
79