Completed
Pull Request — master (#11)
by Mauro
13:30
created

FindDirectoryService::find()   C

Complexity

Conditions 7
Paths 5

Size

Total Lines 44
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 25
CRAP Score 7

Importance

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