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
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Manager for data layer single responsibility |
13
|
|
|
* |
14
|
|
|
* @author List of contributors <https://github.com/libgraviton/graviton/graphs/contributors> |
15
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GNU Public License |
16
|
|
|
* @link http://swisscom.ch |
17
|
|
|
*/ |
18
|
|
|
class AnalyticsManager |
19
|
|
|
{ |
20
|
|
|
/** @var DocumentManager */ |
21
|
|
|
protected $documentManager; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* AnalyticsManager constructor. |
25
|
|
|
* @param DocumentManager $documentManager Db manager and query control |
26
|
|
|
*/ |
27
|
|
|
public function __construct(DocumentManager $documentManager) |
28
|
|
|
{ |
29
|
|
|
$this->documentManager = $documentManager; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Query db based on definition |
34
|
|
|
* Another option is to use: $collection->createAggregationBuilder(); |
35
|
|
|
* |
36
|
|
|
* @param AnalyticModel $schema Definition |
37
|
|
|
* @return array|object |
38
|
|
|
*/ |
39
|
|
|
public function getData(AnalyticModel $schema) |
40
|
|
|
{ |
41
|
|
|
$conn = $this->documentManager->getConnection(); |
42
|
|
|
$db = $this->documentManager->getConfiguration()->getDefaultDB(); |
43
|
|
|
$collection = $conn->selectCollection($db, $schema->getCollection()); |
44
|
|
|
|
45
|
|
|
$pipeline = []; |
46
|
|
|
// Json Definition object key -> value to array object. |
47
|
|
|
foreach ($schema->getAggregate() as $op => $query) { |
48
|
|
|
$pipeline[] = [ |
49
|
|
|
$op => $this->parseObjectDates($query) |
50
|
|
|
]; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
$iterator = $collection->aggregate($pipeline); |
54
|
|
|
if ('object' === $schema->getType()) { |
55
|
|
|
return $iterator->getSingleResult(); |
56
|
|
|
} |
57
|
|
|
return $iterator->toArray(); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Enabling to possibility to create dtae queries |
62
|
|
|
* Will replace PARSE_DATE(date|format) |
63
|
|
|
* sample: PARSE_DATE(-4 years|Y) -> new DateTime(-4 years)->format(Y) -> 2013 |
64
|
|
|
* |
65
|
|
|
* @param object $query Aggregation query |
66
|
|
|
* @return object |
67
|
|
|
*/ |
68
|
|
|
private function parseObjectDates($query) |
69
|
|
|
{ |
70
|
|
|
$string = json_encode($query); |
71
|
|
|
preg_match_all('/PARSE_DATE\(([^\)]+)\)/', $string, $matches); |
72
|
|
|
if ($matches && array_key_exists(1, $matches) && is_array($matches[1])) { |
73
|
|
|
foreach ($matches[0] as $key => $value) { |
74
|
|
|
$formatting = explode('|', $matches[1][$key]); |
75
|
|
|
$date = new \DateTime($formatting[0]); |
76
|
|
|
$string = str_replace('"'.$value.'"', $date->format($formatting[1]), $string); |
77
|
|
|
} |
78
|
|
|
$query = json_decode($string); |
79
|
|
|
} |
80
|
|
|
return $query; |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|