RecentPostBlock::configureSettings()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Smart\ContentBundle\Admin\Block;
4
5
use Smart\ContentBundle\Entity\Repository\PostRepository;
6
use Sonata\BlockBundle\Block\BlockContextInterface;
7
use Sonata\BlockBundle\Block\Service\AbstractAdminBlockService;
8
use Symfony\Component\OptionsResolver\OptionsResolver;
9
use Symfony\Component\Templating\EngineInterface;
10
use Symfony\Component\HttpFoundation\Response;
11
12
/**
13
 * Nicolas Bastien <[email protected]>
14
 */
15
class RecentPostBlock extends AbstractAdminBlockService
0 ignored issues
show
Deprecated Code introduced by
The class Sonata\BlockBundle\Block...stractAdminBlockService has been deprecated with message: since sonata-project/block-bundle 3.16 without any replacement

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
16
{
17
    /**
18
     * @var PostRepository
19
     */
20
    private $postRepository;
21
22
    /**
23
     * @param string                $name
24
     * @param EngineInterface       $templating
25
     * @param PostRepository        $postRepository
26
     */
27
    public function __construct($name, EngineInterface $templating, PostRepository $postRepository)
28
    {
29
        parent::__construct($name, $templating);
30
31
        $this->postRepository = $postRepository;
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37
    public function configureSettings(OptionsResolver $resolver)
38
    {
39
        $resolver->setDefault('template', '@SmartContent/admin/block/recent_posts.html.twig');
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45
    public function execute(BlockContextInterface $blockContext, Response $response = null)
46
    {
47
        $date = $this->postRepository->getLastPublishedPostDate();
48
        $today = new \DateTime();
49
50
        $interval = 0;
51
        if (null !== $date) {
52
            $interval = $today->diff($date)->days;
53
        }
54
55
        return $this->renderPrivateResponse(
56
            $blockContext->getTemplate(),
57
            [
58
                'block'        => $blockContext->getBlock(),
59
                'settings'     => $blockContext->getSettings(),
60
                'posts'        => $this->postRepository->getLastPublishedPosts(),
61
                'interval'    => $interval
62
            ],
63
            $response
64
        );
65
    }
66
}
67