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
|
|
|
|
10
|
|
|
class Grid |
11
|
|
|
{ |
12
|
|
|
private $agent; |
13
|
|
|
private $gridContext; |
14
|
|
|
private $gridMetadata; |
15
|
|
|
private $gridViewFactory; |
16
|
|
|
private $actionPerformer; |
17
|
|
|
|
18
|
|
|
public function __construct( |
19
|
|
|
GridViewFactory $gridViewFactory, |
20
|
|
|
ActionPerformer $actionPerformer, |
21
|
|
|
AgentInterface $agent, |
22
|
|
|
GridContext $gridContext, |
23
|
|
|
GridMetadata $gridMetadata |
24
|
|
|
) { |
25
|
|
|
$this->agent = $agent; |
26
|
|
|
$this->gridContext = $gridContext; |
27
|
|
|
$this->gridMetadata = $gridMetadata; |
28
|
|
|
$this->gridViewFactory = $gridViewFactory; |
29
|
|
|
$this->actionPerformer = $actionPerformer; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function createView() |
33
|
|
|
{ |
34
|
|
|
return $this->gridViewFactory->createView( |
35
|
|
|
$this->agent, |
36
|
|
|
$this->gridContext, |
37
|
|
|
$this->gridMetadata |
38
|
|
|
); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function performActionFromPostData(array $postData) |
42
|
|
|
{ |
43
|
|
|
$valid = [ |
44
|
|
|
ActionBar::INPUT_NAME, |
45
|
|
|
SelectView::INPUT_NAME, |
46
|
|
|
]; |
47
|
|
|
|
48
|
|
View Code Duplication |
if ($diff = array_diff(array_keys($postData), $valid)) { |
|
|
|
|
49
|
|
|
throw new \InvalidArgumentException(sprintf( |
50
|
|
|
'Unexpected keys in POST: "%s", valid keys: "%s"', |
51
|
|
|
implode('", "', $diff), implode('", "', $valid) |
52
|
|
|
)); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
if (!isset($postData[ActionBar::INPUT_NAME])) { |
56
|
|
|
throw new \InvalidArgumentException(sprintf( |
57
|
|
|
'Expected action to be in post with key "%s", but it was not there.', |
58
|
|
|
ActionBar::INPUT_NAME |
59
|
|
|
)); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
if (!isset($postData[SelectView::INPUT_NAME])) { |
63
|
|
|
return; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$actionName = $postData[ActionBar::INPUT_NAME]; |
67
|
|
|
$selectedIdentifiers = array_keys($postData[SelectView::INPUT_NAME]); |
68
|
|
|
|
69
|
|
|
$this->performAction($actionName, $selectedIdentifiers); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function performAction(string $actionName, array $selectedIdentifiers) |
73
|
|
|
{ |
74
|
|
|
$this->actionPerformer->perform( |
75
|
|
|
$this->agent, |
76
|
|
|
$this->gridMetadata, |
77
|
|
|
$actionName, |
78
|
|
|
$selectedIdentifiers |
79
|
|
|
); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
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.