Completed
Push — master ( dca29b...9d1c68 )
by Mauro
03:55
created

Finder::createRegExp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 1
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 5
    public function find($pattern, \SplFileInfo $fileInfo)
25
    {
26 5
        if (!is_string($pattern)) {
27 1
            throw new \InvalidArgumentException('Pattern should be an string');
28
        }
29 4
        $pattern = $this->createRegExp($pattern);
30 4
        $data = fopen($fileInfo->getPathName(), 'r');
31
32 4
        $return = false;
33
34 4
        while ($line = fgets($data, 1024)) {
35 4
            if (preg_match($pattern, $line)) {
36 3
                $return = $fileInfo;
37 3
            }
38 4
        }
39
40 4
        return $return;
41
    }
42
43
    /**
44
     * Create Regular Expression
45
     *
46
     * @param string $pattern
47
     *
48
     * @return string
49
     */
50 4
    private function createRegExp($pattern)
51
    {
52 4
        $regexp = "/$pattern/";
53 4
        return $regexp;
54
    }
55
56
}
57