Advertisement
fastje

Untitled

Jun 19th, 2023
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.00 KB | None | 0 0
  1. <?php
  2.  
  3.  
  4. class AmoCRMAdapter implements CRMAdapterInterface
  5. {
  6. private AmoCRMApiClient $apiClient;
  7.  
  8. public function __construct(
  9. private readonly string $clientId,
  10. private readonly string $clientSecret,
  11. private readonly string $redirectUri,
  12. private readonly string $domain,
  13. private readonly AmoCRMTokenStorage $tokenStorage,
  14. private readonly LoggerInterface $amoCrmLogger
  15. ) {
  16. $this->apiClient = new AmoCRMApiClient($this->clientId, $this->clientSecret, $this->redirectUri);
  17. }
  18.  
  19. public function getAccessTokenByCode(string $code): void
  20. {
  21. try {
  22. $accessToken = $this->apiClient->getOAuthClient()->getAccessTokenByCode($code);
  23. } catch (\Exception $e) {
  24. throw new AmoCRMCannotGetTokenException($e->getMessage());
  25. }
  26.  
  27. $this->tokenStorage->setToken($accessToken);
  28. }
  29. public function getToken(): AccessTokenInterface
  30. {
  31. return $this->tokenStorage->getToken();
  32. }
  33.  
  34. public function getDomain(): string
  35. {
  36. return $this->domain;
  37. }
  38.  
  39. public function getClientId(): string
  40. {
  41. return $this->clientId;
  42. }
  43.  
  44. private function auth(): void
  45. {
  46. $accessToken = $this->tokenStorage->getToken();
  47. $tokenStorage = $this->tokenStorage;
  48.  
  49. $saveTokenCallback = static function (AccessTokenInterface $accessToken, string $baseDomain) use ($tokenStorage): void {
  50. $tokenStorage->setToken($accessToken);
  51. };
  52.  
  53. $this->apiClient
  54. ->setAccessToken($accessToken)
  55. ->setAccountBaseDomain($this->domain)
  56. ->onAccessTokenRefresh($saveTokenCallback);
  57. }
  58.  
  59. public function createLead(CRMUserDto $user): void
  60. {
  61. $this->auth();
  62. // TODO: Implement createLead() method.
  63. }
  64.  
  65. public function updateLead(CRMUserDto $user): void
  66. {
  67. $this->auth();
  68. // TODO: Implement updateLead() method.
  69. }
  70. }
  71.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement