|
| 1 | +<?php |
| 2 | +namespace Breadlesscode\Commentable\Service; |
| 3 | + |
| 4 | +use Breadlesscode\Commentable\Dto\CommentDto; |
| 5 | +use Breadlesscode\Commentable\Exception\InvalidNodeTypeException; |
| 6 | +use Neos\ContentRepository\Domain\Model\NodeInterface; |
| 7 | +use Neos\ContentRepository\Domain\Model\NodeTemplate; |
| 8 | +use Neos\ContentRepository\Domain\Service\ContextFactoryInterface; |
| 9 | +use Neos\ContentRepository\Domain\Service\NodeTypeManager; |
| 10 | +use Neos\Flow\Annotations as Flow; |
| 11 | + |
| 12 | +class CommentService |
| 13 | +{ |
| 14 | + /** |
| 15 | + * node type of the commentable mixin |
| 16 | + */ |
| 17 | + const COMMENTABLE_NODE_TYPE = 'Breadlesscode.Commentable:Mixin.Commentable'; |
| 18 | + |
| 19 | + /** |
| 20 | + * node type of the commentable mixin |
| 21 | + */ |
| 22 | + const COMMENT_NODE_TYPE = 'Breadlesscode.Commentable:Content.Comment'; |
| 23 | + |
| 24 | + /** |
| 25 | + * @Flow\Inject() |
| 26 | + * @var ContextFactoryInterface |
| 27 | + */ |
| 28 | + protected $contextFactory; |
| 29 | + |
| 30 | + /** |
| 31 | + * @Flow\Inject() |
| 32 | + * @var NodeTypeManager |
| 33 | + */ |
| 34 | + protected $nodeTypeManager; |
| 35 | + |
| 36 | + /** |
| 37 | + * creates a comment node from the CommentDto |
| 38 | + * |
| 39 | + * @param NodeInterface $commentCollection |
| 40 | + * @param CommentDto $comment |
| 41 | + * @return NodeInterface |
| 42 | + * @throws \Neos\ContentRepository\Exception\NodeTypeNotFoundException |
| 43 | + */ |
| 44 | + protected function createNode(NodeInterface $commentCollection, CommentDto $comment): NodeInterface |
| 45 | + { |
| 46 | + $commentNodeType = $this->nodeTypeManager->getNodeType(self::COMMENT_NODE_TYPE); |
| 47 | + $commentNodeTemplate = new NodeTemplate(); |
| 48 | + $commentNodeTemplate->setNodeType($commentNodeType); |
| 49 | + |
| 50 | + $commentNode = $commentCollection->createNodeFromTemplate($commentNodeTemplate); |
| 51 | + $commentNode->setProperty('name', $comment->getName()); |
| 52 | + $commentNode->setProperty('email', $comment->getEmail()); |
| 53 | + $commentNode->setProperty('content', $comment->getContent()); |
| 54 | + $commentNode->setProperty('createdAt', $comment->getCreatedAt()); |
| 55 | + $commentNode->setHidden($comment->isHidden()); |
| 56 | + |
| 57 | + return $commentNode; |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * adds a comment node to a blog post node |
| 62 | + * |
| 63 | + * @param NodeInterface $commentableNode |
| 64 | + * @param CommentDto $comment |
| 65 | + * @throws InvalidNodeTypeException |
| 66 | + * @throws \Neos\ContentRepository\Exception\NodeTypeNotFoundException |
| 67 | + */ |
| 68 | + public function addComment(NodeInterface $commentableNode, CommentDto $comment) |
| 69 | + { |
| 70 | + if (!$this->isCommentableNode($commentableNode)) { |
| 71 | + throw new InvalidNodeTypeException('The commentable node type should implement the mixin '.self::COMMENTABLE_NODE_TYPE, 1536244558); |
| 72 | + } |
| 73 | + |
| 74 | + $this->createNode($commentableNode->getNode('comments'), $comment); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * @param string $commentableIdentifier |
| 79 | + * @return NodeInterface|null |
| 80 | + * @throws \Exception |
| 81 | + */ |
| 82 | + public function findCommentable(string $commentableIdentifier): ?NodeInterface |
| 83 | + { |
| 84 | + $commentableNode = $this->getContext()->getNodeByIdentifier($commentableIdentifier); |
| 85 | + |
| 86 | + if (!$this->isCommentableNode($commentableNode)) { |
| 87 | + throw new InvalidNodeTypeException('The commentable node type should implement the mixin '.self::COMMENTABLE_NODE_TYPE, 1536244559); |
| 88 | + } |
| 89 | + |
| 90 | + return $commentableNode; |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * @param NodeInterface $node |
| 95 | + * @return bool |
| 96 | + */ |
| 97 | + public function isCommentableNode(NodeInterface $node): bool |
| 98 | + { |
| 99 | + return $node->getNodeType()->isOfType(self::COMMENTABLE_NODE_TYPE); |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * @return \Neos\ContentRepository\Domain\Service\Context |
| 104 | + * @throws \Exception |
| 105 | + */ |
| 106 | + protected function getContext() |
| 107 | + { |
| 108 | + return $this->contextFactory->create([ |
| 109 | + 'workspaceName' => 'live', |
| 110 | + 'currentDateTime' => new \Neos\Flow\Utility\Now(), |
| 111 | + 'dimensions' => [], |
| 112 | + 'invisibleContentShown' => true, |
| 113 | + 'removedContentShown' => false, |
| 114 | + 'inaccessibleContentShown' => false |
| 115 | + ]); |
| 116 | + } |
| 117 | +} |
0 commit comments