Completed
Push — master ( 87527d...6b8782 )
by Jan
03:58
created

AttachmentDeleteListener   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
c 1
b 0
f 0
dl 0
loc 40
rs 10
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A preUpdateHandler() 0 5 2
A __construct() 0 4 1
A postRemoveHandler() 0 6 2
1
<?php
2
/**
3
 *
4
 * part-db version 0.1
5
 * Copyright (C) 2005 Christoph Lechner
6
 * http://www.cl-projects.de/
7
 *
8
 * part-db version 0.2+
9
 * Copyright (C) 2009 K. Jacobs and others (see authors.php)
10
 * http://code.google.com/p/part-db/
11
 *
12
 * Part-DB Version 0.4+
13
 * Copyright (C) 2016 - 2019 Jan Böhmer
14
 * https://github.com/jbtronics
15
 *
16
 * This program is free software; you can redistribute it and/or
17
 * modify it under the terms of the GNU General Public License
18
 * as published by the Free Software Foundation; either version 2
19
 * of the License, or (at your option) any later version.
20
 *
21
 * This program is distributed in the hope that it will be useful,
22
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24
 * GNU General Public License for more details.
25
 *
26
 * You should have received a copy of the GNU General Public License
27
 * along with this program; if not, write to the Free Software
28
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
29
 *
30
 */
31
32
namespace App\EntityListeners;
33
34
35
use App\Entity\Attachments\Attachment;
36
use App\Services\AttachmentHelper;
37
use App\Services\AttachmentReverseSearch;
38
use Doctrine\ORM\Event\LifecycleEventArgs;
39
use Doctrine\ORM\Event\PreUpdateEventArgs;
40
use Doctrine\ORM\Mapping\PostRemove;
41
use Doctrine\ORM\Mapping\PreUpdate;
42
43
/**
44
 * This listener watches for changes on attachments and deletes the files associated with an attachment, that are not
45
 * used any more. This can happens after an attachment is delteted or the path is changed.
46
 * @package App\EntityListeners
47
 */
48
class AttachmentDeleteListener
49
{
50
    protected $attachmentReverseSearch;
51
    protected $attachmentHelper;
52
53
    public function __construct(AttachmentReverseSearch $attachmentReverseSearch, AttachmentHelper $attachmentHelper)
54
    {
55
        $this->attachmentReverseSearch = $attachmentReverseSearch;
56
        $this->attachmentHelper = $attachmentHelper;
57
    }
58
59
    /**
60
     * Removes the file associated with the attachment, if the file associated with the attachment changes.
61
     * @param Attachment $attachment
62
     * @param PreUpdateEventArgs $event
63
     *
64
     * @PreUpdate
65
     */
66
    public function preUpdateHandler(Attachment $attachment, PreUpdateEventArgs $event)
0 ignored issues
show
Unused Code introduced by
The parameter $attachment is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

66
    public function preUpdateHandler(/** @scrutinizer ignore-unused */ Attachment $attachment, PreUpdateEventArgs $event)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
67
    {
68
        if ($event->hasChangedField('path')) {
69
            $file = new \SplFileInfo($this->attachmentHelper->placeholderToRealPath($event->getOldValue('path')));
70
            $this->attachmentReverseSearch->deleteIfNotUsed($file);
71
        }
72
    }
73
74
    /**
75
     * Removes the file associated with the attachment, after the attachment was deleted.
76
     *
77
     * @param Attachment $attachment
78
     * @param LifecycleEventArgs $event
79
     *
80
     * @PostRemove
81
     */
82
    public function postRemoveHandler(Attachment $attachment, LifecycleEventArgs $event)
0 ignored issues
show
Unused Code introduced by
The parameter $event is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

82
    public function postRemoveHandler(Attachment $attachment, /** @scrutinizer ignore-unused */ LifecycleEventArgs $event)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
83
    {
84
        $file = $this->attachmentHelper->attachmentToFile($attachment);
85
        //Only delete if the attachment has a valid file.
86
        if ($file !== null) {
87
            $this->attachmentReverseSearch->deleteIfNotUsed($file);
88
        }
89
    }
90
91
}