|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace AppBundle\DataFixtures\ORM\Video; |
|
4
|
|
|
|
|
5
|
|
|
use AppBundle\DataFixtures\ORM\Data\TagFixturesData; |
|
6
|
|
|
use AppBundle\DataFixtures\ORM\User\UserFixtures; |
|
7
|
|
|
use Symfony\Component\HttpFoundation\File\UploadedFile; |
|
8
|
|
|
use AppBundle\DataFixtures\ORM\Data\UserFixturesData; |
|
9
|
|
|
use AppBundle\DataFixtures\ORM\Data\VideoFixturesData; |
|
10
|
|
|
use AppBundle\Entity\Video; |
|
11
|
|
|
use Doctrine\Bundle\FixturesBundle\Fixture; |
|
12
|
|
|
use Doctrine\Common\Persistence\ObjectManager; |
|
13
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
|
14
|
|
|
|
|
15
|
|
|
class VideoFixtures extends Fixture |
|
16
|
|
|
{ |
|
17
|
|
|
const MAX_TAG = 10; |
|
18
|
|
|
|
|
19
|
|
|
public function load(ObjectManager $manager) |
|
20
|
|
|
{ |
|
21
|
|
|
$fs = new Filesystem(); |
|
22
|
|
|
$users = array_slice(UserFixturesData::$data, 0, UserFixtures::LIMIT); |
|
23
|
|
|
$data = VideoFixturesData::$data; |
|
24
|
|
|
|
|
25
|
|
|
foreach ($data as $videoData) { |
|
26
|
|
|
echo '.'; |
|
27
|
|
|
$video = new Video(); |
|
28
|
|
|
$video->setTitle($videoData['title']); |
|
29
|
|
|
$video->setDescription($videoData['title']); |
|
30
|
|
|
$filePath = __DIR__ . '/../Data/test.mp4'; |
|
31
|
|
|
$video->setCreator($this->getReference($users[array_rand($users)]['firstName'])); |
|
32
|
|
|
for ($i = 0; $i < rand(1, self::MAX_TAG); ++$i) { |
|
33
|
|
|
$tag = $this->getReference(TagFixturesData::$data[array_rand(TagFixturesData::$data)]['name']); |
|
34
|
|
|
if (!in_array($tag, $video->getTags()->toArray())) { |
|
35
|
|
|
$video->addTag($tag); |
|
36
|
|
|
} |
|
37
|
|
|
} |
|
38
|
|
|
$manager->persist($video); |
|
39
|
|
|
$manager->flush(); // video storage strategy is based on subdirectory video id, so video must be persisted te get an id |
|
40
|
|
|
$fileName = \filter_var($videoData['title'], FILTER_SANITIZE_EMAIL); |
|
41
|
|
|
$targetfilePath = __DIR__ . '/../Data/' . $fileName . 'mp4'; |
|
42
|
|
|
$fs->copy($filePath, $targetfilePath); |
|
43
|
|
|
$video->setVideoFile(new UploadedFile($targetfilePath, 'test.mp4', 'video/mp4', null, null, true)); |
|
44
|
|
|
$video->setUpdatedAt(new \DateTime()); |
|
45
|
|
|
$manager->persist($video); |
|
46
|
|
|
} |
|
47
|
|
|
$manager->flush(); |
|
48
|
|
|
} |
|
49
|
|
|
} |
|
50
|
|
|
|