Version20210509222909   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 18
c 1
b 0
f 0
dl 0
loc 46
rs 10
wmc 8

5 Methods

Rating   Name   Duplication   Size   Complexity  
A postUp() 0 17 3
A down() 0 4 1
A setContainer() 0 6 2
A getDescription() 0 3 1
A up() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace DoctrineMigrations;
6
7
use Doctrine\DBAL\Schema\Schema;
8
use Doctrine\Migrations\AbstractMigration;
9
use Exception;
10
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
11
use Symfony\Component\DependencyInjection\ContainerInterface;
12
use App\Service\GpxService;
13
14
/**
15
 * Auto-generated Migration: Please modify to your needs!
16
 */
17
final class Version20210509222909 extends AbstractMigration implements ContainerAwareInterface
18
{
19
    /** @var ContainerInterface */
20
    private $container;
21
22
    public function getDescription(): string
23
    {
24
        return '';
25
    }
26
27
    public function setContainer(?ContainerInterface $container = null): void
28
    {
29
        if ($container === null) {
30
            throw new Exception("Wasn't given a container in this container-aware migration");
31
        }
32
        $this->container = $container;
33
    }
34
35
    public function up(Schema $schema): void
36
    {
37
        // this up() migration is auto-generated, please modify it to your needs
38
        $this->addSql('ALTER TABLE wander ADD geo_json LONGTEXT DEFAULT NULL');
39
    }
40
41
    public function down(Schema $schema): void
42
    {
43
        // this down() migration is auto-generated, please modify it to your needs
44
        $this->addSql('ALTER TABLE wander DROP geo_json');
45
    }
46
    public function postUp(Schema $schema): void
47
    {
48
        if ($this->container == null) {
49
            throw new Exception('No container available in container-aware migration');
50
        }
51
52
        /** @var GpxService */
53
        $gpxService = $this->container->get('App\Service\GpxService');
54
55
        $updateGeoJsonStatement = $this->connection->prepare('UPDATE wander SET geo_json = (:geo_json) WHERE id = :id');
56
57
        $wanders = $this->connection->fetchAllAssociative('SELECT id, gpx_filename FROM wander WHERE gpx_filename IS NOT NULL');
58
        foreach ($wanders as $wander) {
59
            $geoJson = $gpxService->gpxToGeoJson($gpxService->getGpxStringFromFilename($wander['gpx_filename']));
60
            $updateGeoJsonStatement->bindValue('id', $wander['id']);
61
            $updateGeoJsonStatement->bindValue('geo_json', $geoJson);
62
            $updateGeoJsonStatement->executeStatement();
63
        }
64
    }
65
}
66