|
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 $model Definition |
|
37
|
|
|
* @return array|object |
|
38
|
|
|
*/ |
|
39
|
|
|
public function getData(AnalyticModel $model) |
|
40
|
|
|
{ |
|
41
|
|
|
$conn = $this->documentManager->getConnection(); |
|
42
|
|
|
$db = $this->documentManager->getConfiguration()->getDefaultDB(); |
|
43
|
|
|
$collection = $conn->selectCollection($db, $model->getCollection()); |
|
44
|
|
|
|
|
45
|
|
|
// Build aggregation pipeline |
|
46
|
|
|
$pipeline = $model->getPipeline(); |
|
47
|
|
|
|
|
48
|
|
|
$iterator = $collection->aggregate($pipeline); |
|
49
|
|
|
if ('object' === $model->getType()) { |
|
50
|
|
|
return $iterator->getSingleResult(); |
|
51
|
|
|
} |
|
52
|
|
|
return $iterator->toArray(); |
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
|
|
|