Passed
Push — master ( 849c0b...dbe650 )
by Matt
06:19 queued 01:33
created

Wander   A

Complexity

Total Complexity 34

Size/Duplication

Total Lines 296
Duplicated Lines 0 %

Importance

Changes 4
Bugs 0 Features 1
Metric Value
eloc 67
c 4
b 0
f 1
dl 0
loc 296
rs 9.68
wmc 34

31 Methods

Rating   Name   Duplication   Size   Complexity  
A getDuration() 0 8 2
A setCumulativeElevationGain() 0 5 1
A getTitle() 0 3 1
A setTitle() 0 5 1
A getStartTime() 0 3 1
A setStartTime() 0 5 1
A getMaxAltitude() 0 3 1
A setAvgPace() 0 5 1
A getAvgPace() 0 3 1
A getDistance() 0 3 1
A __construct() 0 3 1
A removeImage() 0 5 1
A getMinAltitude() 0 3 1
A setDescription() 0 5 1
A setEndTime() 0 5 1
A getImages() 0 3 1
A setDistance() 0 5 1
A getEndTime() 0 3 1
A getGpxFilename() 0 3 1
A getDescription() 0 3 1
A setAvgSpeed() 0 5 1
A setMaxAltitude() 0 5 1
A addImage() 0 7 2
A setGpxFilename() 0 5 1
A setMinAltitude() 0 5 1
A getCumulativeElevationGain() 0 3 1
A getAvgSpeed() 0 3 1
A isTimeLengthSuspicious() 0 4 1
A getId() 0 3 1
A getImageCount() 0 4 1
A __toString() 0 7 2
1
<?php
2
3
namespace App\Entity;
4
5
use ApiPlatform\Core\Annotation\ApiProperty;
6
use App\Repository\WanderRepository;
7
use ApiPlatform\Core\Annotation\ApiResource;
8
use ApiPlatform\Core\Annotation\ApiSubresource;
9
use DateInterval;
10
use DateTime;
11
use Doctrine\Common\Collections\ArrayCollection;
12
use Doctrine\Common\Collections\Collection;
13
use Symfony\Component\Serializer\Annotation\Groups;
14
use Doctrine\ORM\Mapping as ORM;
15
use App\EventListener\WanderUploadListener;
16
use Carbon\Carbon;
17
use Carbon\CarbonInterval;
18
19
20
/**
21
 * @ORM\Entity(repositoryClass=WanderRepository::class)
22
 *
23
 * @ORM\EntityListeners({
24
 *  WanderUploadListener::class, App\EventListener\WanderDeleteListener::class
25
 * })
26
 *
27
 * @ApiResource(
28
 *  collectionOperations={"get"={"normalization_context"={"groups"="wander:list"}}},
29
 *  itemOperations={"get"={"normalization_context"={"groups"="wander:item"}}},
30
 *  order={"endTime"="ASC"},
31
 *  paginationEnabled=false
32
 * )
33
 *
34
 */
