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

AbstractPostArchiveAction   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 6

Importance

Changes 0
Metric Value
wmc 8
lcom 2
cbo 6
dl 0
loc 108
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A renderArchive() 0 27 2
A setSeoPage() 0 4 1
A trans() 0 4 1
A getBlog() 0 4 1
A getSeoPage() 0 4 1
A getPostManager() 0 4 1
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\NewsBundle\Model\BlogInterface;
17
use Sonata\NewsBundle\Model\PostManagerInterface;
18
use Sonata\SeoBundle\Seo\SeoPageInterface;
19
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
20
use Symfony\Component\HttpFoundation\Request;
21
use Symfony\Component\HttpFoundation\Response;
22
use Symfony\Component\Translation\TranslatorInterface;
23
24
abstract class AbstractPostArchiveAction extends Controller
25
{
26
    /**
27
     * @var BlogInterface
28
     */
29
    private $blog;
30
31
    /**
32
     * @var PostManagerInterface
33
     */
34
    private $postManager;
35
36
    /**
37
     * @var TranslatorInterface
38
     */
39
    private $translator;
40
41
    /**
42
     * @var SeoPageInterface|null
43
     */
44
    private $seoPage;
45
46
    public function __construct(BlogInterface $blog, PostManagerInterface $postManager, TranslatorInterface $translator)
47
    {
48
        $this->blog = $blog;
49
        $this->postManager = $postManager;
50
        $this->translator = $translator;
51
    }
52
53
    /**
54
     * @internal
55
     *
56
     * NEXT_MAJOR: make this method protected
57
     *
58
     * @return Response
59
     */
60
    final public function renderArchive(Request $request, array $criteria = [], array $parameters = [])
61
    {
62
        $pager = $this->postManager->getPager(
63
            $criteria,
64
            $request->get('page', 1)
65
        );
66
67
        $parameters = array_merge([
68
            'pager' => $pager,
69
            'blog' => $this->blog,
70
            'tag' => false,
71
            'collection' => false,
72
            'route' => $request->get('_route'),
73
            'route_parameters' => $request->get('_route_params'),
74
        ], $parameters);
75
76
        $response = $this->render(
77
            sprintf('@SonataNews/Post/archive.%s.twig', $request->getRequestFormat()),
78
            $parameters
79
        );
80
81
        if ('rss' === $request->getRequestFormat()) {
82
            $response->headers->set('Content-Type', 'application/rss+xml');
83
        }
84
85
        return $response;
86
    }
87
88
    /**
89
     * @param null|SeoPageInterface $seoPage
90
     */
91
    public function setSeoPage(SeoPageInterface $seoPage = null): void
92
    {
93
        $this->seoPage = $seoPage;
94
    }
95
96
    /**
97
     * @param string      $id
98
     * @param string|null $domain
99
     * @param string|null $locale
100
     *
101
     * @return string
102
     */
103
    final protected function trans($id, array $parameters = [], $domain = 'SonataNewsBundle', $locale = null)
104
    {
105
        return $this->translator->trans($id, $parameters, $domain, $locale);
106
    }
107
108
    /**
109
     * @return BlogInterface
110
     */
111
    final protected function getBlog()
112
    {
113
        return $this->blog;
114
    }
115
116
    /**
117
     * @return null|SeoPageInterface
118
     */
119
    final protected function getSeoPage()
120
    {
121
        return $this->seoPage;
122
    }
123
124
    /**
125
     * @return PostManagerInterface
126
     */
127
    final protected function getPostManager()
128
    {
129
        return $this->postManager;
130
    }
131
}
132