f5dfb524df00f690547a23cd82275c4ee0785106
[GitHub/WoltLab/WCF.git] /
1 <?php
2 namespace wcf\system\user\notification\event;
3 use wcf\data\like\object\ILikeObject;
4 use wcf\data\like\Like;
5 use wcf\data\user\UserProfile;
6 use wcf\system\cache\runtime\UserRuntimeCache;
7 use wcf\system\like\LikeHandler;
8 use wcf\system\user\notification\object\IUserNotificationObject;
9 use wcf\system\user\notification\object\LikeUserNotificationObject;
10 use wcf\system\WCF;
11
12 /**
13 * Default implementation of some methods of the testable user notification event interface
14 * for like user notificiation events.
15 *
16 * As PHP 5.5 does not support abstract static functions in traits, we require them by this documentation:
17 * - protected static function createTestLikeObject(UserProfile $recipient, UserProfile $author)
18 * creates a likable object
19 * - protected static function getTestLikeableObjectTypeName()
20 * returns the name of the likeable object type name
21 *
22 * @author Matthias Schmidt
23 * @copyright 2001-2018 WoltLab GmbH
24 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
25 * @package WoltLabSuite\Core\System\User\Notification\Event
26 * @since 3.1
27 */
28 trait TTestableLikeUserNotificationEvent {
29 /**
30 * @inheritDoc
31 */
32 public static function canBeTriggeredByGuests() {
33 return false;
34 }
35
36 /**
37 * Returns the like object for the given user notification object.
38 *
39 * @param IUserNotificationObject $object
40 * @return ILikeObject
41 */
42 protected static function getTestLikeObject(IUserNotificationObject $object) {
43 /** @var LikeUserNotificationObject $object */
44
45 $oldUser = WCF::getUser();
46
47 WCF::getSession()->changeUser(UserRuntimeCache::getInstance()->getObject($object->userID), true);
48
49 LikeHandler::getInstance()->loadLikeObjects(
50 LikeHandler::getInstance()->getObjectType(self::getTestLikeableObjectTypeName()),
51 [$object->objectID]
52 );
53
54 WCF::getSession()->changeUser($oldUser, true);
55
56 return LikeHandler::getInstance()->getLikeObject(
57 LikeHandler::getInstance()->getObjectType(self::getTestLikeableObjectTypeName()),
58 $object->objectID
59 )->getLikedObject();
60 }
61
62 /**
63 * @inheritDoc
64 * @return LikeUserNotificationObject[]
65 */
66 public static function getTestObjects(UserProfile $recipient, UserProfile $author) {
67 /** @var ILikeObject $likeObject */
68 $likeObject = self::createTestLikeObject($recipient, $author);
69 $likeObject->setObjectType(LikeHandler::getInstance()->getObjectType(self::getTestLikeableObjectTypeName()));
70
71 /** @var Like $like */
72 $like = LikeHandler::getInstance()->like(
73 $likeObject,
74 $author->getDecoratedObject(),
75 Like::LIKE
76 )['like'];
77
78 return [new LikeUserNotificationObject($like)];
79 }
80 }