Theme::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
namespace TheCodingMachine\CMS\StaticRegistry\Loaders;
3
4
use Symfony\Component\Yaml\Yaml;
5
6
class Theme
7
{
8
    /**
9
     * @var string
10
     */
11
    private $path;
12
    /**
13
     * @var string[]
14
     */
15
    private $zones;
16
17
    /**
18
     * @param string[] $zones
19
     */
20
    public function __construct(string $path, array $zones)
21
    {
22
        $this->path = $path;
23
        $this->zones = $zones;
24
    }
25
26
    public static function fromDirectory(string $path): self
27
    {
28
        if (!is_dir($path)) {
29
            throw new UnableToLoadFileException('Cannot find directory '.$path);
30
        }
31
        $path = rtrim($path, '/\\');
32
        $configPath = $path.'/config.yml';
33
        if (!is_file($configPath)) {
34
            throw new UnableToLoadFileException('Cannot find config.yml in theme '.$path);
35
        }
36
37
        $yaml = Yaml::parse(file_get_contents($configPath));
38
39
        $compulsoryFields = ['zones'];
40
41
        foreach ($compulsoryFields as $field) {
42
            if (!isset($yaml[$field])) {
43
                throw new UnableToLoadFileException('Missing field '.$field.' in YAML file '.$configPath);
44
            }
45
        }
46
47
        return new self(
48
            $path,
49
            $yaml['zones']
50
        );
51
    }
52
53
    /**
54
     * Returns true if $path is a theme directory.
55
     *
56
     * @param string $path The path, with no trailing /
57
     * @return bool
58
     */
59
    public static function existsInDirectory(string $path): bool
60
    {
61
        $configPath = $path.'/config.yml';
62
        return file_exists($configPath);
63
    }
64
65
    /**
66
     * @return string
67
     */
68
    public function getName(): string
69
    {
70
        return basename($this->path);
71
    }
72
73
    /**
74
     * @return string
75
     */
76
    public function getPath(): string
77
    {
78
        return $this->path;
79
    }
80
81
    /**
82
     * @return string[]
83
     */
84
    public function getZones(): array
85
    {
86
        return $this->zones;
87
    }
88
}
89