Completed
Push — master ( 6e1259...c4a3c9 )
by Nikola
02:37
created

GlobSource::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 8
rs 9.4286
cc 2
eloc 5
nc 2
nop 1
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 Psr\Log\LoggerInterface;
16
use RunOpenCode\Backup\Backup\File;
17
use RunOpenCode\Backup\Contract\LoggerAwareInterface;
18
use RunOpenCode\Backup\Contract\SourceInterface;
19
use RunOpenCode\Backup\Exception\SourceException;
20
use RunOpenCode\Backup\Log\LoggerAwareTrait;
21
22
/**
23
 * Class GlobSource
24
 *
25
 * GlobSource source uses glob expressions to determine which files should be backed up.
26
 *
27
 * @package RunOpenCode\Backup\Source
28
 */
29
class GlobSource implements SourceInterface, LoggerAwareInterface
30
{
31
    use LoggerAwareTrait;
32
33
    private $globs;
34
35
    public function __construct($glob)
36
    {
37
        if (is_array($glob)) {
38
            $this->globs = $glob;
39
        } else {
40
            $this->globs = array($glob => str_replace('*', '', $glob));
41
        }
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47
    public function fetch()
48
    {
49
        $backupFiles = array();
50
51
        foreach ($this->globs as $glob => $rootPath) {
52
            $files = @glob($glob, GLOB_ERR);
53
54
            if ($files === false) {
55
56
                $this->getLogger()->error(sprintf('GlobSource expression "%s" is not correct and it fails getting list of files.', $glob), array(
57
                    'glob' => $glob
58
                ));
59
60
                throw new SourceException();
61
62
            } elseif (count($files)) {
63
64
                foreach ($files as $file) {
65
                    $backupFiles[] = File::fromLocal($file, $rootPath);
66
                }
67
            }
68
        }
69
70
        return $backupFiles;
71
    }
72
}