TwoImageFixtures::load()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 10
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 15
rs 9.9332
1
<?php
2
3
namespace App\DataFixtures;
4
5
use Doctrine\Bundle\FixturesBundle\Fixture;
6
use Doctrine\Bundle\FixturesBundle\FixtureGroupInterface;
7
use Doctrine\Persistence\ObjectManager;
8
use Symfony\Component\Filesystem\Filesystem;
9
use Symfony\Component\Finder\Finder;
10
use App\Entity\Image;
11
use Symfony\Component\HttpFoundation\File\File;
12
use Symfony\Component\HttpFoundation\File\UploadedFile;
13
use Symfony\Component\Routing\Generator\UrlGenerator;
14
15
16
class TwoImageFixtures extends Fixture implements FixtureGroupInterface
17
{
18
    public static function getGroups(): array
19
    {
20
        return ['image'];
21
    }
22
23
    public function load(ObjectManager $manager): void
24
    {
25
        $fs = new Filesystem();
26
        $finder = new Finder();
27
        foreach ($finder->in(__DIR__ . '/image/two')->name('/\.jpg$/i') as $source) {
28
29
            $image = new Image();
30
31
            $targetPath = sys_get_temp_dir() . '/' . $source->getFilename();
32
            $fs->copy($source->getPathname(), $targetPath);
33
            $uploadedFake = new UploadedFile($targetPath, $source->getFilename(), 'image/jpeg', null, true);
34
            $image->setImageFile($uploadedFake);
35
            $manager->persist($image);
36
        }
37
        $manager->flush();
38
    }
39
}
40