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) |
|
|
|
|
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(['rel' => $properties['folder']]); |
51
|
|
|
|
52
|
|
|
if (!$this->folder instanceof Folder) { |
53
|
|
|
$this->folder = $this->em->getRepository(Folder::class)->findOneBy(['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
|
|
|
|
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:If that code throws an exception and the
EntityManager
is closed. Any other code which depends on the same instance of theEntityManager
during this request will fail.On the other hand, if you instead inject the
ManagerRegistry
, thegetManager()
method guarantees that you will always get a usable manager instance.