As designed Inconsistency: XF\Service\Thread\Creator::setUser() vs. XF\Service\Thread\Replies::setUser()

Kirby

Well-known member
Affected version
2.0 DP 10
XF\Service\Thread\Creator::setUser() is protected, whereas XF\Service\Thread\Replier::setUser() is public.

This seems inconsistent to me and does make it rather cumbersome to post a new thread as a specific user:
PHP:
$creator = \XF::asVisitor($user, function() use ($forum)
{
	return \XF::service('XF:Thread\Creator', $forum);
});

instead of just
PHP:
$creator = \XF::service('XF:Thread\Creator', $forum);
$creator->setUser($user);
 
It's deliberate.

A bunch of other services (like the Replier) are now structured in the same was as the Creator service (which was finished after the release of DP10).

Your first example is exactly the approach that should be taken.

It's important to ensure that the user cannot change during the lifetime of the service, as that could result in some unexpected behaviour. With that in mind, the setUser method shouldn't be public, and the user should be set and stored based on the visitor User entity when the service is instantiated. If there's a need to run the service as a different user, that can be done using the asVisitor function.
 
Back
Top Bottom