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

CollectionPostArchiveAction::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 10
rs 9.9332
cc 1
nc 1
nop 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\CollectionManagerInterface;
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 CollectionPostArchiveAction extends AbstractPostArchiveAction
25
{
26
    /**
27
     * @var CollectionManagerInterface
28
     */
29
    private $collectionManager;
30
31
    public function __construct(
32
        BlogInterface $blog,
33
        PostManagerInterface $postManager,
34
        TranslatorInterface $translator,
35
        CollectionManagerInterface $collectionManager
36
    ) {
37
        parent::__construct($blog, $postManager, $translator);
38
39
        $this->collectionManager = $collectionManager;
40
    }
41
42
    /**
43
     * @param string $collection
44
     *
45
     * @return Response
46
     */
47
    public function __invoke(Request $request, $collection)
48
    {
49
        $collection = $this->collectionManager->findOneBy([
50
            'slug' => $collection,
51
            'enabled' => true,
52
        ]);
53
54
        if (!$collection || !$collection->getEnabled()) {
55
            throw new NotFoundHttpException('Unable to find the collection');
56
        }
57
58
        if ($seoPage = $this->getSeoPage()) {
59
            $seoPage
60
                ->setTitle($this->trans('archive_collection.meta_title', [
61
                    '%title%' => $this->getBlog()->getTitle(),
62
                    '%collection%' => $collection->getName(),
63
                ]))
64
                ->addMeta('property', 'og:title', $this->trans('archive_collection.meta_title', [
65
                    '%title%' => $this->getBlog()->getTitle(),
66
                    '%collection%' => $collection->getName(),
67
                ]))
68
                ->addMeta('name', 'description', $this->trans('archive_collection.meta_description', [
69
                    '%title%' => $this->getBlog()->getTitle(),
70
                    '%collection%' => $collection->getName(),
71
                    '%description%' => $collection->getDescription(),
72
                ]))
73
                ->addMeta('property', 'og:description', $this->trans('archive_collection.meta_description', [
74
                    '%title%' => $this->getBlog()->getTitle(),
75
                    '%collection%' => $collection->getName(),
76
                    '%description%' => $collection->getDescription(),
77
                ]));
78
        }
79
80
        return $this->renderArchive($request, ['collection' => $collection], ['collection' => $collection]);
81
    }
82
}
83