Bristol SU Support Package
Action.php
Go to the documentation of this file.
1 <?php
2 
4 
8 use Exception;
13 
17 abstract class Action implements ShouldQueue, RecordsHistory
18 {
20 
24  private $data;
25 
29  private $response;
30 
31  public function __construct(array $data = [])
32  {
33  $this->data = $data;
34  }
35 
43  abstract public static function options(): Form;
44 
45  public function handle()
46  {
47  try {
48  $this->response = app()->call([$this, 'run']);
49  } catch (Exception $e) {
50  $this->response = ActionResponse::failure(($e->getMessage() === '' ? 'An error was thrown during processing' : $e->getMessage()));
51  }
52  if ($this instanceof RecordsHistory) {
53  $this->saveHistory($this->getResponse());
54  }
55  }
56 
62  public function getResponse(): ?ActionResponse
63  {
64  return $this->response;
65  }
66 
74  public function option(string $key, $default = null)
75  {
76  if (array_key_exists($key, $this->data)) {
77  return $this->data[$key];
78  }
79  return $default;
80  }
81 
87  public function getData(): array
88  {
89  return $this->data;
90  }
91 
97  abstract public function run(): ActionResponse;
98 
99 }
option(string $key, $default=null)
Definition: Action.php:74
static failure(string $message='')