1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Database manager and query manager |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace Graviton\AnalyticsBundle\Manager; |
7
|
|
|
|
8
|
|
|
use Doctrine\ODM\MongoDB\DocumentManager; |
9
|
|
|
use Graviton\AnalyticsBundle\Model\AnalyticModel; |
10
|
|
|
use Graviton\DocumentBundle\Service\DateConverter; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Manager for data layer single responsibility |
14
|
|
|
* |
15
|
|
|
* @author List of contributors <https://github.com/libgraviton/graviton/graphs/contributors> |
16
|
|
|
* @license https://opensource.org/licenses/MIT MIT License |
17
|
|
|
* @link http://swisscom.ch |
18
|
|
|
*/ |
19
|
|
|
class AnalyticsManager |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var DocumentManager |
23
|
|
|
*/ |
24
|
|
|
private $documentManager; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
private $databaseName; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var DateConverter |
33
|
|
|
*/ |
34
|
|
|
private $dateConverter; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* AnalyticsManager constructor. |
38
|
|
|
* @param DocumentManager $documentManager Db manager and query control |
39
|
|
|
* @param string $databaseName Db string name |
40
|
|
|
* @param DateConverter $dateConverter date converter |
41
|
|
|
*/ |
42
|
|
|
public function __construct( |
43
|
|
|
DocumentManager $documentManager, |
44
|
|
|
$databaseName, |
45
|
|
|
DateConverter $dateConverter |
46
|
|
|
) { |
47
|
|
|
$this->documentManager = $documentManager; |
48
|
|
|
$this->databaseName = $databaseName; |
49
|
|
|
$this->dateConverter = $dateConverter; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Query db based on definition |
54
|
|
|
* Another option is to use: $collection->createAggregationBuilder(); |
55
|
|
|
* |
56
|
|
|
* @param AnalyticModel $model Definition |
57
|
|
|
* @param array $params data params |
58
|
|
|
* |
59
|
|
|
* @return array|object |
60
|
|
|
*/ |
61
|
|
|
public function getData(AnalyticModel $model, $params = []) |
62
|
|
|
{ |
63
|
|
|
$conn = $this->documentManager->getConnection(); |
64
|
|
|
$collection = $conn->selectCollection($this->databaseName, $model->getCollection()); |
65
|
|
|
|
66
|
|
|
// Build aggregation pipeline |
67
|
|
|
$pipeline = $model->getAggregate($params); |
68
|
|
|
|
69
|
|
|
$iterator = $collection->aggregate($pipeline, ['cursor' => true]); |
70
|
|
|
|
71
|
|
|
if ('object' === $model->getType()) { |
72
|
|
|
return $this->convertDates($iterator->getSingleResult()); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return array_map([$this, 'convertDates'], $iterator->toArray()); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* convert date representations in the returned output.. as we are not typed here, we don't |
80
|
|
|
* know what to expect.. |
81
|
|
|
* |
82
|
|
|
* @param array $data data |
83
|
|
|
* |
84
|
|
|
* @return array data with formatted dates |
85
|
|
|
*/ |
86
|
|
|
private function convertDates(array $data) |
87
|
|
|
{ |
88
|
|
|
foreach ($data as $key => $val) { |
89
|
|
|
if (is_array($val)) { |
90
|
|
|
$data[$key] = $this->convertDates($val); |
91
|
|
|
} |
92
|
|
|
if ($val instanceof \MongoDate) { |
93
|
|
|
$data[$key] = $this->dateConverter->formatDateTime($val->toDateTime()); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
return $data; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|