Passed
Pull Request — master (#5775)
by Angel Fernando Quiroz
07:22
created

Version20230204150030::up()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 54
Code Lines 38

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 38
nc 6
nop 1
dl 0
loc 54
rs 9.0008
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\Entity\Asset;
10
use Chamilo\CoreBundle\Entity\ExtraField;
11
use Chamilo\CoreBundle\Entity\ExtraFieldValues;
12
use Chamilo\CoreBundle\Entity\Session;
13
use Chamilo\CoreBundle\Migrations\AbstractMigrationChamilo;
14
use Chamilo\CoreBundle\Repository\SessionRepository;
15
use Doctrine\DBAL\Schema\Schema;
16
use Symfony\Component\HttpFoundation\File\UploadedFile;
17
18
class Version20230204150030 extends AbstractMigrationChamilo
19
{
20
    public function getDescription(): string
21
    {
22
        return 'Move extrafield session image to asset';
23
    }
24
25
    public function up(Schema $schema): void
26
    {
27
        $kernel = $this->container->get('kernel');
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

27
        /** @scrutinizer ignore-call */ 
28
        $kernel = $this->container->get('kernel');

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...
28
        $rootPath = $kernel->getProjectDir();
29
30
        $batchSize = self::BATCH_SIZE;
31
        $counter = 1;
32
        $dql = 'SELECT v FROM Chamilo\CoreBundle\Entity\ExtraFieldValues v';
33
        $dql .= ' JOIN v.field f';
34
        $dql .= ' WHERE f.variable = :variable AND f.itemType = :itemType';
35
        $q = $this->entityManager->createQuery($dql);
0 ignored issues
show
Bug introduced by
The method createQuery() 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

35
        /** @scrutinizer ignore-call */ 
36
        $q = $this->entityManager->createQuery($dql);

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...
36
        $q->setParameters([
37
            'variable' => 'image',
38
            'itemType' => ExtraField::SESSION_FIELD_TYPE,
39
        ]);
40
41
        $sessionRepo = $this->container->get(SessionRepository::class);
42
43
        /** @var ExtraFieldValues $item */
44
        foreach ($q->toIterable() as $item) {
45
            $path = $item->getFieldValue();
46
            if (empty($path)) {
47
                continue;
48
            }
49
            $filePath = $rootPath.'/app/upload/'.$path;
50
            error_log('MIGRATIONS :: $filePath -- '.$filePath.' ...');
51
            if ($this->fileExists($filePath)) {
52
                $fileName = basename($path);
53
                $mimeType = mime_content_type($filePath);
54
                $file = new UploadedFile($filePath, $fileName, $mimeType, null, true);
55
                $asset = (new Asset())
56
                    ->setCategory(Asset::SESSION)
57
                    ->setTitle($fileName)
58
                    ->setFile($file)
59
                ;
60
                $this->entityManager->persist($asset);
61
                $this->entityManager->flush();
62
                $item->setAsset($asset);
63
                $this->entityManager->persist($item);
64
65
                $sessionId = $item->getItemId();
66
67
                /** @var Session $session */
68
                $session = $sessionRepo->find($sessionId);
69
                $session->setImage($asset);
70
                $sessionRepo->update($session);
71
            }
72
73
            if (($counter % $batchSize) === 0) {
74
                $this->entityManager->flush();
75
            }
76
            $counter++;
77
        }
78
        $this->entityManager->flush();
79
    }
80
81
    public function down(Schema $schema): void {}
82
}
83