Join
2 years ago
ComputedMetricFactory.php
2 years ago
Dimension.php
1 month ago
DimensionMetricFactory.php
1 year ago
DimensionSegmentFactory.php
6 months ago
DimensionsProvider.php
6 months ago
Discriminator.php
2 years ago
Join.php
2 years ago
MetricsList.php
3 months ago
Updater.php
3 months ago
DimensionMetricFactory.php
116 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Matomo - free/libre analytics platform |
| 5 | * |
| 6 | * @link https://matomo.org |
| 7 | * @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later |
| 8 | */ |
| 9 | namespace Piwik\Columns; |
| 10 | |
| 11 | use Piwik\Piwik; |
| 12 | use Piwik\Plugin\ArchivedMetric; |
| 13 | use Piwik\Plugin\ComputedMetric; |
| 14 | /** |
| 15 | * A factory to create metrics from a dimension. |
| 16 | * |
| 17 | * @api since Piwik 3.2.0 |
| 18 | */ |
| 19 | class DimensionMetricFactory |
| 20 | { |
| 21 | /** |
| 22 | * @var Dimension |
| 23 | */ |
| 24 | private $dimension = null; |
| 25 | /** |
| 26 | * Generates a new dimension metric factory. |
| 27 | * @param Dimension $dimension A dimension instance the created metrics should be based on. |
| 28 | */ |
| 29 | public function __construct(\Piwik\Columns\Dimension $dimension) |
| 30 | { |
| 31 | $this->dimension = $dimension; |
| 32 | } |
| 33 | /** |
| 34 | * @return ArchivedMetric |
| 35 | */ |
| 36 | public function createCustomMetric($metricName, $readableName, $aggregation, $documentation = '', ?string $semanticType = null) |
| 37 | { |
| 38 | if (!$this->dimension->getDbTableName() || !$this->dimension->getColumnName()) { |
| 39 | throw new \Exception(sprintf('Cannot make metric from dimension %s because DB table or column missing', $this->dimension->getId())); |
| 40 | } |
| 41 | $metric = new ArchivedMetric($this->dimension, $aggregation); |
| 42 | $metric->setType($semanticType ?: $this->dimension->getType()); |
| 43 | $metric->setName($metricName); |
| 44 | $metric->setTranslatedName($readableName); |
| 45 | $metric->setDocumentation($documentation); |
| 46 | $metric->setCategory($this->dimension->getCategoryId()); |
| 47 | return $metric; |
| 48 | } |
| 49 | /** |
| 50 | * @return \Piwik\Plugin\ComputedMetric |
| 51 | */ |
| 52 | public function createComputedMetric($metricName1, $metricName2, $aggregation) |
| 53 | { |
| 54 | // We cannot use reuse ComputedMetricFactory here as it would result in an endless loop since ComputedMetricFactory |
| 55 | // requires a MetricsList which is just being built here... |
| 56 | $metric = new ComputedMetric($metricName1, $metricName2, $aggregation); |
| 57 | $metric->setCategory($this->dimension->getCategoryId()); |
| 58 | return $metric; |
| 59 | } |
| 60 | /** |
| 61 | * @return ArchivedMetric |
| 62 | */ |
| 63 | public function createMetric($aggregation) |
| 64 | { |
| 65 | $dimension = $this->dimension; |
| 66 | if (!$dimension->getNamePlural()) { |
| 67 | throw new \Exception(sprintf('No metric can be created for this dimension %s automatically because no $namePlural is set.', $dimension->getId())); |
| 68 | } |
| 69 | $prefix = ''; |
| 70 | $translatedName = $dimension->getNamePlural(); |
| 71 | $documentation = ''; |
| 72 | $semanticType = null; |
| 73 | // if null, will default to dimension type |
| 74 | switch ($aggregation) { |
| 75 | case ArchivedMetric::AGGREGATION_COUNT: |
| 76 | $prefix = ArchivedMetric::AGGREGATION_COUNT_PREFIX; |
| 77 | $translatedName = $dimension->getNamePlural(); |
| 78 | $documentation = Piwik::translate('General_ComputedMetricCountDocumentation', $dimension->getNamePlural()); |
| 79 | $semanticType = \Piwik\Columns\Dimension::TYPE_NUMBER; |
| 80 | break; |
| 81 | case ArchivedMetric::AGGREGATION_SUM: |
| 82 | $prefix = ArchivedMetric::AGGREGATION_SUM_PREFIX; |
| 83 | $translatedName = Piwik::translate('General_ComputedMetricSum', $dimension->getNamePlural()); |
| 84 | $documentation = Piwik::translate('General_ComputedMetricSumDocumentation', $dimension->getNamePlural()); |
| 85 | if ($dimension->getType() == \Piwik\Columns\Dimension::TYPE_BOOL) { |
| 86 | $semanticType = \Piwik\Columns\Dimension::TYPE_NUMBER; |
| 87 | } |
| 88 | break; |
| 89 | case ArchivedMetric::AGGREGATION_MAX: |
| 90 | $prefix = ArchivedMetric::AGGREGATION_MAX_PREFIX; |
| 91 | $translatedName = Piwik::translate('General_ComputedMetricMax', $dimension->getNamePlural()); |
| 92 | $documentation = Piwik::translate('General_ComputedMetricMaxDocumentation', $dimension->getNamePlural()); |
| 93 | break; |
| 94 | case ArchivedMetric::AGGREGATION_MIN: |
| 95 | $prefix = ArchivedMetric::AGGREGATION_MIN_PREFIX; |
| 96 | $translatedName = Piwik::translate('General_ComputedMetricMin', $dimension->getNamePlural()); |
| 97 | $documentation = Piwik::translate('General_ComputedMetricMinDocumentation', $dimension->getNamePlural()); |
| 98 | break; |
| 99 | case ArchivedMetric::AGGREGATION_UNIQUE: |
| 100 | $prefix = ArchivedMetric::AGGREGATION_UNIQUE_PREFIX; |
| 101 | $translatedName = Piwik::translate('General_ComputedMetricUniqueCount', $dimension->getNamePlural()); |
| 102 | $documentation = Piwik::translate('General_ComputedMetricUniqueCountDocumentation', $dimension->getNamePlural()); |
| 103 | $semanticType = \Piwik\Columns\Dimension::TYPE_NUMBER; |
| 104 | break; |
| 105 | case ArchivedMetric::AGGREGATION_COUNT_WITH_NUMERIC_VALUE: |
| 106 | $prefix = ArchivedMetric::AGGREGATION_COUNT_WITH_NUMERIC_VALUE_PREFIX; |
| 107 | $translatedName = Piwik::translate('General_ComputedMetricCountWithValue', $dimension->getName()); |
| 108 | $documentation = Piwik::translate('General_ComputedMetricCountWithValueDocumentation', $dimension->getName()); |
| 109 | $semanticType = \Piwik\Columns\Dimension::TYPE_NUMBER; |
| 110 | break; |
| 111 | } |
| 112 | $metricId = strtolower($dimension->getMetricId()); |
| 113 | return $this->createCustomMetric($prefix . $metricId, $translatedName, $aggregation, $documentation, $semanticType); |
| 114 | } |
| 115 | } |
| 116 |