Passed
Pull Request — master (#5565)
by
unknown
07:20
created

Version20240602231700::up()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 47
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 6
eloc 26
c 2
b 0
f 0
nc 6
nop 1
dl 0
loc 47
rs 8.8817
1
<?php
2
3
declare(strict_types=1);
4
5
/* For licensing terms, see /license.txt */
6
7
namespace Chamilo\CoreBundle\Migrations\Schema\V200;
8
9
use Chamilo\CoreBundle\Migrations\AbstractMigrationChamilo;
10
use Chamilo\CourseBundle\Repository\CDocumentRepository;
11
use Doctrine\DBAL\Connection;
12
use Doctrine\DBAL\Schema\Schema;
13
14
final class Version20240602231700 extends AbstractMigrationChamilo
15
{
16
    public function getDescription(): string
17
    {
18
        return 'Delete documents based on resource_node_id';
19
    }
20
21
    public function up(Schema $schema): void
22
    {
23
        $documentRepo = $this->container->get(CDocumentRepository::class);
0 ignored issues
show
Bug introduced by
The method get() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

23
        /** @scrutinizer ignore-call */ 
24
        $documentRepo = $this->container->get(CDocumentRepository::class);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
24
25
        // Query to get the resourceNodeIds
26
        $sql = "SELECT cd1.iid, cd1.path, cd1.c_id
27
                FROM c_document cd1
28
                LEFT JOIN c_document cd2 ON cd2.c_id = cd1.c_id AND cd2.path = SUBSTRING_INDEX(cd1.path, '/', 2)
29
                WHERE cd1.path LIKE '/shared_folder_session_%'
30
                AND cd2.iid IS NULL";
31
        $result = $this->connection->executeQuery($sql);
32
        $orphans = $result->fetchAllAssociative();
33
34
        if (empty($orphans)) {
35
            echo 'No orphan documents found.' . PHP_EOL;
36
            return;
37
        }
38
39
        // Extract resourceNodeIds from the result
40
        $resourceNodeIds = array_column($orphans, 'iid');
41
42
        // If there are resourceNodeIds, continue with the script
43
        $sql = 'SELECT iid FROM c_document WHERE resource_node_id IN (?)';
44
        $result = $this->connection->executeQuery($sql, [$resourceNodeIds], [Connection::PARAM_INT_ARRAY]);
45
        $iids = $result->fetchAllAssociative();
46
47
        if (empty($iids)) {
48
            echo 'No documents found to delete.' . PHP_EOL;
49
            return;
50
        }
51
52
        foreach ($iids as $itemData) {
53
            echo 'Deleting document with iid: ' . $itemData['iid'] . PHP_EOL;
54
            $document = $documentRepo->find($itemData['iid']);
55
            if ($document) {
56
                if ($document->getResourceNode()) {
57
                    $this->entityManager->remove($document->getResourceNode());
0 ignored issues
show
Bug introduced by
The method remove() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

57
                    $this->entityManager->/** @scrutinizer ignore-call */ 
58
                                          remove($document->getResourceNode());

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
58
                }
59
                $this->entityManager->remove($document);
60
                echo 'Deleted document with iid: ' . $itemData['iid'] . PHP_EOL;
61
            } else {
62
                echo 'Document with iid ' . $itemData['iid'] . ' not found.' . PHP_EOL;
63
            }
64
        }
65
66
        $this->entityManager->flush();
67
        $this->entityManager->clear();
68
    }
69
}
70