Completed
Push — master ( f5f581...c955a3 )
by Jan
08:07 queued 04:02
created

AttachmentReverseSearch::findAttachmentsByFile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 9
rs 10
c 0
b 0
f 0
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\Attachments;
33
34
use App\Entity\Attachments\Attachment;
35
use App\Services\Attachments\AttachmentPathResolver;
36
use App\Services\Attachments\AttachmentURLGenerator;
37
use Doctrine\ORM\EntityManagerInterface;
38
use Liip\ImagineBundle\Imagine\Cache\CacheManager;
39
use Symfony\Component\Filesystem\Filesystem;
40
use Symfony\Component\HttpFoundation\File\File;
41
42
/**
43
 * This service provides functions to find attachments via an reverse search based on a file.
44
 * @package App\Services
45
 */
46
class AttachmentReverseSearch
47
{
48
    protected $em;
49
    protected $pathResolver;
50
    protected $cacheManager;
51
    protected $attachmentURLGenerator;
52
53
    public function __construct(EntityManagerInterface $em, AttachmentPathResolver $pathResolver,
54
                                CacheManager $cacheManager, AttachmentURLGenerator $attachmentURLGenerator)
55
    {
56
        $this->em = $em;
57
        $this->pathResolver = $pathResolver;
58
        $this->cacheManager = $cacheManager;
59
        $this->attachmentURLGenerator = $attachmentURLGenerator;
60
    }
61
62
    /**
63
     * Find all attachments that use the given file
64
     * @param \SplFileInfo $file The file for which is searched
65
     * @return Attachment[] An list of attachments that use the given file.
66
     */
67
    public function findAttachmentsByFile(\SplFileInfo $file) : array
68
    {
69
        //Path with %MEDIA%
70
        $relative_path_new = $this->pathResolver->realPathToPlaceholder($file->getPathname());
71
        //Path with %BASE%
72
        $relative_path_old = $this->pathResolver->realPathToPlaceholder($file->getPathname(), true);
73
74
        $repo = $this->em->getRepository(Attachment::class);
75
        return $repo->findBy(['path' => [$relative_path_new, $relative_path_old]]);
76
    }
77
78
    /**
79
     * Deletes the given file if it is not used by more than $threshold attachments
80
     * @param \SplFileInfo $file The file that should be removed
81
     * @param int $threshold The threshold used, to determine if a file should be deleted or not.
82
     * @return bool True, if the file was delete. False if not.
83
     */
84
    public function deleteIfNotUsed(\SplFileInfo $file, int $threshold = 1) : bool
85
    {
86
87
        /* When the file is used more then $threshold times, don't delete it */
88
        if (count($this->findAttachmentsByFile($file)) > $threshold) {
89
            return false;
90
        }
91
92
        //Remove file from liip image cache
93
        $this->cacheManager->remove($this->attachmentURLGenerator->absolutePathToAssetPath($file->getPathname()));
94
95
        $fs = new Filesystem();
96
        $fs->remove($file);
97
98
99
100
        return true;
101
    }
102
}