Test Failed
Pull Request — master (#85)
by Matt
11:14
created

WanderDeleteListener   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
eloc 14
c 3
b 0
f 1
dl 0
loc 41
rs 10
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A preRemove() 0 11 2
A postRemove() 0 13 2
1
<?php
2
3
namespace App\EventListener;
4
5
use App\Entity\Wander;
6
use App\Service\GpxService;
7
use Psr\Log\LoggerInterface;
8
use Doctrine\Persistence\Event\LifecycleEventArgs;
9
10
class WanderDeleteListener
11
{
12
    /** @var LoggerInterface $logger */
13
    private $logger;
14
15
    /** @var GpxService */
16
    private $gpxService;
17
18
    public function __construct(LoggerInterface $logger, GpxService $gpxService)
19
    {
20
        $this->logger = $logger;
21
        $this->gpxService = $gpxService;
22
    }
23
24
    public function preRemove(
25
            Wander $wander,
26
            /** @scrutinizer ignore-unused */ LifecycleEventArgs $event
27
        ): void
28
    {
29
        // If we're about to delete a wander, we want to remove it as a featuring
30
        // wander from the related Image first, otherwise we'll break referential
31
        // integrity.
32
        $image = $wander->getFeaturedImage();
33
        if ($image !== null) {
34
            $image->setFeaturingWander(null);
35
        }
36
    }
37
38
    public function postRemove(
39
            Wander $wander,
40
            /** @scrutinizer ignore-unused */ LifecycleEventArgs $event
41
        ): void
42
    {
43
        $path = $this->gpxService->getFullGpxFilePathFromWander($wander);
44
        if (!file_exists($path)) {
45
            $this->logger->debug("Could not find GPX file " . $path . " to remove");
46
            return;
47
        }
48
49
        $this->logger->debug("Removing GPX file " . $path . " on deletion of wander " . $wander->getId());
50
        unlink($path);
51
    }
52
}