Completed
Push — master ( 8fe45b...3e4cf7 )
by Jeroen
08:37
created

Kunstmaan/FixturesBundle/Builder/MediaBuilder.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Kunstmaan\FixturesBundle\Builder;
4
5
use Doctrine\ORM\EntityManager;
6
use Kunstmaan\FixturesBundle\Loader\Fixture;
7
use Kunstmaan\MediaBundle\Entity\Folder;
8
use Kunstmaan\MediaBundle\Entity\Media;
9
use Kunstmaan\MediaBundle\Helper\File\FileHandler;
10
use Kunstmaan\MediaBundle\Helper\MimeTypeGuesserFactoryInterface;
11
use Symfony\Component\HttpFoundation\File\File;
12
use Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesserInterface;
13
14
class MediaBuilder implements BuilderInterface
15
{
16
    private $em;
17
18
    private $fileHandler;
19
20
    /**
21
     * @var MimeTypeGuesserInterface
22
     */
23
    private $mimeTypeGuesser;
24
25
    private $folder;
26
27
    public function __construct(EntityManager $em, FileHandler $fileHandler, MimeTypeGuesserFactoryInterface $mimeTypeGuesserFactory)
0 ignored issues
show
You have injected the EntityManager via parameter $em. This is generally not recommended as it might get closed and become unusable. Instead, it is recommended to inject the ManagerRegistry and retrieve the EntityManager via getManager() each time you need it.

The EntityManager might become unusable for example if a transaction is rolled back and it gets closed. Let’s assume that somewhere in your application, or in a third-party library, there is code such as the following:

function someFunction(ManagerRegistry $registry) {
    $em = $registry->getManager();
    $em->getConnection()->beginTransaction();
    try {
        // Do something.
        $em->getConnection()->commit();
    } catch (\Exception $ex) {
        $em->getConnection()->rollback();
        $em->close();

        throw $ex;
    }
}

If that code throws an exception and the EntityManager is closed. Any other code which depends on the same instance of the EntityManager during this request will fail.

On the other hand, if you instead inject the ManagerRegistry, the getManager() method guarantees that you will always get a usable manager instance.

Loading history...
28
    {
29
        $this->em = $em;
30
        $this->fileHandler = $fileHandler;
31
        $this->mimeTypeGuesser = $mimeTypeGuesserFactory->get();
32
    }
33
34
    public function canBuild(Fixture $fixture)
35
    {
36
        if ($fixture->getEntity() instanceof Media) {
37
            return true;
38
        }
39
40
        return false;
41
    }
42
43
    public function preBuild(Fixture $fixture)
44
    {
45
        $properties = $fixture->getProperties();
46
        if (!isset($properties['folder'])) {
47
            throw new \Exception('There is no folder specified for media fixture ' . $fixture->getName());
48
        }
49
50
        $this->folder = $this->em->getRepository(Folder::class)->findOneBy(array('rel' => $properties['folder']));
51
52
        if (!$this->folder instanceof Folder) {
53
            $this->folder = $this->em->getRepository(Folder::class)->findOneBy(array('internalName' => $properties['folder']));
54
        }
55
56
        if (!$this->folder instanceof Folder) {
57
            throw new \Exception('Could not find the specified folder for media fixture ' . $fixture->getName());
58
        }
59
    }
60
61
    public function postBuild(Fixture $fixture)
62
    {
63
        /** @var Media $media */
64
        $media = $fixture->getEntity();
65
66
        $filePath = $media->getOriginalFilename();
67
        $data = new File($filePath, true);
68
        $contentType = $this->mimeTypeGuesser->guess($data->getPathname());
69
70
        if (method_exists($data, 'getClientOriginalName')) {
71
            $media->setOriginalFilename($data->getClientOriginalName());
72
        } else {
73
            $media->setOriginalFilename($data->getFilename());
74
        }
75
76
        if ($media->getName() === null) {
77
            $media->setName($media->getOriginalFilename());
78
        }
79
80
        $media->setContent($data);
81
82
        $media->setContentType($contentType);
83
        $media->setFolder($this->folder);
84
85
        $this->fileHandler->prepareMedia($media);
86
        $this->fileHandler->updateMedia($media);
87
        $this->fileHandler->saveMedia($media);
88
    }
89
90
    public function postFlushBuild(Fixture $fixture)
91
    {
92
        return;
93
    }
94
}
95