readByHandleAndVersion()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
c 1
b 0
f 0
dl 0
loc 14
rs 10
cc 2
nc 2
nop 2
1
<?php
2
3
/*
4
 * This file is part of the SexyField package.
5
 *
6
 * (c) Dion Snoeijen <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare (strict_types = 1);
13
14
namespace Tardigrades\SectionField\Service;
15
16
use Doctrine\ORM\EntityManagerInterface;
17
use Tardigrades\Entity\SectionHistory as SectionHistoryEntity;
18
use Tardigrades\Entity\SectionInterface;
19
use Tardigrades\SectionField\ValueObject\Handle;
20
use Tardigrades\SectionField\ValueObject\Id;
21
use Tardigrades\SectionField\ValueObject\Version;
22
23
class DoctrineSectionHistoryManager implements SectionHistoryManagerInterface
24
{
25
    /** @var EntityManagerInterface */
26
    private $entityManager;
27
28
    public function __construct(
29
        EntityManagerInterface $entityManager
30
    ) {
31
        $this->entityManager = $entityManager;
32
    }
33
34
    public function create(SectionInterface $entity): SectionInterface
35
    {
36
        $this->entityManager->persist($entity);
37
        $this->entityManager->flush();
38
39
        return $entity;
40
    }
41
42
    public function read(Id $id): SectionInterface
43
    {
44
        $sectionHistoryRepository = $this->entityManager->getRepository(SectionHistoryEntity::class);
45
46
        /** @var SectionInterface $sectionHistory */
47
        $sectionHistory = $sectionHistoryRepository->find($id->toInt());
48
49
        if (empty($sectionHistory)) {
50
            throw new SectionHistoryNotFoundException();
51
        }
52
53
        return $sectionHistory;
54
    }
55
56
    public function readAll(): array
57
    {
58
        $sectionHistoryRepository = $this->entityManager->getRepository(SectionHistoryEntity::class);
59
60
        $sections = $sectionHistoryRepository->findAll();
61
62
        if (empty($sections)) {
63
            throw new SectionHistoryNotFoundException();
64
        }
65
66
        return $sections;
67
    }
68
69
    public function update(SectionInterface $entity): void
70
    {
71
        $this->entityManager->persist($entity);
72
        $this->entityManager->flush();
73
    }
74
75
    public function delete(SectionInterface $entity): void
76
    {
77
        $this->entityManager->remove($entity);
78
        $this->entityManager->flush();
79
    }
80
81
    public function readByHandleAndVersion(Handle $handle, Version $version): SectionInterface
82
    {
83
        $sectionHistoryRepository = $this->entityManager->getRepository(SectionHistoryEntity::class);
84
85
        $section = $sectionHistoryRepository->findBy([
86
            'handle' => (string) $handle,
87
            'version' => $version->toInt()
88
        ]);
89
90
        if (empty($section)) {
91
            throw new SectionNotFoundException();
92
        }
93
94
        return $section[0];
95
    }
96
}
97