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

AnalyticsManager::getData()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 0
cts 12
cp 0
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 9
nc 3
nop 1
crap 12
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