Completed
Pull Request — master (#21)
by Daniel
02:36
created

Grid::performActionFromPostData()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 11

Duplication

Lines 6
Ratio 31.58 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 6
loc 19
rs 9.4285
cc 2
eloc 11
nc 2
nop 1
1
<?php
2
3
namespace Psi\Component\Grid;
4
5
use Psi\Component\Grid\Cell\View\SelectView;
6
use Psi\Component\Grid\Metadata\GridMetadata;
7
use Psi\Component\Grid\View\ActionBar;
8
use Psi\Component\ObjectAgent\AgentInterface;
9
use Psi\Component\ObjectAgent\Query\Query;
10
11
class Grid
12
{
13
    private $agent;
14
    private $gridContext;
15
    private $gridMetadata;
16
    private $gridViewFactory;
17
    private $actionPerformer;
18
19
    public function __construct(
20
        GridViewFactory $gridViewFactory,
21
        ActionPerformer $actionPerformer,
22
        AgentInterface $agent,
23
        GridContext $gridContext,
24
        GridMetadata $gridMetadata
25
    ) {
26
        $this->agent = $agent;
27
        $this->gridContext = $gridContext;
28
        $this->gridMetadata = $gridMetadata;
29
        $this->gridViewFactory = $gridViewFactory;
30
        $this->actionPerformer = $actionPerformer;
31
    }
32
33
    public function createView()
34
    {
35
        return $this->gridViewFactory->createView(
36
            $this->agent,
37
            $this->gridContext,
38
            $this->gridMetadata
39
        );
40
    }
41
42
    public function performActionFromPostData(array $postData)
43
    {
44
        $required = [
45
            ActionBar::INPUT_NAME,
46
            SelectView::INPUT_NAME,
47
        ];
48
49 View Code Duplication
        if (array_diff($required, array_keys($postData))) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
50
            throw new \InvalidArgumentException(sprintf(
51
                'Expected all keys "%s" in post data, but (only) got "%s"',
52
                implode('", "', $required), implode('", "', array_keys($postData))
53
            ));
54
        }
55
56
        $actionName = $postData[ActionBar::INPUT_NAME];
57
        $selectedIdentifiers = array_keys($postData[SelectView::INPUT_NAME]);
58
59
        $this->performAction($actionName, $selectedIdentifiers);
60
    }
61
62
    public function performAction(string $actionName, array $selectedIdentifiers)
63
    {
64
        $this->actionPerformer->perform(
65
            $this->agent,
66
            $this->gridMetadata,
67
            $actionName,
68
            $selectedIdentifiers
69
        );
70
    }
71
}
72