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.1
  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 User $user
  25. */
  26. public function __construct(string $api_url, string $password, User $user) {
  27. parent::__construct($api_url, $password);
  28. if (!$user->email)
  29. $user->load();
  30. $this->basic($user->email);
  31. $this->owner = $user;
  32. }
  33. /**
  34. * @see Base
  35. */
  36. protected function set_scope(string $method) {
  37. switch ($method) {
  38. case "create":
  39. $this->scope = sprintf("/users/%s/tokens", $this->owner->username);
  40. return true;
  41. }
  42. return false;
  43. }
  44. /**
  45. * Create a new token
  46. *
  47. * Valid parameters:
  48. *
  49. * 1. name
  50. *
  51. * This reflects the API v1 documentation.
  52. *
  53. * @param ...$args The parameter values
  54. * @return bool
  55. */
  56. public function create(...$args) {
  57. $params = array(
  58. "name" => isset($args[0]) && is_string($args[0]) ? $args[0] : null
  59. );
  60. $params = array_filter($params, function($val) {
  61. return $val != null;
  62. });
  63. return parent::create($params);
  64. }
  65. }
  66. }