yiisoft /
yii-demo
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace App\Blog\Comment; |
||
| 6 | |||
| 7 | use App\Blog\Entity\Comment; |
||
| 8 | use Cycle\ORM\Command\CommandInterface; |
||
| 9 | use Cycle\ORM\Command\ContextCarrierInterface; |
||
| 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 | final class CommentMapper extends Mapper |
||
| 17 | { |
||
| 18 | /** |
||
| 19 | * @param Comment $entity |
||
| 20 | */ |
||
| 21 | public function queueUpdate($entity, Node $node, State $state): ContextCarrierInterface |
||
| 22 | { |
||
| 23 | /** @var Update $command */ |
||
| 24 | $command = parent::queueUpdate($entity, $node, $state); |
||
| 25 | |||
| 26 | $now = new \DateTimeImmutable(); |
||
| 27 | |||
| 28 | $state->register('updated_at', $now, true); |
||
| 29 | $command->registerAppendix('updated_at', $now); |
||
| 30 | |||
| 31 | $this->touch($entity, $node, $state, $command); |
||
| 32 | |||
| 33 | return $command; |
||
| 34 | } |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @param Comment $entity |
||
| 38 | */ |
||
| 39 | public function queueDelete($entity, Node $node, State $state): CommandInterface |
||
| 40 | { |
||
| 41 | // identify entity as being "deleted" |
||
| 42 | $state->setStatus(Node::SCHEDULED_DELETE); |
||
| 43 | $state->decClaim(); |
||
| 44 | |||
| 45 | $command = new Update( |
||
| 46 | $this->source->getDatabase(), |
||
| 47 | $this->source->getTable(), |
||
| 48 | [ |
||
| 49 | 'deleted_at' => new \DateTimeImmutable(), |
||
| 50 | 'public' => false, |
||
| 51 | ] |
||
| 52 | ); |
||
| 53 | |||
| 54 | // forward primaryKey value from entity state |
||
| 55 | // this sequence is only required if the entity is created and deleted |
||
| 56 | // within one transaction |
||
| 57 | $command->waitScope($this->primaryColumn); |
||
| 58 | $state->forward( |
||
| 59 | $this->primaryKey, |
||
| 60 | $command, |
||
| 61 | $this->primaryColumn, |
||
| 62 | true, |
||
| 63 | ConsumerInterface::SCOPE |
||
| 64 | ); |
||
| 65 | |||
| 66 | return $command; |
||
| 67 | } |
||
| 68 | |||
| 69 | private function touch(Comment $entity, Node $node, State $state, ContextCarrierInterface $command) |
||
|
0 ignored issues
–
show
|
|||
| 70 | { |
||
| 71 | $now = new \DateTimeImmutable(); |
||
| 72 | |||
| 73 | if ($entity->isPublic() && $entity->getPublishedAt() === null) { |
||
| 74 | $state->register('published_at', $now, true); |
||
| 75 | $command->register('published_at', $now, true); |
||
| 76 | } |
||
| 77 | } |
||
| 78 | } |
||
| 79 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.