diff --git a/README.md b/README.md index 5dd887c..ab823d4 100644 --- a/README.md +++ b/README.md @@ -95,11 +95,12 @@ The event section is work in progress as there is currently no real way to regis become available later so that plugins are also enabled to hook into the events instead of delivering metrics, reporting or storages to the process. -| Event | Description | -|------------------------------------------------------------|:---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| -| Panaly\Configuration\ConfigurationFile\ConfigurationLoaded | The event is dispatched directly after the `ConfigurationFile` was loaded. It allows to overwrite the full configuration by delivering a new instance that will then be taken for the process. | -| Panaly\Configuration\ConfigurationFile\RuntimeLoaded | After the configuration was fully loaded and converted to the `RuntimeConfiguration` this event is triggered, it is the last possibility to change the metric running process. | -| Panaly\Configuration\ConfigurationFile\MetricResultCreated | When the collection, or execution, of configured metric groups is finished the event is triggered with all information and the result can be modified before the storage and reporting runs. The full environment is given her with the `ConfigurationFile`, the `RuntimeConfiguration` and the `Result` | +| Event | Description | +|--------------------------------------------------------------|:---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| +| Panaly\Configuration\ConfigurationFile\BeforeMetricCalculate | Before the Collector executes a metric collection method it will dispatch this even which gives the possibility to change the metric options directly before the metric is executed. | +| Panaly\Configuration\ConfigurationFile\ConfigurationLoaded | The event is dispatched directly after the `ConfigurationFile` was loaded. It allows to overwrite the full configuration by delivering a new instance that will then be taken for the process. | +| Panaly\Configuration\ConfigurationFile\RuntimeLoaded | After the configuration was fully loaded and converted to the `RuntimeConfiguration` this event is triggered, it is the last possibility to change the metric running process. | +| Panaly\Configuration\ConfigurationFile\MetricResultCreated | When the collection, or execution, of configured metric groups is finished the event is triggered with all information and the result can be modified before the storage and reporting runs. The full environment is given her with the `ConfigurationFile`, the `RuntimeConfiguration` and the `Result` | ## Thanks and License diff --git a/src/Collector/Collector.php b/src/Collector/Collector.php index 5f6fac5..99d90f9 100644 --- a/src/Collector/Collector.php +++ b/src/Collector/Collector.php @@ -6,6 +6,7 @@ use Panaly\Configuration\ConfigurationFile; use Panaly\Configuration\RuntimeConfiguration; +use Panaly\Event\BeforeMetricCalculate; use Panaly\Result\Group; use Panaly\Result\Metric; use Panaly\Result\Result; @@ -31,7 +32,11 @@ public function collect(): Result foreach ($executingGroup->metrics as $executingMetric) { $metricHandler = $this->runtimeConfiguration->getMetric($executingMetric->metric); - $metricResult = $metricHandler->calculate($executingMetric->options); + + $event = new BeforeMetricCalculate($metricHandler, $executingMetric->options); + $this->runtimeConfiguration->getEventDispatcher()->dispatch($event); + + $metricResult = $metricHandler->calculate($event->getOptions()); $group->addMetric(new Metric( $executingMetric->title ?? $metricHandler->getDefaultTitle(), diff --git a/src/Event/BeforeMetricCalculate.php b/src/Event/BeforeMetricCalculate.php new file mode 100644 index 0000000..4710311 --- /dev/null +++ b/src/Event/BeforeMetricCalculate.php @@ -0,0 +1,32 @@ + */ + public function getOptions(): array + { + return $this->options; + } + + public function getOption(string $option): mixed + { + return $this->options[$option] ?? null; + } + + public function setOption(string $option, mixed $value): void + { + $this->options[$option] = $value; + } +}