Completed
Push — feature/EVO-10415-Analytics-wi... ( e2b3ad )
by
unknown
12:30
created

AnalyticsManager   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 5
dl 0
loc 35
ccs 0
cts 16
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getData() 0 13 3
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 = $schema->getPipeline();
46
        $data = $collection->aggregate($pipeline)->toArray();
47
        if ('object' === $schema->getType()) {
48
            return array_key_exists(0, $data) ? $data[0] : new \stdClass();
49
        }
50
        return $data;
51
    }
52
}
53