ActionPerformer   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 4
dl 0
loc 42
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A perform() 0 22 2
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