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

AttachmentReverseSearch   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 14
c 2
b 0
f 0
dl 0
loc 44
rs 10
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A deleteIfNotUsed() 0 11 2
A findAttachmentsByFile() 0 9 1
A __construct() 0 4 1
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\Services;
33
34
use App\Entity\Attachments\Attachment;
35
use Doctrine\ORM\EntityManagerInterface;
36
use Symfony\Component\Filesystem\Filesystem;
37
use Symfony\Component\HttpFoundation\File\File;
38
39
/**
40
 * This service provides functions to find attachments via an reverse search based on a file.
41
 * @package App\Services
42
 */
43
class AttachmentReverseSearch
44
{
45
    protected $em;
46
    protected $attachment_helper;
47
48
    public function __construct(EntityManagerInterface $em, AttachmentHelper $attachmentHelper)
49
    {
50
        $this->em = $em;
51
        $this->attachment_helper = $attachmentHelper;
52
    }
53
54
    /**
55
     * Find all attachments that use the given file
56
     * @param File $file
57
     * @return Attachment[]
58
     */
59
    public function findAttachmentsByFile(\SplFileInfo $file) : array
60
    {
61
        //Path with %MEDIA%
62
        $relative_path_new = $this->attachment_helper->realPathToPlaceholder($file->getPathname());
63
        //Path with %BASE%
64
        $relative_path_old = $this->attachment_helper->realPathToPlaceholder($file->getPathname(), true);
65
66
        $repo = $this->em->getRepository(Attachment::class);
67
        return $repo->findBy(['path' => [$relative_path_new, $relative_path_old]]);
68
    }
69
70
    /**
71
     * Deletes the given file if it is not used by more than $threshold attachments
72
     * @param \SplFileInfo $file The file that should be removed
73
     * @param int $threshold The threshold used, to determine if a file should be deleted or not.
74
     * @return bool True, if the file was delete. False if not.
75
     */
76
    public function deleteIfNotUsed(\SplFileInfo $file, int $threshold = 0) : bool
77
    {
78
        /* When the file is used more then $threshold times, don't delete it */
79
        if (count($this->findAttachmentsByFile($file)) > $threshold) {
80
            return false;
81
        }
82
83
        $fs = new Filesystem();
84
        $fs->remove($file);
85
86
        return true;
87
    }
88
}