Retrieve current user in Symfony app while respecting LoD












1















I'm having some issues understanding how the Law of Demeter should be applied in some cases with Symfony's DI system.



I have some factory that requires to access current logged in user in the application. To do that I need to require @security.token_storage to inject it as a constructor argument.



But in my factory, to access the user I will need to do : $tokenStorage->getToken()->getUser(), and worst, if I want to access some property of my user, I will need to dive one level deeper.



How would you fix this issue according to the law of demeter ?



Here is a sample of my code :



class SomeFactory
{

/**
* @var User
*/
private $currentUser;

/**
* @param TokenStorageInterface $tokenStorage
*/
public function __construct(TokenStorageInterface $tokenStorage)
{
$this->currentUser = $this->setCurrentUser($tokenStorage);
}

/**
* @param TokenStorageInterface $tokenStorage
*/
protected function setCurrentUser(TokenStorageInterface $tokenStorage)
{
if ($tokenStorage->getToken()
&& $tokenStorage->getToken()->getUser()
&& in_array('ADMIN_ROLE', $tokenStorage->getToken()->getUser()->getRoles())
) {
$this->currentUser = $tokenStorage->getToken()->getUser();
}
}
}


I hope i am being clear.



Thank you very much :)










share|improve this question























  • It's doubtful that even Demeter expected his self-proclaimed law to be taken seriously. You could probably define a factory service to inject the current user with a type hint. But you need to be careful. The user is not available until part way through the request processing cycle. If your SomeFactory is instantiated before the user is available then you would suffer the consequences of premature injection.

    – Cerad
    Nov 25 '18 at 16:53











  • Yes actually that's another issue I just encountered. Since I'm trying to inject the user too early, the conditions are never met in my setCurrentUser method. It seems like what ritter is suggesting on this post is true : stackoverflow.com/questions/32590621/… The service should keep a reference to the tokenStorage so that when the method needing the current user is called, it can access the right value of the user logged in at that particular moment. Otherwise the value is null. So I guess Demeter is not something to be applied here.

    – simsComputing
    Nov 25 '18 at 17:10


















1















I'm having some issues understanding how the Law of Demeter should be applied in some cases with Symfony's DI system.



I have some factory that requires to access current logged in user in the application. To do that I need to require @security.token_storage to inject it as a constructor argument.



But in my factory, to access the user I will need to do : $tokenStorage->getToken()->getUser(), and worst, if I want to access some property of my user, I will need to dive one level deeper.



How would you fix this issue according to the law of demeter ?



Here is a sample of my code :



class SomeFactory
{

/**
* @var User
*/
private $currentUser;

/**
* @param TokenStorageInterface $tokenStorage
*/
public function __construct(TokenStorageInterface $tokenStorage)
{
$this->currentUser = $this->setCurrentUser($tokenStorage);
}

/**
* @param TokenStorageInterface $tokenStorage
*/
protected function setCurrentUser(TokenStorageInterface $tokenStorage)
{
if ($tokenStorage->getToken()
&& $tokenStorage->getToken()->getUser()
&& in_array('ADMIN_ROLE', $tokenStorage->getToken()->getUser()->getRoles())
) {
$this->currentUser = $tokenStorage->getToken()->getUser();
}
}
}


I hope i am being clear.



Thank you very much :)










share|improve this question























  • It's doubtful that even Demeter expected his self-proclaimed law to be taken seriously. You could probably define a factory service to inject the current user with a type hint. But you need to be careful. The user is not available until part way through the request processing cycle. If your SomeFactory is instantiated before the user is available then you would suffer the consequences of premature injection.

    – Cerad
    Nov 25 '18 at 16:53











  • Yes actually that's another issue I just encountered. Since I'm trying to inject the user too early, the conditions are never met in my setCurrentUser method. It seems like what ritter is suggesting on this post is true : stackoverflow.com/questions/32590621/… The service should keep a reference to the tokenStorage so that when the method needing the current user is called, it can access the right value of the user logged in at that particular moment. Otherwise the value is null. So I guess Demeter is not something to be applied here.

    – simsComputing
    Nov 25 '18 at 17:10
















1












1








1








I'm having some issues understanding how the Law of Demeter should be applied in some cases with Symfony's DI system.



I have some factory that requires to access current logged in user in the application. To do that I need to require @security.token_storage to inject it as a constructor argument.



But in my factory, to access the user I will need to do : $tokenStorage->getToken()->getUser(), and worst, if I want to access some property of my user, I will need to dive one level deeper.



How would you fix this issue according to the law of demeter ?



Here is a sample of my code :



class SomeFactory
{

/**
* @var User
*/
private $currentUser;

/**
* @param TokenStorageInterface $tokenStorage
*/
public function __construct(TokenStorageInterface $tokenStorage)
{
$this->currentUser = $this->setCurrentUser($tokenStorage);
}

/**
* @param TokenStorageInterface $tokenStorage
*/
protected function setCurrentUser(TokenStorageInterface $tokenStorage)
{
if ($tokenStorage->getToken()
&& $tokenStorage->getToken()->getUser()
&& in_array('ADMIN_ROLE', $tokenStorage->getToken()->getUser()->getRoles())
) {
$this->currentUser = $tokenStorage->getToken()->getUser();
}
}
}


