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