YamlProvider   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 2
dl 0
loc 42
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A setResource() 0 3 1
A loadTenants() 0 16 3
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