I hope i am being clear.



Thank you very much :)










share|improve this question














I'm having some issues understanding how the Law of Demeter should be applied in some cases with Symfony's DI system.



I have some factory that requires to access current logged in user in the application. To do that I need to require @security.token_storage to inject it as a constructor argument.



But in my factory, to access the user I will need to do : $tokenStorage->getToken()->getUser(), and worst, if I want to access some property of my user, I will need to dive one level deeper.



How would you fix this issue according to the law of demeter ?



Here is a sample of my code :



class SomeFactory
{

/**
* @var User
*/
private $currentUser;

/**
* @param TokenStorageInterface $tokenStorage
*/
public function __construct(TokenStorageInterface $tokenStorage)
{
$this->currentUser = $this->setCurrentUser($tokenStorage);
}

/**
* @param TokenStorageInterface $tokenStorage
*/
protected function setCurrentUser(TokenStorageInterface $tokenStorage)
{
if ($tokenStorage->getToken()
&& $tokenStorage->getToken()->getUser()
&& in_array('ADMIN_ROLE', $tokenStorage->getToken()->getUser()->getRoles())
) {
$this->currentUser = $tokenStorage->getToken()->getUser();
}
}
}


I hope i am being clear.



Thank you very much :)







symfony dependency-injection law-of-demeter






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 25 '18 at 16:26









simsComputingsimsComputing

176




176













  • It's doubtful that even Demeter expected his self-proclaimed law to be taken seriously. You could probably define a factory service to inject the current user with a type hint. But you need to be careful. The user is not available until part way through the request processing cycle. If your SomeFactory is instantiated before the user is available then you would suffer the consequences of premature injection.

    – Cerad
    Nov 25 '18 at 16:53











  • Yes actually that's another issue I just encountered. Since I'm trying to inject the user too early, the conditions are never met in my setCurrentUser method. It seems like what ritter is suggesting on this post is true : stackoverflow.com/questions/32590621/… The service should keep a reference to the tokenStorage so that when the method needing the current user is called, it can access the right value of the user logged in at that particular moment. Otherwise the value is null. So I guess Demeter is not something to be applied here.

    – simsComputing
    Nov 25 '18 at 17:10





















  • It's doubtful that even Demeter expected his self-proclaimed law to be taken seriously. You could probably define a factory service to inject the current user with a type hint. But you need to be careful. The user is not available until part way through the request processing cycle. If your SomeFactory is instantiated before the user is available then you would suffer the consequences of premature injection.

    – Cerad
    Nov 25 '18 at 16:53











  • Yes actually that's another issue I just encountered. Since I'm trying to inject the user too early, the conditions are never met in my setCurrentUser method. It seems like what ritter is suggesting on this post is true : stackoverflow.com/questions/32590621/… The service should keep a reference to the tokenStorage so that when the method needing the current user is called, it can access the right value of the user logged in at that particular moment. Otherwise the value is null. So I guess Demeter is not something to be applied here.

    – simsComputing
    Nov 25 '18 at 17:10



















It's doubtful that even Demeter expected his self-proclaimed law to be taken seriously. You could probably define a factory service to inject the current user with a type hint. But you need to be careful. The user is not available until part way through the request processing cycle. If your SomeFactory is instantiated before the user is available then you would suffer the consequences of premature injection.

– Cerad
Nov 25 '18 at 16:53





It's doubtful that even Demeter expected his self-proclaimed law to be taken seriously. You could probably define a factory service to inject the current user with a type hint. But you need to be careful. The user is not available until part way through the request processing cycle. If your SomeFactory is instantiated before the user is available then you would suffer the consequences of premature injection.

– Cerad
Nov 25 '18 at 16:53













Yes actually that's another issue I just encountered. Since I'm trying to inject the user too early, the conditions are never met in my setCurrentUser method. It seems like what ritter is suggesting on this post is true : stackoverflow.com/questions/32590621/… The service should keep a reference to the tokenStorage so that when the method needing the current user is called, it can access the right value of the user logged in at that particular moment. Otherwise the value is null. So I guess Demeter is not something to be applied here.

– simsComputing
Nov 25 '18 at 17:10







Yes actually that's another issue I just encountered. Since I'm trying to inject the user too early, the conditions are never met in my setCurrentUser method. It seems like what ritter is suggesting on this post is true : stackoverflow.com/questions/32590621/… The service should keep a reference to the tokenStorage so that when the method needing the current user is called, it can access the right value of the user logged in at that particular moment. Otherwise the value is null. So I guess Demeter is not something to be applied here.

– simsComputing
Nov 25 '18 at 17:10














0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53469503%2fretrieve-current-user-in-symfony-app-while-respecting-lod%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53469503%2fretrieve-current-user-in-symfony-app-while-respecting-lod%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Contact image not getting when fetch all contact list from iPhone by CNContact

count number of partitions of a set with n elements into k subsets

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks