Bristol SU Support Package
ModuleBuilder.php
Go to the documentation of this file.
1 <?php
2 
4 
10 use BristolSU\Support\Module\Contracts\ModuleBuilder as ModuleBuilderContract;
11 use \BristolSU\Support\Module\Contracts\Module as ModuleContract;
14 use Exception;
17 
21 class ModuleBuilder implements ModuleBuilderContract
22 {
23 
29  private $module;
30 
37 
43  private $config;
44 
50  private $alias;
51 
58 
65 
72 
78  private $serviceRequest;
79 
91  Repository $config,
96  {
97  $this->module = $module;
98  $this->permissionRepository = $permissionRepository;
99  $this->config = $config;
100  $this->eventRepository = $eventRepository;
101  $this->completionConditionRepository = $completionConditionRepository;
102  $this->moduleSettingsStore = $moduleSettingsStore;
103  $this->serviceRequest = $serviceRequest;
104  }
105 
111  public function create(string $alias)
112  {
113  $this->alias = $alias;
114  }
115 
122  protected function getAlias(): string
123  {
124  if ($this->alias === null) {
125  throw new Exception('Set an alias before using the builder');
126  }
127  return $this->alias;
128  }
129 
135  public function setAlias()
136  {
137  $this->module->setAlias($this->getAlias());
138  }
139 
145  public function setPermissions()
146  {
147  $this->module->setPermissions(
148  $this->permissionRepository->forModule($this->getAlias())
149  );
150  }
151 
157  public function setName()
158  {
159  $this->module->setName(
160  $this->config->get($this->getAlias().'.name')
161  );
162  }
163 
169  public function setDescription()
170  {
171  $this->module->setDescription(
172  $this->config->get($this->getAlias().'.description')
173  );
174  }
175 
181  public function setSettings()
182  {
183  $this->module->setSettings(
184  (new VFGTransformer)->transformToArray(
185  $this->moduleSettingsStore->get($this->getAlias())
186  )
187  );
188  }
189 
195  public function setTriggers()
196  {
197  $this->module->setTriggers(
198  array_filter($this->eventRepository
199  ->allForModule($this->getAlias()),
200  function($event) {
201  return in_array(TriggerableEvent::class, class_implements($event['event']));
202  })
203  );
204  }
205 
211  public function setServices()
212  {
213  $this->module->setServices([
214  'required' => $this->serviceRequest->getRequired($this->getAlias()),
215  'optional' => $this->serviceRequest->getOptional($this->getAlias())
216  ]);
217  }
218 
224  public function setFor()
225  {
226  $this->module->setFor(
227  $this->config->get($this->getAlias().'.for', 'user')
228  );
229  }
230 
236  public function getModule(): ModuleContract
237  {
238  return $this->module;
239  }
240 
246  public function setCompletionConditions()
247  {
248  $this->module->setCompletionConditions(
249  collect($this->completionConditionRepository->getAllForModule($this->getAlias()))->toArray()
250  );
251  }
252 }
__construct(ModuleContract $module, PermissionRepository $permissionRepository, Repository $config, EventRepository $eventRepository, CompletionConditionRepository $completionConditionRepository, ModuleSettingsStore $moduleSettingsStore, ServiceRequest $serviceRequest)