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