zend-blog-3-backend

Форк
0
/
CommentManager.php 
115 строк · 3.0 Кб
1
<?php
2

3
namespace App\Service;
4

5
use App\DTO\CommentDTO;
6
use App\Entity\Comment;
7
use App\Event\CommentEvent;
8
use App\Exception\NotAllowedCommentException;
9
use App\Repository\CommentatorRepository;
10
use App\Repository\CommentRepository;
11
use App\Repository\PostRepository;
12
use App\Repository\UserRepository;
13
use Laminas\Filter\StripTags;
14
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
15

16
class CommentManager
17
{
18
    /**
19
     * @var Tracking
20
     */
21
    private $tracking;
22

23
    /**
24
     * @var EventDispatcherInterface
25
     */
26
    private $dispatcher;
27

28
    /**
29
     * @var CommentatorRepository
30
     */
31
    private $commentatorRepo;
32

33
    /**
34
     * @var CommentRepository
35
     */
36
    private $commentRepo;
37

38
    /**
39
     * @var PostRepository
40
     */
41
    private $postRepo;
42

43
    /**
44
     * @var UserRepository
45
     */
46
    private $userRepository;
47

48
    public function __construct(
49
        Tracking $tracking,
50
        EventDispatcherInterface $dispatcher,
51
        CommentatorRepository $commentatorRepo,
52
        CommentRepository $commentRepo,
53
        UserRepository $userRepository,
54
        PostRepository $postRepo
55
    ) {
56
        $this->tracking = $tracking;
57
        $this->dispatcher = $dispatcher;
58
        $this->commentatorRepo = $commentatorRepo;
59
        $this->userRepository = $userRepository;
60
        $this->commentRepo = $commentRepo;
61
        $this->postRepo = $postRepo;
62
    }
63

64
    /**
65
     * @param CommentDTO $commentData
66
     *
67
     * @throws NotAllowedCommentException
68
     *
69
     * @return Comment
70
     */
71
    public function saveExternalComment(CommentDTO $commentData): Comment
72
    {
73
        $agent = $commentData->userAgent ? $this->tracking->getTrackingAgent($commentData->userAgent) : null;
74

75
        $post = $this->postRepo->find($commentData->topicId);
76
        if (!($post && !$post->isDisableComments())) {
77
            throw new NotAllowedCommentException();
78
        }
79

80
        $filter = new StripTags([
81
            'allowTags' => ['a', 's', 'b', 'i', 'em', 'strong', 'img', 'p'],
82
            'allowAttribs' => ['src', 'href'],
83
        ]);
84

85
        $comment = new Comment();
86
        $comment
87
            ->setTrackingAgent($agent)
88
            ->setIpAddress($commentData->ipAddress)
89
            ->setText($filter->filter($commentData->text))
90
            ->setPost($post)
91
        ;
92

93
        if ($commentData->parentId > 0) {
94
            $parent = $this->commentRepo->find($commentData->parentId);
95
            if ($parent) {
96
                $comment->setParent($parent);
97
            }
98
        }
99

100
        if ($commentData->commentator) {
101
            $commentator = $this->commentatorRepo->findOrCreate($commentData->commentator);
102
            $comment->setCommentator($commentator);
103
        } elseif ($commentData->user && $user = $this->userRepository->find($commentData->user->id)) {
104
            $comment->setUser($user);
105
        } else {
106
            throw new \LogicException('Comment without sender');
107
        }
108

109
        $this->commentRepo->save($comment);
110

111
        $this->dispatcher->dispatch(new CommentEvent($comment));
112

113
        return $comment;
114
    }
115
}
116

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.