New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is it possible to call methods with Symfony services intances? #456

Closed
Kocal opened this Issue Feb 4, 2019 · 6 comments

Comments

Projects
None yet
2 participants
@Kocal
Copy link

Kocal commented Feb 4, 2019

Hi, I'm having an issue when writing fixtures that should call a method that takes a service instance.

I'm writing fixtures for configuring some payment gateways (for Payum).
It's works fine unless I try to encrypt them, because I don't know how to do that.

This is what I have actually:

App\Entity\Payment\GatewayConfig:
  first_gateway_config:
    gatewayName: 'gateway_1'
    factoryName: 'system_pay'
    config:
      sandbox: true
      field_1: "XXX"
      field_2: "YYY"

Fields gatewayName and factoryName will be stored in plain text, but config should be encrypted.

My entity App\Entity\Payment\GatewayConfig extends from Payum\Core\Model\GatewayConfig, so I should call $gatewayConfig->encrypt($cypher) method, where $cypher is an instance of Payum\Core\Security\CypherInterface.

So I thought about adding a new call like this, but of course it does not work:

App\Entity\Payment\GatewayConfig:
  first_gateway_config:
    gatewayName: 'gateway_1'
    factoryName: 'system_pay'
    config:
      sandbox: true
      field_1: "XXX"
      field_2: "YYY"
    __calls:
      - encrypt: [<(Payum\Core\Security\CypherInterface::class)>]

Does someone here have already encountered the same issue? Is there a way to do this?

Thanks!

@theofidry

This comment has been minimized.

Copy link
Collaborator

theofidry commented Feb 4, 2019

The easiest way I think would be to register a custom faker provider

@Kocal

This comment has been minimized.

Copy link
Author

Kocal commented Feb 4, 2019

I already thought about it, but I don't know how to use it.

Faker providers only work on entity's fields and not entity itself, right? 🤔

@theofidry

This comment has been minimized.

Copy link
Collaborator

theofidry commented Feb 4, 2019

You can the entity itself with @self IIRC

@Kocal

This comment has been minimized.

Copy link
Author

Kocal commented Feb 4, 2019

Oh, didn't know about this.

So I suppose I will have to create a custom Faker Provider (e.g. EncryptGatewayConfig), register it, and call <encryptGatewayConfig(@self)>, but how/where should I call it? I can't put it on a field, right?

@theofidry

This comment has been minimized.

Copy link
Collaborator

theofidry commented Feb 4, 2019

Ah I see what you mean. Maybe you can try:

App\Entity\Payment\GatewayConfig:
  first_gateway_config:
    gatewayName: 'gateway_1'
    factoryName: 'system_pay'
    config:
      sandbox: true
      field_1: "XXX"
      field_2: "YYY"
    __calls:
      - '@my-encrypt-service::encrypt': ['@self']

It works for factories but I don't know about regular calls.

Alternatively you can register a pre-processor to do the necessary calls on your object

@Kocal

This comment has been minimized.

Copy link
Author

Kocal commented Feb 4, 2019

I just tried with the first option and as expected it does not work:

PHP Fatal error:  Uncaught Error: Call to undefined method Nelmio\Alice\Throwable\Exception\Generator\Caller\CallProcessorExceptionFactory::createForNoParserFoundForElement() in /srv/app/vendor/nelmio/alice/src/Generator/Caller/CallProcessorRegistry.php:59
Stack trace:
#0 /srv/app/vendor/nelmio/alice/src/Generator/Caller/SimpleCaller.php(80): Nelmio\Alice\Generator\Caller\CallProcessorRegistry->process(Object(Nelmio\Alice\Definition\Object\SimpleObject), Object(Nelmio\Alice\Generator\ResolvedFixtureSet), Object(Nelmio\Alice\Generator\GenerationContext), Object(Nelmio\Alice\Definition\MethodCall\MethodCallWithReference))
#1 /srv/app/vendor/nelmio/alice/src/Generator/ObjectGenerator/SimpleObjectGenerator.php(147): Nelmio\Alice\Generator\Caller\SimpleCaller->doCallsOn(Object(Nelmio\Alice\Definition\Object\SimpleObject), Object(Nelmio\Alice\Generator\ResolvedFixtureSet), Object(Nelmio\Alice\Generator\GenerationContext))
#2 /srv/app/vendor/nelmio/alice/src/Generator/ObjectGenerator/SimpleObjectGenerator.php(132): Nelmio\Alice\Gener in /srv/app/vendor/nelmio/alice/src/Generator/ObjectGenerator/SimpleObjectGenerator.php on line 114

Fatal error: Uncaught Error: Call to undefined method Nelmio\Alice\Throwable\Exception\Generator\Caller\CallProcessorExceptionFactory::createForNoParserFoundForElement() in /srv/app/vendor/nelmio/alice/src/Generator/Caller/CallProcessorRegistry.php:59
Stack trace:
#0 /srv/app/vendor/nelmio/alice/src/Generator/Caller/SimpleCaller.php(80): Nelmio\Alice\Generator\Caller\CallProcessorRegistry->process(Object(Nelmio\Alice\Definition\Object\SimpleObject), Object(Nelmio\Alice\Generator\ResolvedFixtureSet), Object(Nelmio\Alice\Generator\GenerationContext), Object(Nelmio\Alice\Definition\MethodCall\MethodCallWithReference))
#1 /srv/app/vendor/nelmio/alice/src/Generator/ObjectGenerator/SimpleObjectGenerator.php(147): Nelmio\Alice\Generator\Caller\SimpleCaller->doCallsOn(Object(Nelmio\Alice\Definition\Object\SimpleObject), Object(Nelmio\Alice\Generator\ResolvedFixtureSet), Object(Nelmio\Alice\Generator\GenerationContext))
#2 /srv/app/vendor/nelmio/alice/src/Generator/ObjectGenerator/SimpleObjectGenerator.php(132): Nelmio\Alice\Gener in /srv/app/vendor/nelmio/alice/src/Generator/ObjectGenerator/SimpleObjectGenerator.php on line 114

Anyway I was going to use a custom pre-processor because it's cleaner (and it's working properly 😝).

Thanks for your time, you rocks! 🙂

@Kocal Kocal closed this Feb 4, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment