1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Psi\Component\Grid; |
6
|
|
|
|
7
|
|
|
use Psi\Component\Grid\Column\SelectColumn; |
8
|
|
|
use Psi\Component\Grid\Metadata\GridMetadata; |
9
|
|
|
use Psi\Component\Grid\View\ActionBar; |
10
|
|
|
use Psi\Component\ObjectAgent\AgentInterface; |
11
|
|
|
|
12
|
|
|
class Grid |
13
|
|
|
{ |
14
|
|
|
const DEFAULT_VARIANT = 'main'; |
15
|
|
|
const DEFAULT_GROUP = 'main'; |
16
|
|
|
|
17
|
|
|
private $agent; |
18
|
|
|
private $gridContext; |
19
|
|
|
private $gridMetadata; |
20
|
|
|
private $gridViewFactory; |
21
|
|
|
private $actionPerformer; |
22
|
|
|
|
23
|
|
|
public function __construct( |
24
|
|
|
GridViewFactory $gridViewFactory, |
25
|
|
|
ActionPerformer $actionPerformer, |
26
|
|
|
AgentInterface $agent, |
27
|
|
|
GridContext $gridContext, |
28
|
|
|
GridMetadata $gridMetadata |
29
|
|
|
) { |
30
|
|
|
$this->agent = $agent; |
31
|
|
|
$this->gridContext = $gridContext; |
32
|
|
|
$this->gridMetadata = $gridMetadata; |
33
|
|
|
$this->gridViewFactory = $gridViewFactory; |
34
|
|
|
$this->actionPerformer = $actionPerformer; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function createView() |
38
|
|
|
{ |
39
|
|
|
return $this->gridViewFactory->createView( |
40
|
|
|
$this->agent, |
41
|
|
|
$this->gridContext, |
42
|
|
|
$this->gridMetadata |
43
|
|
|
); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function performActionFromPostData(array $postData): ActionResponseInterface |
47
|
|
|
{ |
48
|
|
|
$valid = [ |
49
|
|
|
ActionBar::INPUT_NAME, |
50
|
|
|
SelectColumn::INPUT_NAME, |
51
|
|
|
]; |
52
|
|
|
|
53
|
|
View Code Duplication |
if ($diff = array_diff(array_keys($postData), $valid)) { |
|
|
|
|
54
|
|
|
throw new \InvalidArgumentException(sprintf( |
55
|
|
|
'Unexpected keys in POST: "%s", valid keys: "%s"', |
56
|
|
|
implode('", "', $diff), implode('", "', $valid) |
57
|
|
|
)); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
if (!isset($postData[ActionBar::INPUT_NAME])) { |
61
|
|
|
throw new \InvalidArgumentException(sprintf( |
62
|
|
|
'Expected action to be in post with key "%s", but it was not there.', |
63
|
|
|
ActionBar::INPUT_NAME |
64
|
|
|
)); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
// allow empty submissions |
68
|
|
|
if (!isset($postData[SelectColumn::INPUT_NAME])) { |
69
|
|
|
$postData[SelectColumn::INPUT_NAME] = []; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
$actionName = $postData[ActionBar::INPUT_NAME]; |
73
|
|
|
$selectData = $postData[SelectColumn::INPUT_NAME]; |
74
|
|
|
|
75
|
|
|
if (!is_array($selectData)) { |
76
|
|
|
throw new \InvalidArgumentException(sprintf( |
77
|
|
|
'Expected data from select column to be an array, but got "%s"', |
78
|
|
|
gettype($selectData) |
79
|
|
|
)); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
$selectedIdentifiers = array_keys($selectData); |
83
|
|
|
|
84
|
|
|
return $this->performAction($actionName, $selectedIdentifiers); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function performAction(string $actionName, array $selectedIdentifiers): ActionResponseInterface |
88
|
|
|
{ |
89
|
|
|
return $this->actionPerformer->perform( |
90
|
|
|
$this->agent, |
91
|
|
|
$this->gridMetadata, |
92
|
|
|
$actionName, |
93
|
|
|
$selectedIdentifiers |
94
|
|
|
); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
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.