Completed
Push — master ( 46235d...917266 )
by Zlatin
10:01
created

src/Media/Provider/MediaProvider.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 Media\Provider;
4
5
use Doctrine\ORM\EntityManager;
6
use Media\Entity\MediaInterface;
7
use Media\Provider\MediaProviderInterface;
8
use Symfony\Component\HttpFoundation\File\UploadedFile;
9
10
class MediaProvider implements MediaProviderInterface
11
{
12
13
    /**
14
     *
15
     * @var EntityManager
16
     */
17
    private $entityManager;
18
19
    /**
20
     *
21
     * @var string
22
     */
23
    private $path;
24
25
    /**
26
     * 
27
     * @param EntityManager $entityManager
28
     * @param string $path
29
     */
30
    public function __construct(EntityManager $entityManager, $path)
0 ignored issues
show
You have injected the EntityManager via parameter $entityManager. 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...
31
    {
32
        $this->path = $path;
33
        $this->entityManager = $entityManager;
34
    }
35
36
    /**
37
     * 
38
     * @param UploadedFile $file
39
     * @param string $directory
40
     * 
41
     * @return string
42
     */
43
    public function generateName(UploadedFile $file, $directory)
44
    {
45
        $extension = $file->guessClientExtension();
46
        do {
47
            $filename = md5(uniqid()) . '.' . $extension;
48
            if (file_exists($directory . DIRECTORY_SEPARATOR . $filename)) {
49
                $filename = null;
50
            }
51
        } while (is_null($filename));
52
53
        return $filename;
54
    }
55
56
    public function save(MediaInterface $media, $class, $options)
57
    {
58
59
        if (!$media->getFileContent() instanceof UploadedFile) {
60
            return $media;
61
        }
62
63
        $directory = $this->path . DIRECTORY_SEPARATOR . $options['context'];
64
        if (!is_dir($directory) || !is_writeable($directory)) {
65
            mkdir($directory, '0755', true);
66
        }
67
68
        if ($options['new_on_update']) {
69
            $entity = new $class;
70
        } else {
71
            $entity = clone $media;
72
        }
73
74
        $filename = $this->generateName($media->getFileContent(), $directory);
75
76
        $media->getFileContent()->move($directory, $filename);
77
78
        $entity->setFileName($filename);
79
        $entity->setContext($options['context']);
80
        $entity->setFileType($media->getFileContent()->getClientMimeType());
81
        $entity->setFileSize($media->getFileContent()->getClientSize());
82
        $entity->setMetadata([
83
            'filename' => $media->getFileContent()->getClientOriginalName(),
84
            'filesize' => $media->getFileContent()->getClientSize(),
85
            'filetype' => $media->getFileContent()->getClientMimeType(),
86
            'extension' => $media->getFileContent()->getClientOriginalExtension()
87
        ]);
88
89
        $this->entityManager->persist($entity);
90
        $this->entityManager->flush();
91
92
        return $entity;
93
    }
94
95
}
96