Currently the only way to set a global preference from the code of another extension is GlobalPreferencesFactory::setGlobalPreferences() which sucks in so many ways:
- it's big and heavy and does a bunch of things only remotely related to writing a global preference value, like generating HtmlForm elements for the global preferences form
- it expects a full list of the user's global preferences, ie. you need to pass it all existing preferences of the user, otherwise you have just deleted all the user's preferences
- this also allows for all kinds of exciting race conditions
- it will break when the current request does not have a title, and behaves differently depending on what that title is
- it calls the GlobalPreferencesSetGlobalPreferences hook, which probably shouldn't be called for programmatic changes
You can directly create a Storage object and use Storage::save(), which is a little saner, although it still requires the full list of the user's preferences; but that's not exposed via the service container and it's fragile to instantiate another extension's internal classes directly.
Or you can do a faux API call to ApiGlobalPreferences (which uses GlobalPreferencesFactory internally), but API calls are quite costly, even faux ones.
There really should be a better way to do this.
(One can update an existing global preference via UserOptionsManager, but that has no way to globally set a preference that wasn't global before.)