WanderFixtures   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 19
c 1
b 0
f 0
dl 0
loc 35
rs 10
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A load() 0 16 2
A getGroups() 0 3 1
1
<?php
2
3
namespace App\DataFixtures;
4
5
use App\Entity\Wander;
6
use App\Service\GpxService;
7
use App\Service\UploadHelper;
8
use Doctrine\Bundle\FixturesBundle\Fixture;
9
use Doctrine\Bundle\FixturesBundle\FixtureGroupInterface;
10
use Doctrine\Persistence\ObjectManager;
11
use Symfony\Component\Filesystem\Filesystem;
12
use Symfony\Component\Finder\Finder;
13
use Symfony\Component\HttpFoundation\File\File;
14
15
class WanderFixtures extends Fixture implements FixtureGroupInterface
16
{
17
    /** @var UploadHelper */
18
    private $uploadHelper;
19
20
    /** @var GpxService */
21
    private $gpxService;
22
23
    public function __construct(UploadHelper $uploadHelper, GpxService $gpxService)
24
    {
25
        $this->uploadHelper = $uploadHelper;
26
        $this->gpxService = $gpxService;
27
    }
28
29
    public static function getGroups(): array
30
    {
31
        return ['wander'];
32
    }
33
34
    public function load(ObjectManager $manager): void
35
    {
36
        $fs = new Filesystem();
37
        $finder = new Finder();
38
        foreach($finder->in(__DIR__ . '/gpx')->name('/\.gpx$/i') as $source) {
39
            $targetPath = sys_get_temp_dir() . '/' . $source->getFilename();
40
            $fs->copy($source->getPathname(), $targetPath);
41
            $uploadedFile = $this->uploadHelper->uploadGpxFile(new File($targetPath));
42
            $wander = new Wander();
43
            $wander->setGpxFilename($uploadedFile);
44
            $this->gpxService->updateWanderFromGpx($wander);
45
            $wander->setTitle('Test Wander Title for ' . $source->getFilename());
46
            $wander->setDescription('Test wander description for ' . $source->getFilename());
47
            $manager->persist($wander);
48
        }
49
        $manager->flush();
50
    }
51
}
52