PageRenderTime 55ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/classes/github/GithubCommit.php

https://bitbucket.org/delpho/tickhub
PHP | 129 lines | 85 code | 28 blank | 16 comment | 15 complexity | df1b47ce9b1113b1abfaf5b270d92d36 MD5 | raw file
  1. <?php
  2. /*
  3. * To change this template, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. /**
  7. * Description of GithubCommits
  8. *
  9. * @author jaraya
  10. */
  11. class GithubCommit {
  12. const TABLE = 'github_commit';
  13. private $id;
  14. private $repo_id;
  15. private $message;
  16. private $author_name;
  17. private $author_email;
  18. private $date;
  19. private $sha;
  20. private $url;
  21. /**
  22. *
  23. * @param int $userID
  24. * @param GithubRepository $repository
  25. * @return array
  26. */
  27. public static function getCommits ($userID, $repository) {
  28. $commits = array();
  29. $userID = Utils::enforcePositiveIntegerValue($userID);
  30. $user = new User($userID);
  31. $httpClient = new HttpClient();
  32. $httpClient->setHeaders(array("Authorization: token {$user->getGithubAccessToken()}"));
  33. foreach ( $repository->getBranches() as $branch ) {
  34. $response = $httpClient->doGetRequest("{$repository->getURL()}/commits/{$branch->getName()}");
  35. $jsonCommits = json_decode($response, true);
  36. foreach ( $jsonCommits as $jsonCommit ) {
  37. if ( is_array($jsonCommit) && isset($jsonCommit['author']) ) {
  38. //Log::getInstance()->log(print_r($jsonCommit['author'], true));
  39. $commit = new GithubCommit();
  40. $commit->repo_id = $repository->getRepositoryID();
  41. $commit->message = $jsonCommit['message'];
  42. $commit->author_name = $jsonCommit['author']['name'];
  43. $commit->author_email = $jsonCommit['author']['email'];
  44. $commit->date = $jsonCommit['author']['date'];
  45. $commit->sha = $jsonCommit['tree']['sha'];
  46. $commit->url = $jsonCommit['tree']['url'];
  47. array_push($commits, $commit);
  48. }
  49. }
  50. }
  51. return $commits;
  52. }
  53. public function save() {
  54. if ( $this->repo_id == null ) {
  55. throw new Exception('Invalid repository Id');
  56. }
  57. $this->checkID();
  58. $sql = ($this->id ? 'UPDATE ' : 'INSERT INTO ') . self::TABLE . ' SET ';
  59. $sql .= 'repo_id = :repo_id,
  60. message = :message,
  61. author_name = :author_name,
  62. author_email = :author_email,
  63. date = :date,
  64. sha = :sha,
  65. url = :url
  66. ';
  67. $params = array(
  68. ':repo_id' => $this->repo_id,
  69. ':message' => $this->message,
  70. ':author_name' => $this->author_name,
  71. ':author_email' => $this->author_email,
  72. ':date' => Utils::parseDate($this->date, DateTime::RFC3339),
  73. ':sha' => $this->sha,
  74. ':url' => $this->url
  75. );
  76. if ( $this->id ) {
  77. $sql .= ' WHERE id = :id ';
  78. $params[':id'] = $this->id;
  79. }
  80. $pdo = Database::getInstance()->getConnection();
  81. $stmt = $pdo->prepare($sql);
  82. $stmt->execute($params);
  83. $rows_affected = $stmt->rowCount();
  84. if ($this->id == null && $rows_affected == 1) {
  85. $this->id = $pdo->lastInsertId();
  86. }
  87. return ($rows_affected == 1 || $rows_affected == 0);
  88. }
  89. private function checkID () {
  90. $pdo = Database::getInstance()->getConnection();
  91. $sql = "SELECT id FROM " . self::TABLE . " WHERE sha = :sha ";
  92. $stmt = $pdo->prepare($sql);
  93. $sucess = $stmt->execute(array(':sha' => $this->sha));
  94. if ($sucess) {
  95. $row = $stmt->fetch(PDO::FETCH_ASSOC);
  96. if ($row && count($row) > 0) {
  97. $this->id = $row['id'];
  98. }
  99. }
  100. }
  101. }