TwoImageFixtures   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 22
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
c 1
b 0
f 0
dl 0
loc 22
rs 10
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A load() 0 15 2
A getGroups() 0 3 1
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