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

Finder::find()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 18
rs 9.2
cc 4
eloc 10
nc 4
nop 2
1
<?php
2
/**
3
 * Class Finder
4
 *
5
 * @author Mauro Moreno <[email protected]>
6
 */
7
namespace MauroMoreno\FindBundle\Service;
8
9
/**
10
 * Class Finder
11
 * @package MauroMoreno\FindBundle\Service
12
 */
13
class Finder implements FinderInterface
14
{
15
16
    /**
17
     * Find method
18
     *
19
     * @param string $pattern
20
     * @param \SplFileInfo $fileInfo
21
     *
22
     * @return bool|\SplFileInfo
23
     */
24
    public function find($pattern, \SplFileInfo $fileInfo)
25
    {
26
        if (!is_string($pattern)) {
27
            throw new \InvalidArgumentException('Pattern should be an string');
28
        }
29
        $pattern = $this->createRegExp($pattern);
30
        $data = fopen($fileInfo->getPathName(), 'r');
31
32
        $return = false;
33
34
        while ($line = fgets($data, 1024)) {
35
            if (preg_match($pattern, $line)) {
36
                $return = $fileInfo;
37
            }
38
        }
39
40
        return $return;
41
    }
42
43
    /**
44
     * Create Regular Expression
45
     *
46
     * @param string $pattern
47
     *
48
     * @return string
49
     */
50
    private function createRegExp($pattern)
51
    {
52
        $regexp = "/$pattern/";
53
        return $regexp;
54
    }
55
56
}
57