ActionPerformer::perform()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 22
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

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