35
class Wander
36
{
37
    /**
38
     * @ORM\Id
39
     * @ORM\GeneratedValue
40
     * @ORM\Column(type="integer")
41
     *
42
     * @Groups({"wander:list", "wander:item"})
43
     */
44
    private $id;
45
46
    /**
47
     * @ORM\Column(type="string", length=1024)
48
     *
49
     * @Groups({"wander:list", "wander:item"})
50
     */
51
    private $title;
52
53
    /**
54
     * @ORM\Column(type="datetime")
55
     *
56
     * @Groups({"wander:list", "wander:item"})
57
     */
58
    private $startTime;
59
60
    /**
61
     * @ORM\Column(type="datetime")
62
     *
63
     * @Groups({"wander:list", "wander:item"})
64
     */
65
    private $endTime;
66
67
    /**
68
     * @ORM\Column(type="text", nullable=true)
69
     *
70
     * @Groups({"wander:list", "wander:item"})
71
     */
72
    private $description;
73
74
    /**
75
     * @ORM\Column(type="string", length=255)
76
     *
77
     * @Groups({"wander:list", "wander:item"})
78
     */
79
    private $gpxFilename;
80
81
    /**
82
     * @ORM\ManyToMany(targetEntity=Image::class, inversedBy="wanders")
83
     *
84
     * @Groups({"wander:list", "wander:item"})
85
     * @ApiSubresource
86
     */
87
    private $images;
88
89
    /**
90
     * @ORM\Column(type="float", nullable=true)
91
     *
92
     * Distance walked, in metres.
93
     *
94
     */
95
    private $distance;
96
97
    /**
98
     * @ORM\Column(type="float", nullable=true)
99
     */
100
    private $avgSpeed;
101
102
    /**
103
     * @ORM\Column(type="float", nullable=true)
104
     */
105
    private $avgPace;
106
107
    /**
108
     * @ORM\Column(type="float", nullable=true)
109
     */
110
    private $minAltitude;
111
112
    /**
113
     * @ORM\Column(type="float", nullable=true)
114
     */
115
    private $maxAltitude;
116
117
    /**
118
     * @ORM\Column(type="float", nullable=true)
119
     */
120
    private $cumulativeElevationGain;
121
122
    /**
123
     * @var string|null
124
     *
125
     * @ApiProperty(iri="http://schema.org/contentUrl")
126
     * @Groups({"wander:list", "wander:item"})
127
     */
128
    public $contentUrl;
129
130
    public function __construct()
131
    {
132
        $this->images = new ArrayCollection();
133
    }
134
135
    public function getId(): ?int
136
    {
137
        return $this->id;
138
    }
139
140
    public function getTitle(): ?string
141
    {
142
        return $this->title;
143
    }
144
145
    public function setTitle(string $title): self
146
    {
147
        $this->title = $title;
148
149
        return $this;
150
    }
151
152
    public function getStartTime(): ?\DateTimeInterface
153
    {
154
        return $this->startTime;
155
    }
156
157
    public function setStartTime(\DateTimeInterface $startTime): self
158
    {
159
        $this->startTime = $startTime;
160
161
        return $this;
162
    }
163
164
    public function getEndTime(): ?\DateTimeInterface
165
    {
166
        return $this->endTime;
167
    }
168
169
    public function setEndTime(\DateTimeInterface $endTime): self
170
    {
171
        $this->endTime = $endTime;
172
173
        return $this;
174
    }
175
176
    public function getDescription(): ?string
177
    {
178
        return $this->description;
179
    }
180
181
    public function setDescription(?string $description): self
182
    {
183
        $this->description = $description;
184
185
        return $this;
186
    }
187
188
    public function getGpxFilename(): ?string
189
    {
190
        return $this->gpxFilename;
191
    }
192
193
    public function setGpxFilename(string $gpxFilename): self
194
    {
195
        $this->gpxFilename = $gpxFilename;
196
197
        return $this;
198
    }
199
200
    public function isTimeLengthSuspicious()
201
    {
202
        $interval = $this->startTime->diff($this->endTime, true);
203
        return $interval->h > 12;
204
    }
205
206
    /**
207
     * @return Collection|Image[]
208
     */
209
    public function getImages(): Collection
210
    {
211
        return $this->images;
212
    }
213
214
    public function addImage(Image $image): self
215
    {
216
        if (!$this->images->contains($image)) {
217
            $this->images[] = $image;
218
        }
219
220
        return $this;
221
    }
222
223
    public function removeImage(Image $image): self
224
    {
225
        $this->images->removeElement($image);
226
227
        return $this;
228
    }
229
230
    public function getDistance(): ?float
231
    {
232
        return $this->distance;
233
    }
234
235
    public function setDistance(?float $distance): self
236
    {
237
        $this->distance = $distance;
238
239
        return $this;
240
    }
241
242
    public function getAvgSpeed(): ?float
243
    {
244
        return $this->avgSpeed;
245
    }
246
247
    public function setAvgSpeed(?float $avgSpeed): self
248
    {
249
        $this->avgSpeed = $avgSpeed;
250
251
        return $this;
252
    }
253
254
    public function getAvgPace(): ?float
255
    {
256
        return $this->avgPace;
257
    }
258
259
    public function setAvgPace(?float $avgPace): self
260
    {
261
        $this->avgPace = $avgPace;
262
263
        return $this;
264
    }
265
266
    public function getMinAltitude(): ?float
267
    {
268
        return $this->minAltitude;
269
    }
270
271
    public function setMinAltitude(?float $minAltitude): self
272
    {
273
        $this->minAltitude = $minAltitude;
274
275
        return $this;
276
    }
277
278
    public function getMaxAltitude(): ?float
279
    {
280
        return $this->maxAltitude;
281
    }
282
283
    public function setMaxAltitude(?float $maxAltitude): self
284
    {
285
        $this->maxAltitude = $maxAltitude;
286
287
        return $this;
288
    }
289
290
    public function getCumulativeElevationGain(): ?float
291
    {
292
        return $this->cumulativeElevationGain;
293
    }
294
295
    public function setCumulativeElevationGain(?float $cumulativeElevationGain): self
296
    {
297
        $this->cumulativeElevationGain = $cumulativeElevationGain;
298
299
        return $this;
300
    }
301
302
    // TODO: We probably don't need this any more; I've replaced
303
    // existing uses with our seconds_to_hms Twig filter.
304
    public function getDuration(): ?CarbonInterval
305
    {
306
        if (!isset($this->startTime, $this->endTime)) {
307
            return null;
308
        }
309
310
        $difference = CarbonInterval::instance($this->startTime->diff($this->endTime));
311
        return $difference;
312
    }
313
314
    // Utilities
315
316
    // TODO: Put this in but didn't use it in the end; maybe I don't need it
317
    // as in Twig we can use wander.images.count anyway. Take out?
318
    public function getImageCount(): int
319
    {
320
        // https://stackoverflow.com/a/8511611/300836
321
        return $this->images->count();
322
    }
323
324
    public function __toString():string
325
    {
326
        $result = $this->title ?? '';
327
        if (isset($this->startTime)) {
328
            $result .= ' (' . $this->startTime->format('j M Y') . ')';
329
        }
330
        return $result;
331
    }
332
}
333