Completed
Push — 3.x-dev-kit ( 21be5a )
by
unknown
03:10
created

RecentPostsBlockService   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 7
Bugs 3 Features 0
Metric Value
wmc 8
c 7
b 3
f 0
lcom 1
cbo 6
dl 0
loc 99
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 2
A getBlockMetadata() 0 6 2
A execute() 0 20 2
A buildEditForm() 0 23 1
A configureSettings() 0 9 1
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\Block;
13
14
use Sonata\AdminBundle\Admin\Pool;
15
use Sonata\AdminBundle\Form\FormMapper;
16
use Sonata\BlockBundle\Block\BaseBlockService;
17
use Sonata\BlockBundle\Block\BlockContextInterface;
18
use Sonata\BlockBundle\Model\BlockInterface;
19
use Sonata\CoreBundle\Model\ManagerInterface;
20
use Sonata\CoreBundle\Model\Metadata;
21
use Sonata\NewsBundle\Model\PostManagerInterface;
22
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
23
use Symfony\Component\HttpFoundation\Response;
24
use Symfony\Component\OptionsResolver\OptionsResolver;
25
26
/**
27
 * @author     Thomas Rabaix <[email protected]>
28
 */
29
class RecentPostsBlockService extends BaseBlockService
30
{
31
    /**
32
     * @var PostManagerInterface
33
     */
34
    protected $manager;
35
36
    /**
37
     * @param string           $name
38
     * @param EngineInterface  $templating
39
     * @param ManagerInterface $postManager
40
     * @param Pool             $adminPool
41
     */
42
    public function __construct($name, EngineInterface $templating, ManagerInterface $postManager, Pool $adminPool = null)
43
    {
44
        if (!$postManager instanceof PostManagerInterface) {
45
            @trigger_error('Calling the '.__METHOD__.' method with a Sonata\CoreBundle\Model\ManagerInterface is deprecated since version 2.4 and will be removed in 3.0. Use the new signature with a Sonata\NewsBundle\Model\PostManagerInterface instead.', E_USER_DEPRECATED);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
46
        }
47
48
        $this->manager = $postManager;
0 ignored issues
show
Documentation Bug introduced by
$postManager is of type object<Sonata\CoreBundle\Model\ManagerInterface>, but the property $manager was declared to be of type object<Sonata\NewsBundle...l\PostManagerInterface>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
49
        $this->adminPool = $adminPool;
0 ignored issues
show
Bug introduced by
The property adminPool does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
50
51
        parent::__construct($name, $templating);
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57
    public function execute(BlockContextInterface $blockContext, Response $response = null)
58
    {
59
        $criteria = array(
60
            'mode' => $blockContext->getSetting('mode'),
61
        );
62
63
        $parameters = array(
64
            'context' => $blockContext,
65
            'settings' => $blockContext->getSettings(),
66
            'block' => $blockContext->getBlock(),
67
            'pager' => $this->manager->getPager($criteria, 1, $blockContext->getSetting('number')),
68
            'admin_pool' => $this->adminPool,
69
        );
70
71
        if ($blockContext->getSetting('mode') === 'admin') {
72
            return $this->renderPrivateResponse($blockContext->getTemplate(), $parameters, $response);
73
        }
74
75
        return $this->renderResponse($blockContext->getTemplate(), $parameters, $response);
76
    }
77
78
    /**
79
     * {@inheritdoc}
80
     */
81
    public function buildEditForm(FormMapper $formMapper, BlockInterface $block)
82
    {
83
        $formMapper->add('settings', 'sonata_type_immutable_array', array(
84
            'keys' => array(
85
                array('number', 'integer', array(
86
                    'required' => true,
87
                    'label' => 'form.label_number',
88
                )),
89
                array('title', 'text', array(
90
                    'required' => false,
91
                    'label' => 'form.label_title',
92
                )),
93
                array('mode', 'choice', array(
94
                    'choices' => array(
95
                        'public' => 'form.label_mode_public',
96
                        'admin' => 'form.label_mode_admin',
97
                    ),
98
                    'label' => 'form.label_mode',
99
                )),
100
            ),
101
            'translation_domain' => 'SonataNewsBundle',
102
        ));
103
    }
104
105
    /**
106
     * {@inheritdoc}
107
     */
108
    public function configureSettings(OptionsResolver $resolver)
109
    {
110
        $resolver->setDefaults(array(
111
            'number' => 5,
112
            'mode' => 'public',
113
            'title' => 'Recent Posts',
114
            'template' => 'SonataNewsBundle:Block:recent_posts.html.twig',
115
        ));
116
    }
117
118
    /**
119
     * {@inheritdoc}
120
     */
121
    public function getBlockMetadata($code = null)
122
    {
123
        return new Metadata($this->getName(), (!is_null($code) ? $code : $this->getName()), false, 'SonataNewsBundle', array(
124
            'class' => 'fa fa-pencil',
125
        ));
126
    }
127
}
128