Completed
Push — master ( 7ed002...38409f )
by Philip
03:12
created

RepositoryCrudService::save()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Dontdrinkandroot\Service;
4
5
use Doctrine\ORM\Tools\Pagination\Paginator;
6
use Dontdrinkandroot\Repository\CrudRepositoryInterface;
7
8
/**
9
 * @author Philip Washington Sorst <[email protected]>
10
 */
11
class RepositoryCrudService implements CrudServiceInterface
12
{
13
    /**
14
     * @var CrudRepositoryInterface
15
     */
16
    private $repository;
17
18 10
    public function __construct(CrudRepositoryInterface $repository)
19
    {
20 10
        $this->repository = $repository;
21 10
    }
22
23
    /**
24
     * {@inheritdoc}
25
     */
26 2
    public function find($id): ?object
27
    {
28 2
        return $this->getRepository()->find($id);
29
    }
30
31
    /**
32
     * {@inheritdoc}
33
     */
34 4
    public function findAll(): array
35
    {
36 4
        return $this->getRepository()->findAll();
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    public function findAllPaginated(int $page = 1, int $perPage = 50): Paginator
43
    {
44
        return $this->getRepository()->findPaginatedBy($page, $perPage);
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50 2
    public function create(object $entity): object
51
    {
52 2
        return $this->getRepository()->persist($entity, true);
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58 2
    public function update(object $entity): object
59
    {
60 2
        $this->getRepository()->flush($entity);
61
62 2
        return $entity;
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68 4
    public function save(object $entity): object
69
    {
70 4
        return $this->getRepository()->merge($entity, true);
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76
    public function remove(object $entity): void
77
    {
78
        $this->getRepository()->remove($entity, true);
79
    }
80
81 10
    protected function getRepository(): CrudRepositoryInterface
82
    {
83 10
        return $this->repository;
84
    }
85
}
86