Constructing objects for external libraries using a model in Laravel
up vote
1
down vote
favorite
I want to know if it's more appropriate to use a separate class (such as a factory?) to construct an object. While the code works, I am concerned about bloating of the model and the tight coupling.
To construct the said object I need:
- environment variables like api tokens
- some of the user's attributes from the model
- classes from the library
Here's the relevant snippet within my user model that creates an AdWordsSession object for use in the third-party library:
class User extends Authenticatable {
...
public function adWordsSession($customer_id = null){
if(!$this->google_auth_token) {
return null;
}
if( $this->adWordsSesssion &&
(
(isset($customer_id) && $this->adWordsSesssion->getClientCustomerId() == $customer_id) ||
(!isset($customer_id) && $this->adWordsSesssion->getClientCustomerId() == $this->adwords_customer_id)
)
){
return $this->adWordsSesssion;
}
$oAuth2Credential = (new OAuth2TokenBuilder)
->withClientId(env('ADWORDS_CLIENT_ID'))
->withClientSecret(env('ADWORDS_CLIENT_SECRET'))
->withRefreshToken($this->google_auth_token)
->build();
$session = (new AdWordsSessionBuilder())
->withDeveloperToken(env('ADWORDS_DEVELOPER_TOKEN'))
->withUserAgent(env('ADWORDS_USER_AGENT'))
->withClientCustomerId($customer_id ?: $this->adwords_customer_id)
->withOAuth2Credential($oAuth2Credential)
->build();
$this->adWordsSesssion = $session;
return $session;
}
...
}
I am also interested to know if it applies to the reverse, using a class returned from the library (web service) to construct a model object. Should this be a static method of the model or a separate factory class. Code follows:
class AdWordsCampaign {
public static function fromAdWordsClass(Campaign $campaign, AdWordsCampaign $adWordsCampaign = null): AdWordsCampaign {
$self = $adWordsCampaign;
if(is_null($adWordsCampaign)) {
$self = new self;
$self->criterions = $self->defaultCriterions();
$self->extensions = $self->defaultExtensions();
}
if($campaign->getId()) {
$self->fieldsHelper()->id = $campaign->getId();
$self->setPublished();
}
$self->fieldsHelper()->name = $campaign->getName();
$self->fieldsHelper()->status = $campaign->getStatus();
$self->fieldsHelper()->advertisingChannelType = $campaign->getAdvertisingChannelType();
$self->fieldsHelper()->advertisingChannelSubType = $campaign->getAdvertisingChannelSubType();
$self->fieldsHelper()->budget = arrayify_object(
$campaign->getBudget() ?
$campaign->getBudget() :
CampaignFields::defaultBudget()
);
return $self;
}
}
php laravel
bumped to the homepage by Community♦ 2 days ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
add a comment |
up vote
1
down vote
favorite
I want to know if it's more appropriate to use a separate class (such as a factory?) to construct an object. While the code works, I am concerned about bloating of the model and the tight coupling.
To construct the said object I need:
- environment variables like api tokens
- some of the user's attributes from the model
- classes from the library
Here's the relevant snippet within my user model that creates an AdWordsSession object for use in the third-party library:
class User extends Authenticatable {
...
public function adWordsSession($customer_id = null){
if(!$this->google_auth_token) {
return null;
}
if( $this->adWordsSesssion &&
(
(isset($customer_id) && $this->adWordsSesssion->getClientCustomerId() == $customer_id) ||
(!isset($customer_id) && $this->adWordsSesssion->getClientCustomerId() == $this->adwords_customer_id)
)
){
return $this->adWordsSesssion;
}
$oAuth2Credential = (new OAuth2TokenBuilder)
->withClientId(env('ADWORDS_CLIENT_ID'))
->withClientSecret(env('ADWORDS_CLIENT_SECRET'))
->withRefreshToken($this->google_auth_token)
->build();
$session = (new AdWordsSessionBuilder())
->withDeveloperToken(env('ADWORDS_DEVELOPER_TOKEN'))
->withUserAgent(env('ADWORDS_USER_AGENT'))
->withClientCustomerId($customer_id ?: $this->adwords_customer_id)
->withOAuth2Credential($oAuth2Credential)
->build();
$this->adWordsSesssion = $session;
return $session;
}
...
}
I am also interested to know if it applies to the reverse, using a class returned from the library (web service) to construct a model object. Should this be a static method of the model or a separate factory class. Code follows:
class AdWordsCampaign {
public static function fromAdWordsClass(Campaign $campaign, AdWordsCampaign $adWordsCampaign = null): AdWordsCampaign {
$self = $adWordsCampaign;
if(is_null($adWordsCampaign)) {
$self = new self;
$self->criterions = $self->defaultCriterions();
$self->extensions = $self->defaultExtensions();
}
if($campaign->getId()) {
$self->fieldsHelper()->id = $campaign->getId();
$self->setPublished();
}
$self->fieldsHelper()->name = $campaign->getName();
$self->fieldsHelper()->status = $campaign->getStatus();
$self->fieldsHelper()->advertisingChannelType = $campaign->getAdvertisingChannelType();
$self->fieldsHelper()->advertisingChannelSubType = $campaign->getAdvertisingChannelSubType();
$self->fieldsHelper()->budget = arrayify_object(
$campaign->getBudget() ?
$campaign->getBudget() :
CampaignFields::defaultBudget()
);
return $self;
}
}
php laravel
bumped to the homepage by Community♦ 2 days ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I want to know if it's more appropriate to use a separate class (such as a factory?) to construct an object. While the code works, I am concerned about bloating of the model and the tight coupling.
To construct the said object I need:
- environment variables like api tokens
- some of the user's attributes from the model
- classes from the library
Here's the relevant snippet within my user model that creates an AdWordsSession object for use in the third-party library:
class User extends Authenticatable {
...
public function adWordsSession($customer_id = null){
if(!$this->google_auth_token) {
return null;
}
if( $this->adWordsSesssion &&
(
(isset($customer_id) && $this->adWordsSesssion->getClientCustomerId() == $customer_id) ||
(!isset($customer_id) && $this->adWordsSesssion->getClientCustomerId() == $this->adwords_customer_id)
)
){
return $this->adWordsSesssion;
}
$oAuth2Credential = (new OAuth2TokenBuilder)
->withClientId(env('ADWORDS_CLIENT_ID'))
->withClientSecret(env('ADWORDS_CLIENT_SECRET'))
->withRefreshToken($this->google_auth_token)
->build();
$session = (new AdWordsSessionBuilder())
->withDeveloperToken(env('ADWORDS_DEVELOPER_TOKEN'))
->withUserAgent(env('ADWORDS_USER_AGENT'))
->withClientCustomerId($customer_id ?: $this->adwords_customer_id)
->withOAuth2Credential($oAuth2Credential)
->build();
$this->adWordsSesssion = $session;
return $session;
}
...
}
I am also interested to know if it applies to the reverse, using a class returned from the library (web service) to construct a model object. Should this be a static method of the model or a separate factory class. Code follows:
class AdWordsCampaign {
public static function fromAdWordsClass(Campaign $campaign, AdWordsCampaign $adWordsCampaign = null): AdWordsCampaign {
$self = $adWordsCampaign;
if(is_null($adWordsCampaign)) {
$self = new self;
$self->criterions = $self->defaultCriterions();
$self->extensions = $self->defaultExtensions();
}
if($campaign->getId()) {
$self->fieldsHelper()->id = $campaign->getId();
$self->setPublished();
}
$self->fieldsHelper()->name = $campaign->getName();
$self->fieldsHelper()->status = $campaign->getStatus();
$self->fieldsHelper()->advertisingChannelType = $campaign->getAdvertisingChannelType();
$self->fieldsHelper()->advertisingChannelSubType = $campaign->getAdvertisingChannelSubType();
$self->fieldsHelper()->budget = arrayify_object(
$campaign->getBudget() ?
$campaign->getBudget() :
CampaignFields::defaultBudget()
);
return $self;
}
}
php laravel
I want to know if it's more appropriate to use a separate class (such as a factory?) to construct an object. While the code works, I am concerned about bloating of the model and the tight coupling.
To construct the said object I need:
- environment variables like api tokens
- some of the user's attributes from the model
- classes from the library
Here's the relevant snippet within my user model that creates an AdWordsSession object for use in the third-party library:
class User extends Authenticatable {
...
public function adWordsSession($customer_id = null){
if(!$this->google_auth_token) {
return null;
}
if( $this->adWordsSesssion &&
(
(isset($customer_id) && $this->adWordsSesssion->getClientCustomerId() == $customer_id) ||
(!isset($customer_id) && $this->adWordsSesssion->getClientCustomerId() == $this->adwords_customer_id)
)
){
return $this->adWordsSesssion;
}
$oAuth2Credential = (new OAuth2TokenBuilder)
->withClientId(env('ADWORDS_CLIENT_ID'))
->withClientSecret(env('ADWORDS_CLIENT_SECRET'))
->withRefreshToken($this->google_auth_token)
->build();
$session = (new AdWordsSessionBuilder())
->withDeveloperToken(env('ADWORDS_DEVELOPER_TOKEN'))
->withUserAgent(env('ADWORDS_USER_AGENT'))
->withClientCustomerId($customer_id ?: $this->adwords_customer_id)
->withOAuth2Credential($oAuth2Credential)
->build();
$this->adWordsSesssion = $session;
return $session;
}
...
}
I am also interested to know if it applies to the reverse, using a class returned from the library (web service) to construct a model object. Should this be a static method of the model or a separate factory class. Code follows:
class AdWordsCampaign {
public static function fromAdWordsClass(Campaign $campaign, AdWordsCampaign $adWordsCampaign = null): AdWordsCampaign {
$self = $adWordsCampaign;
if(is_null($adWordsCampaign)) {
$self = new self;
$self->criterions = $self->defaultCriterions();
$self->extensions = $self->defaultExtensions();
}
if($campaign->getId()) {
$self->fieldsHelper()->id = $campaign->getId();
$self->setPublished();
}
$self->fieldsHelper()->name = $campaign->getName();
$self->fieldsHelper()->status = $campaign->getStatus();
$self->fieldsHelper()->advertisingChannelType = $campaign->getAdvertisingChannelType();
$self->fieldsHelper()->advertisingChannelSubType = $campaign->getAdvertisingChannelSubType();
$self->fieldsHelper()->budget = arrayify_object(
$campaign->getBudget() ?
$campaign->getBudget() :
CampaignFields::defaultBudget()
);
return $self;
}
}
php laravel
php laravel
asked Jan 8 '17 at 19:19
JC Lee
11615
11615
bumped to the homepage by Community♦ 2 days ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
bumped to the homepage by Community♦ 2 days ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
I would implement this as a service in Laravel, and use IOC to resolve my dependencies.
I would make a service, called AdWordsCampaign. This would resolve its dependencies in constructor, namely OAuth2TokenBuilder and AdWordsSessionBuilder, although these two classes are good candidates for services themselves (a factory pattern is most of times is a sign you should abstract creation of class into a service provider).
Then I would implement a method in this service, adWordsSession which takes two parameters: a User instance and $customer_id. Then it has everything it needs to to its job.
I would also implement the reverse case using a service. Semantics are the same.
As for static method, I would avoid them. If you like them, use a Facade. They enable you to use a service anywhere you want.
I strongly suggest to read Laravel docs about services and providers and IOC and Facades. It will give you enough insight about how they work, and where to use them.
As a rule of thumb, whenever you see yourself using new * in your codes, it seems your could use a service/provider and inject it.
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
I would implement this as a service in Laravel, and use IOC to resolve my dependencies.
I would make a service, called AdWordsCampaign. This would resolve its dependencies in constructor, namely OAuth2TokenBuilder and AdWordsSessionBuilder, although these two classes are good candidates for services themselves (a factory pattern is most of times is a sign you should abstract creation of class into a service provider).
Then I would implement a method in this service, adWordsSession which takes two parameters: a User instance and $customer_id. Then it has everything it needs to to its job.
I would also implement the reverse case using a service. Semantics are the same.
As for static method, I would avoid them. If you like them, use a Facade. They enable you to use a service anywhere you want.
I strongly suggest to read Laravel docs about services and providers and IOC and Facades. It will give you enough insight about how they work, and where to use them.
As a rule of thumb, whenever you see yourself using new * in your codes, it seems your could use a service/provider and inject it.
add a comment |
up vote
0
down vote
I would implement this as a service in Laravel, and use IOC to resolve my dependencies.
I would make a service, called AdWordsCampaign. This would resolve its dependencies in constructor, namely OAuth2TokenBuilder and AdWordsSessionBuilder, although these two classes are good candidates for services themselves (a factory pattern is most of times is a sign you should abstract creation of class into a service provider).
Then I would implement a method in this service, adWordsSession which takes two parameters: a User instance and $customer_id. Then it has everything it needs to to its job.
I would also implement the reverse case using a service. Semantics are the same.
As for static method, I would avoid them. If you like them, use a Facade. They enable you to use a service anywhere you want.
I strongly suggest to read Laravel docs about services and providers and IOC and Facades. It will give you enough insight about how they work, and where to use them.
As a rule of thumb, whenever you see yourself using new * in your codes, it seems your could use a service/provider and inject it.
add a comment |
up vote
0
down vote
up vote
0
down vote
I would implement this as a service in Laravel, and use IOC to resolve my dependencies.
I would make a service, called AdWordsCampaign. This would resolve its dependencies in constructor, namely OAuth2TokenBuilder and AdWordsSessionBuilder, although these two classes are good candidates for services themselves (a factory pattern is most of times is a sign you should abstract creation of class into a service provider).
Then I would implement a method in this service, adWordsSession which takes two parameters: a User instance and $customer_id. Then it has everything it needs to to its job.
I would also implement the reverse case using a service. Semantics are the same.
As for static method, I would avoid them. If you like them, use a Facade. They enable you to use a service anywhere you want.
I strongly suggest to read Laravel docs about services and providers and IOC and Facades. It will give you enough insight about how they work, and where to use them.
As a rule of thumb, whenever you see yourself using new * in your codes, it seems your could use a service/provider and inject it.
I would implement this as a service in Laravel, and use IOC to resolve my dependencies.
I would make a service, called AdWordsCampaign. This would resolve its dependencies in constructor, namely OAuth2TokenBuilder and AdWordsSessionBuilder, although these two classes are good candidates for services themselves (a factory pattern is most of times is a sign you should abstract creation of class into a service provider).
Then I would implement a method in this service, adWordsSession which takes two parameters: a User instance and $customer_id. Then it has everything it needs to to its job.
I would also implement the reverse case using a service. Semantics are the same.
As for static method, I would avoid them. If you like them, use a Facade. They enable you to use a service anywhere you want.
I strongly suggest to read Laravel docs about services and providers and IOC and Facades. It will give you enough insight about how they work, and where to use them.
As a rule of thumb, whenever you see yourself using new * in your codes, it seems your could use a service/provider and inject it.
answered Jan 9 '17 at 6:54
vfsoraki
1615
1615
add a comment |
add a comment |
Thanks for contributing an answer to Code Review Stack Exchange!
- 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.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f152070%2fconstructing-objects-for-external-libraries-using-a-model-in-laravel%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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