Passed
Push — master ( 7070d1...90aa69 )
by Sebastian
03:34
created

UuidMapperAbstract::save()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 3
c 1
b 0
f 1
dl 0
loc 7
ccs 0
cts 6
cp 0
rs 10
cc 2
nc 2
nop 1
crap 6
1
<?php
2
3
/**
4
 * Linna Framework.
5
 *
6
 * @author Sebastian Rapetti <[email protected]>
7
 * @copyright (c) 2018, Sebastian Rapetti
8
 * @license http://opensource.org/licenses/MIT MIT License
9
 */
10
declare(strict_types=1);
11
12
namespace Linna\DataMapper;
13
14
use InvalidArgumentException;
15
16
/**
17
 * Abstract Class for ObjectMapper, uuid version.
18
 */
19
abstract class UuidMapperAbstract
20
{
21
    /**
22
     * Create a new instance of the DomainObject that this
23
     * mapper is responsible for.
24
     *
25
     * @return UuidDomainObjectInterface
26
     */
27
    public function create(): UuidDomainObjectInterface
28
    {
29
        return $this->concreteCreate();
30
    }
31
32
    /**
33
     * Store the DomainObject in persistent storage. Either insert
34
     * or update the store as required.
35
     *
36
     * @param UuidDomainObjectInterface $domainObject
37
     *
38
     * @return void
39
     */
40
    public function save(UuidDomainObjectInterface &$domainObject): void
41
    {
42
        if ($domainObject->getId() === '') {
43
            $this->concreteInsert($domainObject);
44
        }
45
46
        $this->concreteUpdate($domainObject);
47
    }
48
49
    /**
50
     * Delete the DomainObject from persistent storage.
51
     *
52
     * @param UuidDomainObjectInterface $domainObject
53
     *
54
     * @return void
55
     */
56
    public function delete(UuidDomainObjectInterface &$domainObject): void
57
    {
58
        $this->concreteDelete($domainObject);
59
    }
60
61
    /**
62
     * Create a new instance of a DomainObject.
63
     *
64
     * @return UuidDomainObjectInterface
65
     */
66
    abstract protected function concreteCreate(): UuidDomainObjectInterface;
67
68
    /**
69
     * Insert the DomainObject to persistent storage.
70
     *
71
     * @param UuidDomainObjectInterface $domainObject
72
     */
73
    abstract protected function concreteInsert(UuidDomainObjectInterface &$domainObject);
74
75
    /**
76
     * Update the DomainObject in persistent storage.
77
     *
78
     * @param UuidDomainObjectInterface $domainObject
79
     */
80
    abstract protected function concreteUpdate(UuidDomainObjectInterface $domainObject);
81
82
    /**
83
     * Delete the DomainObject from peristent Storage.
84
     *
85
     * @param UuidDomainObjectInterface $domainObject
86
     */
87
    abstract protected function concreteDelete(UuidDomainObjectInterface &$domainObject);
88
89
    /**
90
     * Check for valid domain Object.
91
     *
92
     * @param UuidDomainObjectInterface $domainObject
93
     *
94
     * @throws InvalidArgumentException if the domain object isn't of the type required by mapper
95
     */
96
    abstract protected function checkDomainObjectType(UuidDomainObjectInterface $domainObject);
97
}
98