[Stamping\StampService::class, Stamping\StampingCommand::class], 'quickstamp' => [Stamping\QuickStampService::class, Stamping\StampingCommand::class], 'stamped' => [Stamping\StampedService::class, Stamping\StampingCommand::class], 'stampQueryPending' => [ Stamping\QueryPendingService::class, Stamping\QueryPendingCommand::class, 'queryPending', // override method name on service ], 'cancelSignature' => [Cancel\CancelSignatureService::class, Cancel\CancelSignatureCommand::class], 'getPendingToCancel' => [Cancel\GetPendingService::class, Cancel\GetPendingCommand::class, 'obtainPending'], 'getCancelReceipt' => [Cancel\GetReceiptService::class, Cancel\GetReceiptResult::class, 'download'], 'getSatStatus' => [Cancel\GetSatStatusService::class, Cancel\GetSatStatusCommand::class, 'query'], 'getRelatedSignature' => [ Cancel\GetRelatedSignatureService::class, Cancel\GetRelatedSignatureCommand::class, ], 'acceptRejectSignature' => [ Cancel\AcceptRejectSignatureService::class, Cancel\AcceptRejectSignatureCommand::class, ], 'datetime' => [Utilities\DatetimeService::class, Utilities\DatetimeCommand::class], 'downloadXml' => [Utilities\DownloadXmlService::class, Utilities\DownloadXmlCommand::class], 'reportCredit' => [Utilities\ReportCreditService::class, Utilities\ReportCreditCommand::class], 'reportTotal' => [Utilities\ReportTotalService::class, Utilities\ReportTotalCommand::class], 'reportUuid' => [Utilities\ReportUuidService::class, Utilities\ReportUuidCommand::class], 'getContracts' => [Manifest\GetContractsService::class, Manifest\GetContractsCommand::class, 'obtainContracts'], 'signContracts' => [ Manifest\SignContractsService::class, Manifest\SignContractsCommand::class, 'sendSignedContracts', ], 'getSignedContracts' => [ Manifest\GetSignedContractsService::class, Manifest\GetSignedContractsCommand::class, ], 'registrationAdd' => [Registration\AddService::class, Registration\AddCommand::class, 'add'], 'registrationAssign' => [Registration\AssignService::class, Registration\AssignCommand::class, 'assign'], 'registrationSwitch' => [Registration\SwitchService::class, Registration\SwitchCommand::class, 'switch'], 'registrationEdit' => [Registration\EditService::class, Registration\EditCommand::class, 'edit'], 'registrationObtain' => [Registration\ObtainService::class, Registration\ObtainCommand::class, 'obtain'], 'registrationCustomers' => [ Registration\ObtainCustomersService::class, Registration\ObtainCustomersCommand::class, 'obtainPage', ], ]; /** @var FinkokSettings */ private $settings; public function __construct(FinkokSettings $factory) { $this->settings = $factory; } public function settings(): FinkokSettings { return $this->settings; } /** * @param string $name * @param array $arguments * @return mixed */ public function __call(string $name, array $arguments) { if (array_key_exists($name, static::SERVICES_MAP)) { $command = $this->checkCommand($name, $arguments[0] ?? null); $service = $this->createService($name); return $this->executeService($name, $service, $command); } throw new BadMethodCallException(sprintf('Helper %s is not registered', $name)); } /** * @param string $method * @param mixed $command * @return object|null */ protected function checkCommand(string $method, $command): ?object { $expected = static::SERVICES_MAP[$method][1]; if ('' === $expected) { return null; } if (! is_object($command) || ! is_a($command, $expected)) { $type = (is_object($command)) ? get_class($command) : gettype($command); throw new InvalidArgumentException( sprintf('Call %s::%s expect %s but received %s', static::class, $method, $expected, $type) ); } return $command; } /** * @param string $method * @return object */ protected function createService(string $method): object { $serviceClass = static::SERVICES_MAP[$method][0]; return new $serviceClass($this->settings); } /** * @param string $method * @param object $service * @param object|null $command * @return mixed */ protected function executeService(string $method, object $service, ?object $command) { $method = static::SERVICES_MAP[$method][2] ?? $method; if (! is_callable([$service, $method])) { throw new BadMethodCallException( sprintf('The service %s does not have a method %s', get_class($service), $method) ); } return $service->{$method}($command); } }