Passed
Branch master (350f1b)
by Jan
04:53
created

AttachmentDeleteListener   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 26
dl 0
loc 75
rs 10
c 3
b 0
f 0
wmc 11

4 Methods

Rating   Name   Duplication   Size   Complexity  
A preUpdateHandler() 0 19 4
A __construct() 0 5 1
A preRemoveHandler() 0 12 3
A postRemoveHandler() 0 11 3
1
<?php
2
/**
3
 * This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
4
 *
5
 * Copyright (C) 2019 - 2020 Jan Böhmer (https://github.com/jbtronics)
6
 *
7
 * This program is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU Affero General Public License as published
9
 * by the Free Software Foundation, either version 3 of the License, or
10
 * (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU Affero General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License
18
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19
 */
20
21
declare(strict_types=1);
22
23
/**
24
 * This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
25
 *
26
 * Copyright (C) 2019 Jan Böhmer (https://github.com/jbtronics)
27
 *
28
 * This program is free software; you can redistribute it and/or
29
 * modify it under the terms of the GNU General Public License
30
 * as published by the Free Software Foundation; either version 2
31
 * of the License, or (at your option) any later version.
32
 *
33
 * This program is distributed in the hope that it will be useful,
34
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
35
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
36
 * GNU General Public License for more details.
37
 *
38
 * You should have received a copy of the GNU General Public License
39
 * along with this program; if not, write to the Free Software
40
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
41
 */
42
43
namespace App\EntityListeners;
44
45
use App\Entity\Attachments\Attachment;
46
use App\Services\Attachments\AttachmentManager;
47
use App\Services\Attachments\AttachmentPathResolver;
48
use App\Services\Attachments\AttachmentReverseSearch;
49
use Doctrine\ORM\Event\LifecycleEventArgs;
50
use Doctrine\ORM\Event\PreUpdateEventArgs;
51
use Doctrine\ORM\Mapping as ORM;
52
use Doctrine\ORM\Mapping\PostRemove;
53
use Doctrine\ORM\Mapping\PreUpdate;
54
use SplFileInfo;
55
56
/**
57
 * This listener watches for changes on attachments and deletes the files associated with an attachment, that are not
58
 * used any more. This can happens after an attachment is delteted or the path is changed.
59
 */
60
class AttachmentDeleteListener
61
{
62
    protected $attachmentReverseSearch;
63
    protected $attachmentHelper;
64
    protected $pathResolver;
65
66
    public function __construct(AttachmentReverseSearch $attachmentReverseSearch, AttachmentManager $attachmentHelper, AttachmentPathResolver $pathResolver)
67
    {
68
        $this->attachmentReverseSearch = $attachmentReverseSearch;
69
        $this->attachmentHelper = $attachmentHelper;
70
        $this->pathResolver = $pathResolver;
71
    }
72
73
    /**
74
     * Removes the file associated with the attachment, if the file associated with the attachment changes.
75
     *
76
     * @PreUpdate
77
     */
78
    public function preUpdateHandler(Attachment $attachment, PreUpdateEventArgs $event): void
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

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

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...
79
    {
80
        if ($event->hasChangedField('path')) {
81
            $old_path = $event->getOldValue('path');
82
83
            //Dont delete file if the attachment uses a builtin ressource:
84
            if (Attachment::checkIfBuiltin($old_path)) {
85
                return;
86
            }
87
88
            $real_path = $this->pathResolver->placeholderToRealPath($old_path);
89
90
            //If the attachment does not point to a valid file, ignore it!
91
            if (null === $real_path) {
92
                return;
93
            }
94
95
            $file = new SplFileInfo($real_path);
96
            $this->attachmentReverseSearch->deleteIfNotUsed($file);
97
        }
98
    }
99
100
    /**
101
     * Ensure that attachments are not used in preview, so that they can be deleted (without integrity violation).
102
     * @ORM\PreRemove()
103
     */
104
    public function preRemoveHandler(Attachment $attachment, LifecycleEventArgs $event): void
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

104
    public function preRemoveHandler(Attachment $attachment, /** @scrutinizer ignore-unused */ LifecycleEventArgs $event): void

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...
105
    {
106
        //Ensure that the attachment that will be deleted, is not used as preview picture anymore...
107
        $attachment_holder = $attachment->getElement();
108
109
        if ($attachment_holder === null) {
110
            return;
111
        }
112
113
        //... Otherwise remove it as preview picture
114
        if ($attachment_holder->getMasterPictureAttachment() === $attachment) {
115
            $attachment_holder->setMasterPictureAttachment(null);
116
        }
117
    }
118
119
    /**
120
     * Removes the file associated with the attachment, after the attachment was deleted.
121
     *
122
     * @PostRemove
123
     */
124
    public function postRemoveHandler(Attachment $attachment, LifecycleEventArgs $event): void
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

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

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...
125
    {
126
        //Dont delete file if the attachment uses a builtin ressource:
127
        if ($attachment->isBuiltIn()) {
128
            return;
129
        }
130
131
        $file = $this->attachmentHelper->attachmentToFile($attachment);
132
        //Only delete if the attachment has a valid file.
133
        if (null !== $file) {
134
            $this->attachmentReverseSearch->deleteIfNotUsed($file);
135
        }
136
    }
137
}
138