ConfigProvider::setFinder()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Vivait\TenantBundle\Provider;
4
5
use Symfony\Component\Finder\Finder;
6
use Symfony\Component\Finder\SplFileInfo;
7
use Vivait\TenantBundle\Model\Tenant;
8
9
/**
10
 * @see ConfigProviderSpec
11
 */
12
final class ConfigProvider implements TenantProvider
13
{
14
    /**
15
     * @var string
16
     */
17
    private $path;
18
19
    /**
20
     * @var Finder
21
     */
22
    private $finder;
23
24
    /**
25
     * @var string
26
     */
27
    private $file_pattern;
28
29
    /**
30
     * @param string $path
31
     */
32
    public function __construct($path, $file_pattern = '/^config_tenant_(?P<tenant>.+?)\.(.*?)$/', Finder $finder = null)
33
    {
34
        $this->path = $path;
35
        $this->file_pattern = $file_pattern;
36
37
        if ($finder === null) {
38
            $this->finder = new Finder();
39
        }
40
        else {
41
            $this->finder = $finder;
42
        }
43
    }
44
45
    /**
46
     * @inheritDoc
47
     */
48
    public function loadTenants() {
49
        if (!file_exists($this->path)) {
50
            // TODO: Throw an error or log or something
51
            return [];
52
        }
53
54
        /* @var $files SplFileInfo[] */
55
        $files = $this->finder
56
            ->files()
57
            ->in($this->path)
58
            ->name($this->file_pattern);
59
60
        $tenants = [];
61
62
        foreach ($files as $file) {
63
            if (!preg_match($this->file_pattern, $file->getFilename(), $matches)) {
64
                throw new \RuntimeException(sprintf('Could not detect tenant key based on filename "%s"', $file->getFilename()));
65
            }
66
67
            $tenants[$matches['tenant']] = new Tenant($matches['tenant']);
68
        }
69
70
        return $tenants;
71
    }
72
73
    /**
74
     * Sets path
75
     * @param string $path
76
     * @return $this
77
     */
78
    public function setPath( $path )
79
    {
80
        $this->path = $path;
81
82
        return $this;
83
    }
84
85
    /**
86
     * Gets finder
87
     * @return Finder
88
     */
89
    public function getFinder()
90
    {
91
        return $this->finder;
92
    }
93
94
    /**
95
     * Sets finder
96
     * @param Finder $finder
97
     * @return $this
98
     */
99
    public function setFinder( $finder )
100
    {
101
        $this->finder = $finder;
102
103
        return $this;
104
    }
105
}
106