Completed
Push — master ( ac238c...da4625 )
by Jan
04:59
created

BuiltinAttachmentsFinder::configureOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 6
rs 10
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 App\Services\Attachments\AttachmentPathResolver;
36
use Symfony\Component\Finder\Finder;
37
use Symfony\Component\HttpKernel\KernelInterface;
38
use Symfony\Component\OptionsResolver\OptionsResolver;
39
40
/**
41
 * This service is used to find builtin attachment ressources
42
 * @package App\Services
43
 */
44
class BuiltinAttachmentsFinder
45
{
46
    protected $pathResolver;
47
48
    public function __construct(KernelInterface $kernel, AttachmentPathResolver $pathResolver)
0 ignored issues
show
Unused Code introduced by
The parameter $kernel 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

48
    public function __construct(/** @scrutinizer ignore-unused */ KernelInterface $kernel, AttachmentPathResolver $pathResolver)

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...
49
    {
50
        $this->pathResolver = $pathResolver;
51
    }
52
53
    protected function configureOptions(OptionsResolver $resolver)
54
    {
55
        $resolver->setDefaults([
56
            'limit' => 15,  //Given only 15 entries
57
            'filename_filter' => '', //Filter the filenames. For example *.jpg to only get jpegs. Can also be an array
58
            'placeholders' => Attachment::BUILTIN_PLACEHOLDER, //By default use all builtin ressources
59
        ]);
60
    }
61
62
    public function find(string $keyword, array $options) : array
63
    {
64
        $finder = new Finder();
65
66
        $resolver = new OptionsResolver();
67
        $this->configureOptions($resolver);
68
        $options = $resolver->resolve($options);
69
70
        //We search only files
71
        $finder->files();
72
        $finder->in($this->pathResolver->getFootprintsPath());
73
74
        //Apply filter if needed
75
        if (!empty($options['filename_filter'])) {
76
            $finder->name($options['filename_filter']);
77
        }
78
79
        $finder->path($keyword);
80
81
        $arr = [];
82
83
        $limit = $options['limit'];
84
85
        foreach ($finder as $file) {
86
            if ($limit <= 0) {
87
                break;
88
            }
89
            $arr[] = $this->pathResolver->realPathToPlaceholder($file->getPathname());
90
            $limit--;
91
        }
92
93
        return $arr;
94
    }
95
96
}