AbstractMongoRepository   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 0

Importance

Changes 3
Bugs 0 Features 1
Metric Value
wmc 7
c 3
b 0
f 1
lcom 2
cbo 0
dl 0
loc 80
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getAll() 0 4 1
A get() 0 4 1
A add() 0 4 1
A getBy() 0 4 1
A getOneBy() 0 4 1
A createMongoIdFromIdentity() 0 4 1
1
<?php
2
3
namespace HMLB\DDDBundle\Doctrine\ODM\MongoDB;
4
5
use Doctrine\ODM\MongoDB\DocumentManager;
6
use Doctrine\ODM\MongoDB\DocumentRepository;
7
use MongoId;
8
use HMLB\DDD\Entity\AggregateRoot;
9
use HMLB\DDD\Entity\Repository;
10
use HMLB\DDD\Entity\Identity;
11
12
/**
13
 * Aggregate root repository implementation for ORM.
14
 *
15
 * @author Hugues Maignol <[email protected]>
16
 */
17
abstract class AbstractMongoRepository implements Repository
18
{
19
    /**
20
     * @var DocumentManager
21
     */
22
    protected $dm;
23
    /**
24
     * @var DocumentRepository
25
     */
26
    protected $documentRepository;
27
28
    public function __construct(DocumentManager $dm = null)
29
    {
30
        $this->dm = $dm;
31
        $this->documentRepository = $dm->getRepository($this->getClassName());
32
    }
33
34
    /**
35
     * @return AggregateRoot[]
36
     */
37
    public function getAll()
38
    {
39
        return $this->documentRepository->findAll();
40
    }
41
42
    /**
43
     * @param Identity $identity
44
     *
45
     * @return AggregateRoot
46
     */
47
    public function get(Identity $identity)
48
    {
49
        return $this->documentRepository->find($identity);
50
    }
51
52
    /**
53
     * @param AggregateRoot $document
54
     */
55
    public function add(AggregateRoot $document)
56
    {
57
        $this->dm->persist($document);
58
    }
59
60
    /**
61
     * Finds Entities by a set of criteria.
62
     *
63
     * @param array    $criteria Query criteria
64
     * @param array    $sort     Sort array for Cursor::sort()
65
     * @param int|null $limit    Limit for Cursor::limit()
66
     * @param int|null $skip     Skip for Cursor::skip()
67
     *
68
     * @return AggregateRoot[]
69
     */
70
    protected function getBy(array $criteria, array $sort = null, $limit = null, $skip = null)
71
    {
72
        return $this->documentRepository->findBy($criteria, $sort, $limit, $skip);
73
    }
74
75
    /**
76
     * Finds a single AggregateRoot by a set of criteria.
77
     *
78
     * @param array $criteria
79
     *
80
     * @return AggregateRoot
81
     */
82
    protected function getOneBy(array $criteria)
83
    {
84
        return $this->documentRepository->findOneBy($criteria);
85
    }
86
87
    /**
88
     * @param Identity $id
89
     *
90
     * @return MongoId
91
     */
92
    protected function createMongoIdFromIdentity(Identity $id)
93
    {
94
        return new MongoId((string) $id);
95
    }
96
}
97