Passed
Push — alpha ( b6fe0d...86999d )
by Yannick
08:00
created

Version20240602231700   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
eloc 21
c 3
b 1
f 0
dl 0
loc 41
rs 10
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getDescription() 0 3 1
A up() 0 34 5
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 that do not have parents based on the path.';
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 documentIids
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
        foreach ($orphans as $itemData) {
40
            echo 'Deleting document with iid: ' . $itemData['iid'] . PHP_EOL;
41
            $document = $documentRepo->find($itemData['iid']);
42
            if ($document) {
43
                if ($document->getResourceNode()) {
44
                    $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

44
                    $this->entityManager->/** @scrutinizer ignore-call */ 
45
                                          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...
45
                }
46
                $this->entityManager->remove($document);
47
                echo 'Deleted document with iid: ' . $itemData['iid'] . PHP_EOL;
48
            } else {
49
                echo 'Document with iid ' . $itemData['iid'] . ' not found.' . PHP_EOL;
50
            }
51
        }
52
53
        $this->entityManager->flush();
54
        $this->entityManager->clear();
55
    }
56
}
57