Completed
Pull Request — master (#44)
by Daniel
01:56
created

ActionPerformer::perform()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 23
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 9.0856
c 0
b 0
f 0
cc 2
eloc 17
nc 2
nop 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Psi\Component\Grid;
6
7
use Psi\Component\Grid\Metadata\GridMetadata;
8
use Psi\Component\ObjectAgent\AgentInterface;
9
use Symfony\Component\OptionsResolver\OptionsResolver;
10
use Psi\Component\Grid\ActionResponseInterface;
11
12
class ActionPerformer
13
{
14
    private $registry;
15
16
    public function __construct(ActionRegistry $registry)
17
    {
18
        $this->registry = $registry;
19
    }
20
21
    /**
22
     * Retrieve a collection from the given agent using the given metadata and
23
     * identifiers and perform the named action on each member of the
24
     * collection.
25
     *
26
     * It should be assumed that the storage will be flushed by the action
27
     * which is executed.
28
     *
29
     * @throws \InvalidArgumentException If the action is not available.
30
     */
31
    public function perform(
32
        AgentInterface $agent,
33
        GridMetadata $gridMetadata,
34
        string $actionName,
35
        array $selectedIdentifiers
36
    ): ActionResponseInterface
37
    {
38
        $actionMetadatas = $gridMetadata->getActions();
39
        if (!isset($actionMetadatas[$actionName])) {
40
            throw new \InvalidArgumentException(sprintf(
41
                'Action "%s" is not available for class "%s". Availabile actions: "%s"',
42
                $actionName, $gridMetadata->getClassMetadata()->name, implode('", "', array_keys($actionMetadatas))
43
            ));
44
        }
45
46
        $actionMetadata = $actionMetadatas[$actionName];
47
        $action = $this->registry->get($actionMetadata->getType());
48
        $options = new OptionsResolver();
49
        $action->configureOptions($options);
50
        $options = $options->resolve($actionMetadata->getOptions());
51
52
        return $action->perform($agent, $gridMetadata->getClassMetadata()->name, $selectedIdentifiers, $options);
53
    }
54
}
55