Completed
Push — symfony3 ( 1143df...c57be8 )
by Kamil
18:42
created

TwigGridRenderer::render()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
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 Sylius\Bundle\ResourceBundle\Grid\Renderer;
13
14
use Sylius\Bundle\ResourceBundle\Grid\Parser\OptionsParserInterface;
15
use Sylius\Component\Grid\Definition\Action;
16
use Sylius\Component\Grid\Definition\Field;
17
use Sylius\Component\Grid\Definition\Filter;
18
use Sylius\Component\Grid\Renderer\GridRendererInterface;
19
use Sylius\Component\Grid\View\GridViewInterface;
20
21
/**
22
 * @author Grzegorz Sadowski <[email protected]>
23
 */
24
final class TwigGridRenderer implements GridRendererInterface
25
{
26
    /**
27
     * @var GridRendererInterface
28
     */
29
    private $gridRenderer;
30
31
    /**
32
     * @var \Twig_Environment
33
     */
34
    private $twig;
35
36
    /**
37
     * @var OptionsParserInterface
38
     */
39
    private $optionsParser;
40
41
    /**
42
     * @var array
43
     */
44
    private $actionTemplates;
45
46
    /**
47
     * @param GridRendererInterface $gridRenderer
48
     * @param \Twig_Environment $twig
49
     * @param OptionsParserInterface $optionsParser
50
     * @param array $actionTemplates
51
     */
52
    public function __construct(
53
        GridRendererInterface $gridRenderer,
54
        \Twig_Environment $twig,
55
        OptionsParserInterface $optionsParser,
56
        array $actionTemplates = []
57
    ) {
58
        $this->gridRenderer = $gridRenderer;
59
        $this->twig = $twig;
60
        $this->optionsParser = $optionsParser;
61
        $this->actionTemplates = $actionTemplates;
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67
    public function render(GridViewInterface $gridView, $template = null)
68
    {
69
        return $this->gridRenderer->render($gridView, $template);
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75
    public function renderField(GridViewInterface $gridView, Field $field, $data)
76
    {
77
        return $this->gridRenderer->renderField($gridView, $field, $data);
78
    }
79
80
    /**
81
     * {@inheritdoc}
82
     */
83
    public function renderAction(GridViewInterface $gridView, Action $action, $data = null)
84
    {
85
        $type = $action->getType();
86
        if (!isset($this->actionTemplates[$type])) {
87
            throw new \InvalidArgumentException(sprintf('Missing template for action type "%s".', $type));
88
        }
89
90
        $options = $this->optionsParser->parseOptions(
91
            $action->getOptions(),
92
            $gridView->getRequestConfiguration()->getRequest(),
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Sylius\Component\Grid\View\GridViewInterface as the method getRequestConfiguration() does only exist in the following implementations of said interface: Sylius\Bundle\ResourceBu...d\View\ResourceGridView.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
93
            $data
94
        );
95
96
        return $this->twig->render($this->actionTemplates[$type], [
97
            'grid' => $gridView,
98
            'action' => $action,
99
            'data' => $data,
100
            'options' => $options,
101
        ]);
102
    }
103
104
    /**
105
     * {@inheritdoc}
106
     */
107
    public function renderFilter(GridViewInterface $gridView, Filter $filter)
108
    {
109
        return $this->gridRenderer->renderFilter($gridView, $filter);
110
    }
111
}
112