YamlProvider::loadTenants()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 0
dl 0
loc 16
rs 9.7333
c 0
b 0
f 0
1
<?php
2
3
namespace Vivait\TenantBundle\Provider;
4
5
use Symfony\Component\Yaml\Yaml;
6
use Vivait\TenantBundle\Model\Tenant;
7
use spec\Vivait\TenantBundle\TenantStrategy\YamlProviderSpec;
8
9
/**
10
 * @see YamlProviderSpec
11
 */
12
final class YamlProvider implements TenantProvider
13
{
14
    /**
15
     * @var string
16
     */
17
    private $resource;
18
19
    /**
20
     * @param null|string $resource The filename of the YAML containing the tenants
21
     */
22
    public function __construct($resource = null)
23
    {
24
        $this->resource = $resource;
25
    }
26
27
    /**
28
     * @param null|string $resource The filename of the YAML containing the tenants
29
     */
30
    public function setResource($resource) {
31
        $this->resource = $resource;
32
    }
33
34
    /**
35
     * @inheritDoc
36
     */
37
    public function loadTenants() {
38
        $yaml = Yaml::parse(file_get_contents($this->resource));
39
40
        if (!is_array($yaml)) {
41
            throw new \RuntimeException('YAML file didn\'t contain an array of tenants');
42
        }
43
44
        $tenants = [];
45
46
        // Convert them to Tenant objects
47
        foreach ($yaml as $tenant) {
48
            $tenants[$tenant] = new Tenant($tenant);
49
        }
50
51
        return $tenants;
52
    }
53
}
54