Passed
Push — master ( f7af3b...d79566 )
by Angel Fernando Quiroz
16:52 queued 14s
created

Version20200821224245::up()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
declare(strict_types=1);
6
7
namespace Chamilo\CoreBundle\Migrations\Schema\V200;
8
9
use Chamilo\CoreBundle\Entity\Message;
10
use Chamilo\CoreBundle\Entity\MessageRelUser;
11
use Chamilo\CoreBundle\Entity\MessageTag;
12
use Chamilo\CoreBundle\Entity\User;
13
use Chamilo\CoreBundle\Migrations\AbstractMigrationChamilo;
14
use Doctrine\DBAL\Schema\Schema;
15
16
final class Version20200821224245 extends AbstractMigrationChamilo
17
{
18
    /**
19
     * @inheritDoc
20
     */
21
    public function up(Schema $schema): void
22
    {
23
        $this->migrateTagsFromInboxMessages();
24
    }
25
26
    private function migrateTagsFromInboxMessages(): void
27
    {
28
        // File generated in the Version20180928172830 migration
29
        $messageTagsInfo = $this->readFile(Version20200821224230::INBOX_TAGS_FILE);
30
31
        if (empty($messageTagsInfo)) {
32
            $this->write(Version20200821224230::INBOX_TAGS_FILE.' file not found. Exiting.');
33
34
            return;
35
        }
36
37
        $oldMessageTagsInfo = unserialize($messageTagsInfo);
38
39
        $messageRelUserRepo = $this->entityManager->getRepository(MessageRelUser::class);
0 ignored issues
show
Bug introduced by
The method getRepository() 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

39
        /** @scrutinizer ignore-call */ 
40
        $messageRelUserRepo = $this->entityManager->getRepository(MessageRelUser::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...
40
        $tagRepo = $this->entityManager->getRepository(MessageTag::class);
41
42
        foreach ($oldMessageTagsInfo as $rowMessageTag) {
43
            $message = $this->entityManager->find(Message::class, $rowMessageTag['m_id']);
44
            $receiver = $this->entityManager->find(User::class, $rowMessageTag['m_receiver_id']);
45
46
            $messageTag = $tagRepo->findOneBy(['tag' => $rowMessageTag['t_tag'], 'user' => $receiver]);
47
48
            if (!$messageTag) {
49
                $messageTag = (new MessageTag())
50
                    ->setTag($rowMessageTag['t_tag'])
51
                    ->setUser($receiver)
52
                ;
53
            }
54
55
            $messageRelUser = $messageRelUserRepo->findOneBy(['message' => $message, 'receiver' => $receiver]);
56
57
            if ($messageRelUser) {
58
                $messageRelUser->addTag($messageTag);
59
60
                $this->entityManager->persist($messageRelUser);
61
                $this->entityManager->flush();
62
            }
63
        }
64
65
        $this->entityManager->clear();
66
67
        $this->removeFile(Version20200821224230::INBOX_TAGS_FILE);
68
    }
69
}
70