|
8 | 8 |
|
9 | 9 | use ApiPlatform\Metadata\Operation; |
10 | 10 | use ApiPlatform\State\ProcessorInterface; |
11 | | -use Chamilo\CoreBundle\Repository\TrackEDefaultRepository; |
12 | 11 | use Chamilo\CourseBundle\Entity\CStudentPublication; |
| 12 | +use Chamilo\CoreBundle\Entity\ResourceNode; |
13 | 13 | use Doctrine\ORM\EntityManagerInterface; |
14 | 14 |
|
15 | 15 | /** |
16 | 16 | * @implements ProcessorInterface<CStudentPublication, void> |
17 | 17 | */ |
18 | 18 | final class CStudentPublicationDeleteProcessor implements ProcessorInterface |
19 | 19 | { |
20 | | - public function __construct( |
21 | | - private readonly EntityManagerInterface $entityManager, |
22 | | - private readonly TrackEDefaultRepository $trackRepo, |
23 | | - ) {} |
24 | | - |
25 | | - public function process( |
26 | | - $data, |
27 | | - Operation $operation, |
28 | | - array $uriVariables = [], |
29 | | - array $context = [] |
30 | | - ): void { |
| 20 | + public function __construct(private readonly EntityManagerInterface $em) {} |
| 21 | + |
| 22 | + public function process($data, Operation $operation, array $uriVariables = [], array $context = []): void |
| 23 | + { |
31 | 24 | if (!$data instanceof CStudentPublication) { |
32 | 25 | return; |
33 | 26 | } |
34 | 27 |
|
35 | | - if ($data->hasResourceNode()) { |
36 | | - $this->trackRepo->registerResourceEvent( |
37 | | - $data->getResourceNode(), |
38 | | - 'deletion' |
39 | | - ); |
40 | | - } |
| 28 | + $node = $data->hasResourceNode() ? $data->getResourceNode() : null; |
41 | 29 |
|
42 | | - foreach ($data->getChildren() as $child) { |
43 | | - $this->entityManager->remove($child); |
44 | | - } |
| 30 | + $this->em->beginTransaction(); |
| 31 | + try { |
| 32 | + try { $this->em->refresh($data); } catch (\Throwable) {} |
| 33 | + |
| 34 | + $this->em->remove($data); |
| 35 | + $this->em->flush(); |
45 | 36 |
|
46 | | - $this->entityManager->remove($data); |
47 | | - $this->entityManager->flush(); |
| 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 | + } |
48 | 54 | } |
49 | 55 | } |
0 commit comments