FindDirectoryService::getExtension()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
/**
3
 * Class FindDirectoryService
4
 *
5
 * @author Mauro Moreno <[email protected]>
6
 */
7
namespace MauroMoreno\FindBundle\Service;
8
9
/**
10
 * Class FindDirectoryService
11
 * @package MauroMoreno\FindBundle\Service
12
 */
13
class FindDirectoryService
14
{
15
16
    /**
17
     * @var string
18
     */
19
    private $pattern;
20
21
    /**
22
     * @var string
23
     */
24
    private $directory;
25
26
    /**
27
     * @var string
28
     */
29
    private $extension = "";
30
31
    /**
32
     * @var FinderInterface
33
     */
34
    private $finder;
35
36
    /**
37
     * @var ListerInterface
38
     */
39
    private $lister;
40
41
    /**
42
     * FindDirectoryService constructor.
43
     * @param FinderInterface $finder
44
     * @param ListerInterface $lister
45
     */
46 11
    public function __construct(
47
        FinderInterface $finder,
48
        ListerInterface $lister
49
    ) {
50 11
        $this->finder = $finder;
51 11
        $this->lister = $lister;
52 11
    }
53
54
    /**
55
     * FindDirectory find
56
     * @return array|bool
57
     */
58 11
    public function find()
59
    {
60 11
        if ($this->getPattern() === '') {
61 1
            throw new \InvalidArgumentException('Pattern cannot be empty.');
62
        }
63
64 10
        if (empty($this->getDirectory())) {
65 1
            throw new \InvalidArgumentException(
66
                'The target directory cannot be empty.'
67 1
            );
68
        }
69
70 9
        if (!is_dir($this->getDirectory())) {
71 1
            throw new \InvalidArgumentException(
72 1
                sprintf(
73 1
                    'The target directory "%s" does not exist.',
74 1
                    $this->getDirectory()
75 1
                )
76 1
            );
77
        }
78
79 8
        $return = false;
80
81 8
        $file_iterator = $this->lister->ls(
82 8
            $this->getDirectory(),
83 8
            $this->getExtension()
84 8
        );
85 8
        $count = count(iterator_to_array($file_iterator));
86
87 8
        if ($count > 0) {
88 6
            $return = [];
89 6
            foreach ($file_iterator as $file) {
90 6
                $found = $this->finder->find($this->getPattern(), $file);
91 6
                if ($found !== false) {
92 4
                    $return[] = [
93 4
                        'filename' => $found->getFilename(),
94 4
                        'pathname' => $found->getPathName()
95 4
                    ];;
96 4
                }
97 6
            }
98 6
        }
99
100 8
        return $return;
101
    }
102
103
    /**
104
     * @return string
105
     */
106 11
    public function getPattern()
107
    {
108 11
        return $this->pattern;
109
    }
110
111
    /**
112
     * @param $pattern
113
     * @return $this
114
     */
115 11
    public function setPattern($pattern)
116
    {
117 11
        $this->pattern = $pattern;
118
119 11
        return $this;
120
    }
121
122
    /**
123
     * @return string
124
     */
125 10
    public function getDirectory()
126
    {
127 10
        return $this->directory;
128
    }
129
130
    /**
131
     * @param $directory
132
     * @return $this
133
     */
134 11
    public function setDirectory($directory)
135
    {
136 11
        $this->directory = $directory;
137
138 11
        return $this;
139
    }
140
141
    /**
142
     * @return string
143
     */
144 8
    public function getExtension()
145
    {
146 8
        return $this->extension;
147
    }
148
149
    /**
150
     * @param $extension
151
     * @return $this
152
     */
153 5
    public function setExtension($extension)
154
    {
155 5
        $this->extension = $extension;
156
157 5
        return $this;
158
    }
159
160
}
161