ImageFinder   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 1
dl 0
loc 45
ccs 14
cts 14
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 2
A __invoke() 0 13 3
1
<?php
2
namespace Germania\ImageFinder;
3
4
use Symfony\Component\Finder\Finder;
5
6
class ImageFinder
7
{
8
    /**
9
     * @var Finder
10
     */
11
    public $finder;
12
13
    /**
14
     * @var array
15
     */
16
    public $extensions = array('jpe?g', 'gif', 'png', 'svgz?');
17
18
19
    /**
20
     * @param Finder $finder     Symfony Finder instance
21
     * @param array  $extensions Optional: Array with allowed file extensions. Default: `array('jpe?g', 'gif', 'png', 'svgz?')`
22
     */
23 5
    public function __construct( Finder $finder, $extensions = array() )
24
    {
25 5
        $this->finder = $finder;
26 5
        $this->extensions = $extensions ?: $this->extensions;
27 5
    }
28
29
30
    /**
31
     * See Symfony Finder API: http://symfony.com/doc/current/components/finder.html
32
     *
33
     * @param  string|array $path  Path or pathes
34
     * @param  string       $regex Optional: Custom file name regex
35
     * @return \Iterator|array
36
     */
37 5
    public function __invoke( $path, $regex = null )
38
    {
39 5
        $regex = $regex ?: '/\.(' . implode('|', $this->extensions) . ')$/i';
40
41 5
        return realpath( $path )
42 5
        ?  $this->finder
43 5
            ->ignoreUnreadableDirs()
44 5
            ->in( $path )
45 5
            ->ignoreDotFiles(true)
46 5
            ->files()
47 5
            ->name( $regex )
48 5
        : array();
49
    }
50
}
51