Created
August 3, 2020 14:52
-
-
Save ntraykov/18b9c2ffc7271487699c72f7d435e139 to your computer and use it in GitHub Desktop.
This file contains 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 | |
use Illuminate\Support\Facades\Http; | |
class GitHubScoreCalculator | |
{ | |
const ENDPOINT = 'https://api.github.com/users/%s/events/public'; | |
const DEFAULT_SCORE_KEY = 'default'; | |
private $points = [ | |
'PushEvent' => 10, | |
'PullRequestEvent' => 5, | |
'IssueCommentEvent' => 4, | |
self::DEFAULT_SCORE_KEY => 1, | |
]; | |
public function calculate($username) | |
{ | |
return $this->fetchEvents($username) | |
->pluck('type') | |
->countBy() | |
->map(function ($count, $event) { | |
return $this->calculateEventScore($count, $event); | |
}) | |
->sum(); | |
} | |
private function fetchEvents($username) | |
{ | |
$url = sprintf(self::ENDPOINT, $username); | |
return collect( | |
Http::get($url)->json() | |
); | |
} | |
private function calculateEventScore($count, $event) | |
{ | |
if (! array_key_exists($event, $this->points)) { | |
$event = self::DEFAULT_SCORE_KEY; | |
} | |
return $count * $this->points[$event]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment