GlobSource   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 2
dl 0
loc 41
ccs 19
cts 19
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 2
B fetch() 0 21 5
1
<?php
2
/*
3
 * This file is part of the Backup package, an RunOpenCode project.
4
 *
5
 * (c) 2015 RunOpenCode
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * This project is fork of "kbond/php-backup", for full credits info, please
11
 * view CREDITS file that was distributed with this source code.
12
 */
13
namespace RunOpenCode\Backup\Source;
14
15
use RunOpenCode\Backup\Backup\File;
16
use RunOpenCode\Backup\Contract\SourceInterface;
17
use RunOpenCode\Backup\Exception\SourceException;
18
19
/**
20
 * Class GlobSource
21
 *
22
 * GlobSource source uses glob expressions to determine which files should be backed up.
23
 *
24
 * @package RunOpenCode\Backup\Source
25
 */
26
class GlobSource implements SourceInterface
27
{
28
    /**
29
     * @var string[]
30
     */
31
    private $globs;
32
33 38
    public function __construct($glob)
34
    {
35 38
        if (is_array($glob)) {
36 2
            $this->globs = $glob;
37 1
        } else {
38 36
            $this->globs = array($glob => str_replace('*', '', $glob));
39
        }
40 38
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45 38
    public function fetch()
46
    {
47 38
        $backupFiles = array();
48
49 38
        foreach ($this->globs as $glob => $rootPath) {
50 38
            $files = glob($glob, GLOB_ERR);
51
52 38
            if ($files === false) {
53
54 4
                throw new SourceException(sprintf('GlobSource expression "%s" is not correct and it fails getting list of files.', $glob));
55
56 36
            } elseif (count($files)) {
57
58 36
                foreach ($files as $file) {
59 36
                    $backupFiles[] = File::fromLocal($file, $rootPath);
60 18
                }
61 18
            }
62 18
        }
63
64 36
        return $backupFiles;
65
    }
66
}
67