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

ActionPerformer::perform()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 25
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 25
rs 8.8571
cc 2
eloc 17
nc 2
nop 4
1
<?php
2
3
namespace Psi\Component\Grid;
4
5
use Psi\Component\ObjectAgent\AgentInterface;
6
use Psi\Component\Grid\ActionRegistry;
7
use Psi\Component\Grid\Metadata\GridMetadata;
8
use Symfony\Component\OptionsResolver\OptionsResolver;
9
10
class ActionPerformer
11
{
12
    private $registry;
13
14
    public function __construct(ActionRegistry $registry)
15
    {
16
        $this->registry = $registry;
17
    }
18
19
    public function perform(
20
        AgentInterface $agent,
21
        GridMetadata $gridMetadata,
22
        string $actionName,
23
        array $selectedIdentifiers
24
    )
25
    {
26
        $actionMetadatas = $gridMetadata->getActions();
27
        if (!isset($actionMetadatas[$actionName])) {
28
            throw new \InvalidArgumentException(sprintf(
29
                'Action "%s" is not available for class "%s". Availabile actions: "%s"',
30
                $actionName, $gridMetadata->getClassMetadata()->name, implode('", "', array_keys($actionMetadatas))
31
            ));
32
        }
33
34
        $collection = $agent->findMany($selectedIdentifiers, $gridMetadata->getClassMetadata()->name);
0 ignored issues
show
Bug introduced by
The method findMany() does not seem to exist on object<Psi\Component\ObjectAgent\AgentInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
35
36
        $actionMetadata = $actionMetadatas[$actionName];
37
        $action = $this->registry->get($actionMetadata->getType());
38
        $options = new OptionsResolver();
39
        $action->configureOptions($options);
40
        $options = $options->resolve($actionMetadata->getOptions());
41
42
        $action->perform($agent, $collection, $options);
43
    }
44
}
45