DeleteAction::configureOptions()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
eloc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Psi\Component\Grid\Action;
6
7
use Psi\Component\Grid\ActionInterface;
8
use Psi\Component\Grid\ActionResponse;
9
use Psi\Component\Grid\ActionResponseInterface;
10
use Psi\Component\ObjectAgent\AgentInterface;
11
use Symfony\Component\OptionsResolver\OptionsResolver;
12
13
class DeleteAction implements ActionInterface
14
{
15
    public function perform(AgentInterface $agent, string $classFqn, array $identifiers, array $options): ActionResponseInterface
16
    {
17
        $collection = $agent->findMany($identifiers, $classFqn);
18
19
        $errors = [];
20
        $affected = count($collection);
21
22
        try {
23
            foreach ($collection as $object) {
24
                $agent->remove($object);
25
            }
26
            $agent->flush();
27
        } catch (\Exception $e) {
28
            $errors[] = $e->getMessage();
29
            $affected = 0;
30
        }
31
32
        return ActionResponse::create([
33
            'errors' => $errors,
34
            'affected' => $affected,
35
        ]);
36
    }
37
38
    public function configureOptions(OptionsResolver $options)
39
    {
40
    }
41
}
42