Created
July 16, 2014 12:03
-
-
Save xphere/f7306f2e7aa90bbf0cbb to your computer and use it in GitHub Desktop.
Choose randomly a team mate from GitHub
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| class Github | |
| { | |
| private $token; | |
| public function __construct($token) | |
| { | |
| $this->token = $token; | |
| } | |
| public function getTeamMembers($teamId) | |
| { | |
| return $this->request("/teams/{$teamId}/members"); | |
| } | |
| private function request($url) | |
| { | |
| $ch = curl_init(); | |
| curl_setopt($ch, CURLOPT_URL, "https://api.github.com{$url}"); | |
| curl_setopt($ch, CURLOPT_HEADER, false); | |
| curl_setopt($ch, CURLOPT_USERAGENT, 'fake user-agent'); | |
| curl_setopt($ch, CURLOPT_USERPWD, "{$this->token}:x-oauth-basic"); | |
| curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); | |
| curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
| $output = curl_exec($ch); | |
| curl_close($ch); | |
| return json_decode($output); | |
| } | |
| } | |
| interface MemberRepository | |
| { | |
| public function getMembers(); | |
| } | |
| class GithubTeamMemberRepository implements MemberRepository | |
| { | |
| private $github; | |
| private $teamId; | |
| public function __construct(Github $github, $teamId) | |
| { | |
| $this->github = $github; | |
| $this->teamId = $teamId; | |
| } | |
| public function getMembers() | |
| { | |
| return $this->github->getTeamMembers($this->teamId); | |
| } | |
| } | |
| class MemoryCachedMemberRepositoryDecorator implements MemberRepository | |
| { | |
| /** @var \MemberRepository */ | |
| private $repository; | |
| private $members; | |
| public function __construct(MemberRepository $repository) | |
| { | |
| $this->repository = $repository; | |
| } | |
| public function getMembers() | |
| { | |
| if (null === $this->members) { | |
| $this->members = $this->repository->getMembers(); | |
| } | |
| return $this->members; | |
| } | |
| } | |
| interface MemberPicker | |
| { | |
| public function selectMember(); | |
| } | |
| abstract class AbstractPickerDecorator implements MemberPicker | |
| { | |
| protected $picker; | |
| public function __construct(MemberPicker $picker) | |
| { | |
| $this->picker = $picker; | |
| } | |
| } | |
| class MemberPickerNotMeDecorator extends AbstractPickerDecorator | |
| { | |
| private $me; | |
| public function __construct(MemberPicker $picker, $me) | |
| { | |
| parent::__construct($picker); | |
| $this->me = $me; | |
| } | |
| public function selectMember() | |
| { | |
| do { | |
| $member = $this->picker->selectMember(); | |
| } while ($this->me === $member->login); | |
| return $member; | |
| } | |
| } | |
| class RandomMemberPicker implements MemberPicker | |
| { | |
| private $repository; | |
| public function __construct(MemberRepository $repository) | |
| { | |
| $this->repository = $repository; | |
| } | |
| public function selectMember() | |
| { | |
| $members = $this->getMembers(); | |
| return $members[array_rand($members)]; | |
| } | |
| public function getMembers() | |
| { | |
| return $this->repository->getMembers(); | |
| } | |
| } | |
| function chooseTeammateFromGithub($teamId, $me, $token) | |
| { | |
| $github = new Github($token); | |
| $repository = new GithubTeamMemberRepository($github, $teamId); | |
| $repository = new MemoryCachedMemberRepositoryDecorator($repository); | |
| $picker = new RandomMemberPicker($repository); | |
| $picker = new MemberPickerNotMeDecorator($picker, $me); | |
| $member = $picker->selectMember(); | |
| return $member; | |
| } | |
| $me = 'xphere'; | |
| $teamId = '504741'; | |
| $token = 'my-security-token-from-github'; | |
| $member = chooseTeammateFromGithub($teamId, $me, $token); | |
| echo 'And the winner is… ', $member->login, PHP_EOL; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment