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