as soon as you use the new keyword, you loose all benefits of dependency injection and the magento object manager.
There is a pretty easy (and mostly the correct) way to instantiate any non-injectable object with a factory. You should always try to use factories and do not instantiate them with the new
keyword or by calling the objectmanager directly.
Just inject OAuth\Common\Consumer\CredentialsFactory
in your constructor.
The Magento objectmanager will automatically create this factory for your object (by naming convention)
you can then call the create
method on the factory and pass in your arguments as an associated array
$credentials = $this->credentialsFactory->create([
'consumerId' => $this->getConsumerId(),
'consumerSecret' => $this->getConsumerSecret(),
'callbackUrl' => $this->getCallbackUrl()
]);
The array keys here have to match the constructor parameters ob the original Credentials Class
Here is an example including the constructor injection
<?php
namespace Vendor\Module
class Class
{
/** @var \OAuth\Common\Consumer\CredentialsFactory */
private $credentialsFactory;
public function __construct(
\OAuth\Common\Consumer\CredentialsFactory $credentialsFactory
) {
$this->credentialsFactory = $credentialsFactory;
}
public function getCredentials($consumerId, $consumerSecret, $callbackUrl): \OAuth\Common\Consumer\Credentials
{
$this->credentialsFactory->create([
'consumerId' => $consumerId,
'consumerSecret' => $consumerSecret,
'callbackUrl' => $callbackUrl
]);
}
}
Here you find the magento documentation on factories and also on when they can be auto generated