| Total Complexity | 9 |
| Total Lines | 76 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 1 | <?php |
||
| 5 | trait ConfigAwareTrait |
||
| 6 | { |
||
| 7 | /** |
||
| 8 | * @var array |
||
| 9 | */ |
||
| 10 | private $config; |
||
| 11 | |||
| 12 | /** |
||
| 13 | * ConfigAwareTrait constructor. |
||
| 14 | * @param array $config |
||
| 15 | */ |
||
| 16 | public function __construct(array $config = []) |
||
| 17 | { |
||
| 18 | $this->beforeConfig(); |
||
| 19 | $this->setConfig($config); |
||
| 20 | $this->afterConfig(); |
||
| 21 | } |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @param string $key |
||
| 25 | * @param mixed|null $default |
||
| 26 | * @return mixed|null |
||
| 27 | */ |
||
| 28 | public function getConfigValue(string $key, $default = null) |
||
| 29 | { |
||
| 30 | return $this->config[$key] ?? $default; |
||
| 31 | } |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @param string $key |
||
| 35 | * @param mixed $value |
||
| 36 | * @return $this |
||
| 37 | */ |
||
| 38 | public function setConfigValue(string $key, $value) |
||
| 42 | } |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Override this method to perform logic BEFORE configuration is applied. |
||
| 46 | * This method is useful for setting default values for properties. |
||
| 47 | * However, remember to call the parent implementation if you do. |
||
| 48 | */ |
||
| 49 | protected function beforeConfig(): void |
||
| 50 | { |
||
| 51 | } |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Override this method to perform logic AFTER configuration is applied. |
||
| 55 | * This method is useful for configuring classes after instantiation, |
||
| 56 | * e.g. adding a query type to a schema. |
||
| 57 | * However, remember to call the parent implementation if you do. |
||
| 58 | */ |
||
| 59 | protected function afterConfig(): void |
||
| 61 | } |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @param array $config |
||
| 65 | * @return $this |
||
| 66 | */ |
||
| 67 | protected function setConfig(array $config) |
||
| 83 |