Token.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. namespace Gogs\API\Request {
  3. /**
  4. * A token related to a user
  5. *
  6. * Supports:
  7. * * POST `/users/{username}/tokens`
  8. *
  9. * Note! Tokens doesnt have a "GET" method. @see Tokens
  10. * as this can load them.
  11. *
  12. * @author Joachim M. Giaever (joachim[]giaever.org)
  13. * @version 0.1.2
  14. */
  15. final class Token extends Base {
  16. const VERSION = "0.1.1";
  17. protected $owner;
  18. public $token_name;
  19. public $token_sha1;
  20. /**
  21. * Initializes a token
  22. *
  23. * @see Base
  24. * @param string $api_url The API URL
  25. * @param string $password The users personal password
  26. * @param User $user
  27. */
  28. public function __construct(string $api_url, string $password, User $user) {
  29. parent::__construct($api_url, $password);
  30. if (!$user->email)
  31. $user->load();
  32. $this->basic($user->email);
  33. $this->owner = $user;
  34. }
  35. /**
  36. * @see Base
  37. */
  38. protected function set_scope(string $method) {
  39. switch ($method) {
  40. case "create":
  41. $this->scope = sprintf("/users/%s/tokens", $this->owner->username);
  42. return true;
  43. }
  44. return false;
  45. }
  46. /**
  47. * Create a new token
  48. *
  49. * Valid parameters:
  50. *
  51. * 1. name
  52. *
  53. * This reflects the API v1 documentation.
  54. *
  55. * @param ...$args The parameter values
  56. * @return Token
  57. */
  58. public function create(...$args) {
  59. $params = array();
  60. $this->set_param($params, "name", $args, 0, "string", null);
  61. $this->filter_params($params);
  62. return parent::create($params);
  63. }
  64. }
  65. }