Completed
Pull Request — master (#8491)
by Stefan
18:30 queued 07:04
created

TwigGridRenderer::renderBulkAction()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 13
nc 2
nop 3
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
declare(strict_types=1);
13
14
namespace Sylius\Bundle\ResourceBundle\Grid\Renderer;
15
16
use Sylius\Bundle\ResourceBundle\Grid\Parser\OptionsParserInterface;
17
use Sylius\Component\Grid\Definition\Action;
18
use Sylius\Component\Grid\Definition\Field;
19
use Sylius\Component\Grid\Definition\Filter;
20
use Sylius\Component\Grid\Renderer\GridRendererInterface;
21
use Sylius\Component\Grid\View\GridViewInterface;
22
23
final class TwigGridRenderer implements GridRendererInterface
24
{
25
    /**
26
     * @var GridRendererInterface
27
     */
28
    private $gridRenderer;
29
30
    /**
31
     * @var \Twig_Environment
32
     */
33
    private $twig;
34
35
    /**
36
     * @var OptionsParserInterface
37
     */
38
    private $optionsParser;
39
40
    /**
41
     * @var array
42
     */
43
    private $actionTemplates;
44
45
    /**
46
     * @var array
47
     */
48
    private $bulkActionTemplates;
49
50
    /**
51
     * @param GridRendererInterface $gridRenderer
52
     * @param \Twig_Environment $twig
53
     * @param OptionsParserInterface $optionsParser
54
     * @param array $actionTemplates
55
     * @param array $bulkActionTemplates
56
     */
57
    public function __construct(
58
        GridRendererInterface $gridRenderer,
59
        \Twig_Environment $twig,
60
        OptionsParserInterface $optionsParser,
61
        array $actionTemplates = [],
62
        array $bulkActionTemplates = []
63
    ) {
64
        $this->gridRenderer = $gridRenderer;
65
        $this->twig = $twig;
66
        $this->optionsParser = $optionsParser;
67
        $this->actionTemplates = $actionTemplates;
68
        $this->bulkActionTemplates = $bulkActionTemplates;
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74
    public function render(GridViewInterface $gridView, ?string $template = null): string
75
    {
76
        return (string) $this->gridRenderer->render($gridView, $template);
77
    }
78
79
    /**
80
     * {@inheritdoc}
81
     */
82
    public function renderField(GridViewInterface $gridView, Field $field, $data): string
83
    {
84
        return (string) $this->gridRenderer->renderField($gridView, $field, $data);
85
    }
86
87
    /**
88
     * {@inheritdoc}
89
     */
90
    public function renderAction(GridViewInterface $gridView, Action $action, $data = null): string
91
    {
92
        $type = $action->getType();
93
        if (!isset($this->actionTemplates[$type])) {
94
            throw new \InvalidArgumentException(sprintf('Missing template for action type "%s".', $type));
95
        }
96
97
        $options = $this->optionsParser->parseOptions(
98
            $action->getOptions(),
99
            $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...
100
            $data
101
        );
102
103
        return (string) $this->twig->render($this->actionTemplates[$type], [
104
            'grid' => $gridView,
105
            'action' => $action,
106
            'data' => $data,
107
            'options' => $options,
108
        ]);
109
    }
110
111
    /**
112
     * {@inheritdoc}
113
     */
114
    public function renderBulkAction(GridViewInterface $gridView, Action $bulkAction, $data = null): string
115
    {
116
        $type = $bulkAction->getType();
117
        if (!isset($this->bulkActionTemplates[$type])) {
118
            throw new \InvalidArgumentException(sprintf('Missing template for bulk action type "%s".', $type));
119
        }
120
121
        $options = $this->optionsParser->parseOptions(
122
            $bulkAction->getOptions(),
123
            $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...
124
            $data
125
        );
126
127
        return (string) $this->twig->render($this->bulkActionTemplates[$type], [
128
            'grid' => $gridView,
129
            'action' => $bulkAction,
130
            'data' => $data,
131
            'options' => $options,
132
        ]);
133
    }
134
135
    /**
136
     * {@inheritdoc}
137
     */
138
    public function renderFilter(GridViewInterface $gridView, Filter $filter): string
139
    {
140
        return (string) $this->gridRenderer->renderFilter($gridView, $filter);
141
    }
142
}
143