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