Apply PSR-12 code style (#3886)
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / lib / data / comment / LikeableCommentProvider.class.php
CommitLineData
285b1d92 1<?php
a9229942 2
285b1d92 3namespace wcf\data\comment;
a9229942 4
285b1d92 5use wcf\data\like\ILikeObjectTypeProvider;
a9229942 6use wcf\data\like\object\ILikeObject;
285b1d92
MW
7use wcf\data\object\type\AbstractObjectTypeProvider;
8use wcf\system\comment\CommentHandler;
8700ed2c 9use wcf\system\like\IViewableLikeProvider;
285b1d92
MW
10
11/**
12 * Object type provider for comments
7cb93482 13 *
a9229942
TD
14 * @author Alexander Ebert
15 * @copyright 2001-2019 WoltLab GmbH
16 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
17 * @package WoltLabSuite\Core\Data\Comment
18 *
19 * @method LikeableComment getObjectByID($objectID)
20 * @method LikeableComment[] getObjectsByIDs(array $objectIDs)
285b1d92 21 */
a9229942
TD
22class LikeableCommentProvider extends AbstractObjectTypeProvider implements
23 ILikeObjectTypeProvider,
24 IViewableLikeProvider
25{
26 /**
27 * @inheritDoc
28 */
29 public $className = Comment::class;
30
31 /**
32 * @inheritDoc
33 */
34 public $decoratorClassName = LikeableComment::class;
35
36 /**
37 * @inheritDoc
38 */
39 public $listClassName = CommentList::class;
40
41 /**
42 * @inheritDoc
43 */
44 public function checkPermissions(ILikeObject $comment)
45 {
46 /** @var Comment $comment */
47
48 if (!$comment->commentID) {
49 return false;
50 }
51
52 $objectType = CommentHandler::getInstance()->getObjectType($comment->objectTypeID);
53
54 return CommentHandler::getInstance()->getCommentManager($objectType->objectType)->isAccessible($comment->objectID);
55 }
56
57 /**
58 * @inheritDoc
59 */
60 public function prepare(array $likes)
61 {
62 $commentIDs = [];
63 foreach ($likes as $like) {
64 $commentIDs[] = $like->objectID;
65 }
66
67 // fetch comments
68 $commentList = new CommentList();
69 $commentList->setObjectIDs($commentIDs);
70 $commentList->readObjects();
71 $comments = $commentList->getObjects();
72
73 // group likes by object type id
74 $likeData = [];
75 foreach ($likes as $like) {
76 if (isset($comments[$like->objectID])) {
77 if (!isset($likeData[$comments[$like->objectID]->objectTypeID])) {
78 $likeData[$comments[$like->objectID]->objectTypeID] = [];
79 }
80 $likeData[$comments[$like->objectID]->objectTypeID][] = $like;
81 }
82 }
83
84 foreach ($likeData as $objectTypeID => $likes) {
85 $objectType = CommentHandler::getInstance()->getObjectType($objectTypeID);
86 if (CommentHandler::getInstance()->getCommentManager($objectType->objectType) instanceof IViewableLikeProvider) {
87 /** @noinspection PhpUndefinedMethodInspection */
88 CommentHandler::getInstance()->getCommentManager($objectType->objectType)->prepare($likes);
89 }
90 }
91 }
285b1d92 92}