GPX   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 126
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 6
dl 0
loc 126
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A writeTracks() 0 11 2
A writeTrackPoints() 0 23 2
A writeExtensions() 0 14 3
A writeMetaData() 0 10 2
B dump() 0 34 1
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace SportTrackerConnector\Core\Workout\Dumper;
6
7
use SportTrackerConnector\Core\Workout\Extension\ExtensionInterface;
8
use SportTrackerConnector\Core\Workout\Extension\HR;
9
use SportTrackerConnector\Core\Workout\TrackPoint;
10
use SportTrackerConnector\Core\Workout\Workout;
11
12
/**
13
 * Dump a workout to GPX format.
14
 */
15
final class GPX implements DumperInterface
16
{
17
    /**
18
     * {@inheritdoc}
19
     */
20
    public function dump(Workout $workout): string
21
    {
22
        $xmlWriter = new \XMLWriter();
23
        $xmlWriter->openMemory();
24
        $xmlWriter->setIndent(true);
25
        $xmlWriter->startDocument('1.0', 'UTF-8');
26
        $xmlWriter->startElement('gpx');
27
28
        $xmlWriter->writeAttribute('version', '1.1');
29
        $xmlWriter->writeAttribute('creator', 'SportTrackerConnector');
30
        $xmlWriter->writeAttributeNs(
31
            'xsi',
32
            'schemaLocation',
33
            null,
34
            'http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd http://www.garmin.com/xmlschemas/GpxExtensions/v3 http://www.garmin.com/xmlschemas/GpxExtensionsv3.xsd http://www.garmin.com/xmlschemas/TrackPointExtension/v1 http://www.garmin.com/xmlschemas/TrackPointExtensionv1.xsd'
35
        );
36
        $xmlWriter->writeAttribute('xmlns', 'http://www.topografix.com/GPX/1/1');
37
        $xmlWriter->writeAttributeNs(
38
            'xmlns',
39
            'gpxtpx',
40
            null,
41
            'http://www.garmin.com/xmlschemas/TrackPointExtension/v1'
42
        );
43
        $xmlWriter->writeAttributeNs('xmlns', 'gpxx', null, 'http://www.garmin.com/xmlschemas/GpxExtensions/v3');
44
        $xmlWriter->writeAttributeNs('xmlns', 'xsi', null, 'http://www.w3.org/2001/XMLSchema-instance');
45
46
        $this->writeMetaData($xmlWriter, $workout);
47
        $this->writeTracks($xmlWriter, $workout);
48
49
        $xmlWriter->endElement();
50
        $xmlWriter->endDocument();
51
52
        return $xmlWriter->outputMemory(true);
53
    }
54
55
    /**
56
     * Write the tracks to the GPX.
57
     *
58
     * @param \XMLWriter $xmlWriter The XML writer.
59
     * @param Workout $workout The workout.
60
     */
61
    private function writeTracks(\XMLWriter $xmlWriter, Workout $workout)
62
    {
63
        foreach ($workout->tracks() as $track) {
64
            $xmlWriter->startElement('trk');
65
            $xmlWriter->writeElement('type', $track->sport());
66
            $xmlWriter->startElement('trkseg');
67
            $this->writeTrackPoints($xmlWriter, $track->trackPoints());
68
            $xmlWriter->endElement();
69
            $xmlWriter->endElement();
70
        }
71
    }
72
73
    /**
74
     * Write the track points to the GPX.
75
     *
76
     * @param \XMLWriter $xmlWriter The XML writer.
77
     * @param TrackPoint[] $trackPoints The track points to write.
78
     */
79
    private function writeTrackPoints(\XMLWriter $xmlWriter, array $trackPoints)
80
    {
81
        foreach ($trackPoints as $trackPoint) {
82
            $xmlWriter->startElement('trkpt');
83
84
            // Location.
85
            $xmlWriter->writeAttribute('lat', (string)$trackPoint->latitude());
86
            $xmlWriter->writeAttribute('lon', (string)$trackPoint->longitude());
87
88
            // Elevation.
89
            $xmlWriter->writeElement('ele', (string)$trackPoint->elevation());
90
91
            // Time of position
92
            $dateTime = clone $trackPoint->dateTime();
93
            $dateTime->setTimezone(new \DateTimeZone('UTC'));
94
            $xmlWriter->writeElement('time', $dateTime->format(\DateTime::W3C));
95
96
            // Extensions.
97
            $this->writeExtensions($xmlWriter, $trackPoint->extensions());
98
99
            $xmlWriter->endElement();
100
        }
101
    }
102
103
    /**
104
     * Write the extensions into the GPX.
105
     *
106
     * @param \XMLWriter $xmlWriter The XMLWriter.
107
     * @param ExtensionInterface[] $extensions The extensions to write.
108
     */
109
    protected function writeExtensions(\XMLWriter $xmlWriter, array $extensions)
110
    {
111
        $xmlWriter->startElement('extensions');
112
        foreach ($extensions as $extension) {
113
            switch ($extension::ID()) {
114
                case HR::ID():
115
                    $xmlWriter->startElementNs('gpxtpx', 'TrackPointExtension', null);
116
                    $xmlWriter->writeElementNs('gpxtpx', 'hr', null, (string)$extension->value());
117
                    $xmlWriter->endElement();
118
                    break;
119
            }
120
        }
121
        $xmlWriter->endElement();
122
    }
123
124
    /**
125
     * Write the metadata in the GPX.
126
     *
127
     * @param \XMLWriter $xmlWriter The XML writer.
128
     * @param Workout $workout The workout.
129
     */
130
    protected function writeMetaData(\XMLWriter $xmlWriter, Workout $workout)
131
    {
132
        $xmlWriter->startElement('metadata');
133
        if ($workout->author() !== null) {
134
            $xmlWriter->startElement('author');
135
            $xmlWriter->writeElement('name', $workout->author()->name());
136
            $xmlWriter->endElement();
137
        }
138
        $xmlWriter->endElement();
139
    }
140
}
141