Passed
Push — master ( 624aae...ea09e1 )
by
unknown
18:37 queued 08:54
created

CStudentPublicationDeleteProcessor::process()   B

Complexity

Conditions 8
Paths 111

Size

Total Lines 31
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 8
eloc 19
nc 111
nop 4
dl 0
loc 31
rs 8.3527
c 1
b 0
f 0
1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
declare(strict_types=1);
6
7
namespace Chamilo\CoreBundle\State;
8
9
use ApiPlatform\Metadata\Operation;
10
use ApiPlatform\State\ProcessorInterface;
11
use Chamilo\CourseBundle\Entity\CStudentPublication;
12
use Chamilo\CoreBundle\Entity\ResourceNode;
13
use Doctrine\ORM\EntityManagerInterface;
14
15
/**
16
 * @implements ProcessorInterface<CStudentPublication, void>
17
 */
18
final class CStudentPublicationDeleteProcessor implements ProcessorInterface
19
{
20
    public function __construct(private readonly EntityManagerInterface $em) {}
21
22
    public function process($data, Operation $operation, array $uriVariables = [], array $context = []): void
23
    {
24
        if (!$data instanceof CStudentPublication) {
25
            return;
26
        }
27
28
        $node = $data->hasResourceNode() ? $data->getResourceNode() : null;
29
30
        $this->em->beginTransaction();
31
        try {
32
            try { $this->em->refresh($data); } catch (\Throwable) {}
0 ignored issues
show
introduced by
Consider moving this CATCH statement to a new line.
Loading history...
33
34
            $this->em->remove($data);
35
            $this->em->flush();
36
37
            if ($node instanceof ResourceNode) {
38
                foreach ($node->getResourceLinks() as $link) {
39
                    $this->em->remove($link);
40
                }
41
                $this->em->flush();
42
43
                foreach ($node->getResourceFiles() as $file) {
44
                    $this->em->remove($file);
45
                }
46
                $this->em->flush();
47
            }
48
49
            $this->em->commit();
50
        } catch (\Throwable $e) {
51
            $this->em->rollback();
52
            throw $e;
53
        }
54
    }
55
}
56