This repository has been archived by the owner on Mar 28, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ThemeGenerator.php
149 lines (128 loc) · 3.14 KB
/
ThemeGenerator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<?php
/**
* Copyright (C) GrizzIT, Inc. All rights reserved.
* See LICENSE for license details.
*/
namespace Ulrack\Cli\Generator;
use Ulrack\Cli\Common\Theme\StyleEnum;
use Ulrack\Cli\Common\Theme\ThemeInterface;
use Ulrack\Cli\Exception\NotInitializedException;
use Ulrack\Cli\Common\Factory\ThemeFactoryInterface;
use Ulrack\Cli\Common\Generator\ThemeGeneratorInterface;
class ThemeGenerator implements ThemeGeneratorInterface
{
/**
* Contains the theme factory.
*
* @var ThemeFactoryInterface
*/
private $themeFactory;
/**
* Contains the current theme.
*
* @var ThemeInterface
*/
private $theme;
/**
* Constructor.
*
* @param ThemeFactoryInterface $themeFactory
*/
public function __construct(
ThemeFactoryInterface $themeFactory
) {
$this->themeFactory = $themeFactory;
}
/**
* Creates a new theme.
*
* @return ThemeGeneratorInterface
*/
public function init(): ThemeGeneratorInterface
{
$this->theme = $this->themeFactory->createTheme();
return $this;
}
/**
* Reinitializes a theme.
*
* @param ThemeInterface $theme
*
* @return ThemeGeneratorInterface
*/
public function reinit(ThemeInterface $theme): ThemeGeneratorInterface
{
$this->theme = $theme;
return $this;
}
/**
* Checks whether the theme is created before certain actions can be performed.
*
* @return void
*
* @throws NotInitializedException When the theme is not initialized.
*/
private function preAddCheck(): void
{
if ($this->theme === null) {
throw new NotInitializedException(
self::class,
'init',
'theme'
);
}
}
/**
* Adds a new style to the current theme.
*
* @param string $key
* @param StyleEnum ...$styles
*
* @return ThemeGeneratorInterface
*
* @throws NotInitializedException When the theme is not initialized.
*/
public function addStyle(
string $key,
StyleEnum ...$styles
): ThemeGeneratorInterface {
$this->preAddCheck();
$this->theme->addStyle(
$key,
$this->themeFactory->createStyle(...$styles)
);
return $this;
}
/**
* Adds a variable to the theme.
*
* @param string $key
* @param mixed $value
*
* @return ThemeGeneratorInterface
*
* @throws NotInitializedException When the theme is not initialized.
*/
public function addVariable(
string $key,
$value
): ThemeGeneratorInterface {
$this->preAddCheck();
$this->theme->addVariable($key, $value);
return $this;
}
/**
* Retrieves the current generating theme.
*
* @return ThemeInterface
*
* @throws NotInitializedException When the theme is not initialized.
*/
public function getTheme(): ThemeInterface
{
$this->preAddCheck();
$theme = $this->theme;
$this->theme = null;
return $theme;
}
}