Completed
Push — master ( d7e941...2e97bd )
by Matt
06:23
created

WanderFixtures::load()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 13
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 16
rs 9.8333
1
<?php
2
3
namespace App\DataFixtures;
4
5
use App\Entity\Wander;
6
use App\Service\GpxService;
7
use App\Service\UploaderHelper;
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 UploaderHelper */
18
    private $uploaderHelper;
19
20
    /** @var GpxService */
21
    private $gpxService;
22
23
    public function __construct(UploaderHelper $uploaderHelper, GpxService $gpxService)
24
    {
25
        $this->uploaderHelper = $uploaderHelper;
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->uploaderHelper->uploadGpxFile(new File($targetPath));
42
            $wander = new Wander();
43
            $wander->setGpxFilename($uploadedFile);
44
            $this->gpxService->updateWanderStatsFromGpx($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