1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Blog\Post; |
4
|
|
|
|
5
|
|
|
use App\Blog\Entity\Post; |
6
|
|
|
use Cycle\ORM\Command\ContextCarrierInterface; |
7
|
|
|
use Cycle\ORM\Command\CommandInterface; |
8
|
|
|
use Cycle\ORM\Command\Database\Update; |
9
|
|
|
use Cycle\ORM\Context\ConsumerInterface; |
10
|
|
|
use Cycle\ORM\Heap\Node; |
11
|
|
|
use Cycle\ORM\Heap\State; |
12
|
|
|
use Cycle\ORM\Mapper\Mapper; |
13
|
|
|
|
14
|
|
|
final class PostMapper extends Mapper |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @param Post $entity |
18
|
|
|
* @param Node $node |
19
|
|
|
* @param State $state |
20
|
|
|
* @return ContextCarrierInterface |
21
|
|
|
* @throws \Exception |
22
|
|
|
*/ |
23
|
|
|
public function queueCreate($entity, Node $node, State $state): ContextCarrierInterface |
24
|
|
|
{ |
25
|
|
|
$command = parent::queueCreate($entity, $node, $state); |
26
|
|
|
$now = new \DateTimeImmutable(); |
27
|
|
|
|
28
|
|
|
$state->register('created_at', $now, true); |
29
|
|
|
$command->register('created_at', $now, true); |
30
|
|
|
|
31
|
|
|
$state->register('updated_at', $now, true); |
32
|
|
|
$command->register('updated_at', $now, true); |
33
|
|
|
|
34
|
|
|
$this->touch($entity, $node, $state, $command); |
35
|
|
|
|
36
|
|
|
return $command; |
37
|
|
|
} |
38
|
|
|
/** |
39
|
|
|
* @param Post $entity |
40
|
|
|
* @param Node $node |
41
|
|
|
* @param State $state |
42
|
|
|
* @return ContextCarrierInterface |
43
|
|
|
* @throws \Exception |
44
|
|
|
*/ |
45
|
|
|
public function queueUpdate($entity, Node $node, State $state): ContextCarrierInterface |
46
|
|
|
{ |
47
|
|
|
/** @var Update $command */ |
48
|
|
|
$command = parent::queueUpdate($entity, $node, $state); |
49
|
|
|
|
50
|
|
|
$now = new \DateTimeImmutable(); |
51
|
|
|
|
52
|
|
|
$state->register('updated_at', $now, true); |
53
|
|
|
$command->registerAppendix('updated_at', $now); |
54
|
|
|
|
55
|
|
|
$this->touch($entity, $node, $state, $command); |
56
|
|
|
|
57
|
|
|
return $command; |
58
|
|
|
} |
59
|
|
|
/** |
60
|
|
|
* @param Post $entity |
61
|
|
|
* @param Node $node |
62
|
|
|
* @param State $state |
63
|
|
|
* @return CommandInterface |
64
|
|
|
* @throws \Exception |
65
|
|
|
*/ |
66
|
|
|
public function queueDelete($entity, Node $node, State $state): CommandInterface |
67
|
|
|
{ |
68
|
|
|
// identify entity as being "deleted" |
69
|
|
|
$state->setStatus(Node::SCHEDULED_DELETE); |
70
|
|
|
$state->decClaim(); |
71
|
|
|
|
72
|
|
|
$command = new Update( |
73
|
|
|
$this->source->getDatabase(), |
74
|
|
|
$this->source->getTable(), |
75
|
|
|
[ |
76
|
|
|
'deleted_at' => new \DateTimeImmutable(), |
77
|
|
|
'public' => false, |
78
|
|
|
] |
79
|
|
|
); |
80
|
|
|
|
81
|
|
|
// forward primaryKey value from entity state |
82
|
|
|
// this sequence is only required if the entity is created and deleted |
83
|
|
|
// within one transaction |
84
|
|
|
$command->waitScope($this->primaryColumn); |
85
|
|
|
$state->forward( |
86
|
|
|
$this->primaryKey, |
87
|
|
|
$command, |
88
|
|
|
$this->primaryColumn, |
89
|
|
|
true, |
90
|
|
|
ConsumerInterface::SCOPE |
91
|
|
|
); |
92
|
|
|
|
93
|
|
|
return $command; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
private function touch(Post $entity, Node $node, State $state, ContextCarrierInterface $command) |
|
|
|
|
97
|
|
|
{ |
98
|
|
|
$now = new \DateTimeImmutable(); |
99
|
|
|
|
100
|
|
|
if ($entity->isPublic() && $entity->getPublishedAt() === null) { |
101
|
|
|
$state->register('published_at', $now, true); |
102
|
|
|
$command->register('published_at', $now, true); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.