Skip to content

Commit 75be697

Browse files
committed
Adding Doctrine repository implementations
1 parent fe9939b commit 75be697

File tree

4 files changed

+183
-0
lines changed

4 files changed

+183
-0
lines changed
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
<?php
2+
3+
namespace CleanPhp\Invoicer\Persistence\Doctrine\Repository;
4+
5+
use CleanPhp\Invoicer\Domain\Entity\AbstractEntity;
6+
use CleanPhp\Invoicer\Domain\Repository\RepositoryInterface;
7+
use Doctrine\ORM\EntityManager;
8+
9+
/**
10+
* Class AbstractDoctrineRepository
11+
* @package CleanPhp\Invoicer\Persistence\Doctrine\Repository
12+
*/
13+
abstract class AbstractDoctrineRepository implements RepositoryInterface
14+
{
15+
/**
16+
* @var EntityManager
17+
*/
18+
protected $entityManager;
19+
20+
/**
21+
* @var string
22+
*/
23+
protected $entityClass;
24+
25+
/**
26+
* @param EntityManager $em
27+
*/
28+
public function __construct(EntityManager $em)
29+
{
30+
if (empty($this->entityClass)) {
31+
throw new \RuntimeException(
32+
get_class($this) . '::$entityClass is not defined'
33+
);
34+
}
35+
36+
$this->entityManager = $em;
37+
}
38+
39+
/**
40+
* @param int $id
41+
* @return object
42+
*/
43+
public function getById($id)
44+
{
45+
return $this->entityManager->find($this->entityClass, $id);
46+
}
47+
48+
/**
49+
* @return array
50+
*/
51+
public function getAll()
52+
{
53+
return $this->entityManager->getRepository($this->entityClass)
54+
->findAll();
55+
}
56+
57+
/**
58+
* @param array $conditions
59+
* @param array $order
60+
* @param int $limit
61+
* @param int $offset
62+
* @return array
63+
*/
64+
public function getBy(
65+
$conditions = [],
66+
$order = [],
67+
$limit = null,
68+
$offset = null
69+
) {
70+
$repository = $this->entityManager->getRepository(
71+
$this->entityClass
72+
);
73+
74+
$results = $repository->findBy(
75+
$conditions,
76+
$order,
77+
$limit,
78+
$offset
79+
);
80+
81+
return $results;
82+
}
83+
84+
/**
85+
* @param AbstractEntity $entity
86+
* @return $this
87+
*/
88+
public function persist(AbstractEntity $entity)
89+
{
90+
$this->entityManager->persist($entity);
91+
return $this;
92+
}
93+
94+
/**
95+
* @return $this
96+
*/
97+
public function begin()
98+
{
99+
$this->entityManager->beginTransaction();
100+
return $this;
101+
}
102+
103+
/**
104+
* @return $this
105+
*/
106+
public function commit()
107+
{
108+
$this->entityManager->flush();
109+
$this->entityManager->commit();
110+
return $this;
111+
}
112+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace CleanPhp\Invoicer\Persistence\Doctrine\Repository;
4+
5+
use CleanPhp\Invoicer\Domain\Repository\CustomerRepositoryInterface;
6+
7+
/**
8+
* Class CustomerRepository
9+
* @package CleanPhp\Invoicer\Persistence\Doctrine\Repository
10+
*/
11+
class CustomerRepository extends AbstractDoctrineRepository implements CustomerRepositoryInterface
12+
{
13+
/**
14+
* @var string
15+
*/
16+
protected $entityClass = 'CleanPhp\Invoicer\Domain\Entity\Customer';
17+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace CleanPhp\Invoicer\Persistence\Doctrine\Repository;
4+
5+
use CleanPhp\Invoicer\Domain\Repository\InvoiceRepositoryInterface;
6+
7+
/**
8+
* Class InvoiceRepository
9+
* @package CleanPhp\Invoicer\Persistence\Doctrine\Repository
10+
*/
11+
class InvoiceRepository extends AbstractDoctrineRepository implements InvoiceRepositoryInterface
12+
{
13+
/**
14+
* @var string
15+
*/
16+
protected $entityClass = 'CleanPhp\Invoicer\Domain\Entity\Invoice';
17+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace CleanPhp\Invoicer\Persistence\Doctrine\Repository;
4+
5+
use CleanPhp\Invoicer\Domain\Repository\OrderRepositoryInterface;
6+
use Doctrine\ORM\Query\Expr\Join;
7+
8+
/**
9+
* Class OrderRepository
10+
* @package CleanPhp\Invoicer\Persistence\Doctrine\Repository
11+
*/
12+
class OrderRepository extends AbstractDoctrineRepository implements OrderRepositoryInterface
13+
{
14+
/**
15+
* @var string
16+
*/
17+
protected $entityClass = 'CleanPhp\Invoicer\Domain\Entity\Order';
18+
19+
/**
20+
* @return array
21+
*/
22+
public function getUninvoicedOrders()
23+
{
24+
$builder = $this->entityManager->createQueryBuilder()
25+
->select('o')
26+
->from($this->entityClass, 'o')
27+
->leftJoin(
28+
'CleanPhp\Invoicer\Domain\Entity\Invoice',
29+
'i',
30+
Join::WITH,
31+
'i.order = o'
32+
)
33+
->where('i.id IS NULL');
34+
35+
return $builder->getQuery()->getResult();
36+
}
37+
}

0 commit comments

Comments
 (0)