AuthRequest.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. /**
  3. * Refer to LICENSE.txt distributed with the Temando Shipping module for notice of license
  4. */
  5. namespace Temando\Shipping\Rest\Request;
  6. /**
  7. * Temando API Session Operation
  8. *
  9. * @package Temando\Shipping\Rest
  10. * @author Christoph Aßmann <christoph.assmann@netresearch.de>
  11. * @author Sebastian Ertner <sebastian.ertner@netresearch.de>
  12. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  13. * @link http://www.temando.com/
  14. */
  15. class AuthRequest implements AuthRequestInterface
  16. {
  17. /**
  18. * @var string
  19. */
  20. private $scope;
  21. /**
  22. * @var string
  23. */
  24. private $accountId;
  25. /**
  26. * @var string
  27. */
  28. private $bearerToken;
  29. /**
  30. * @var string
  31. */
  32. private $username;
  33. /**
  34. * @var string
  35. */
  36. private $password;
  37. /**
  38. * AuthRequest constructor.
  39. * @param string $scope
  40. * @param string $accountId
  41. * @param string $bearerToken
  42. * @param string $username
  43. * @param string $password
  44. */
  45. public function __construct($scope, $accountId = '', $bearerToken = '', $username = '', $password = '')
  46. {
  47. $this->scope = $scope;
  48. $this->accountId = $accountId;
  49. $this->bearerToken = $bearerToken;
  50. $this->username = $username;
  51. $this->password = $password;
  52. }
  53. /**
  54. * @return string
  55. */
  56. public function getRequestBody()
  57. {
  58. $attributes = [
  59. 'scope' => $this->scope,
  60. ];
  61. if ($this->username && $this->password) {
  62. $attributes['email'] = $this->username;
  63. $attributes['password'] = $this->password;
  64. } else {
  65. $attributes['accountId'] = $this->accountId;
  66. $attributes['bearerToken'] = $this->bearerToken;
  67. }
  68. $params = [
  69. 'data' => [
  70. 'type' => 'session',
  71. 'attributes' => $attributes,
  72. ],
  73. ];
  74. return json_encode($params);
  75. }
  76. }