Skip to content

docs: Refactoring and min PHP version bumped to 8 #170

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Make sure your request is meaningful and you have tested the app locally before

#### Requirements

* [PHP 7.4+](https://www.apachefriends.org/index.html)
* [PHP 8.0+](https://www.apachefriends.org/index.html)
* [Composer](https://getcomposer.org)
* [Imagick](https://www.php.net/imagick)

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ Make sure your request is meaningful and you have tested the app locally before

#### Requirements

- [PHP 7.4+](https://www.apachefriends.org/index.html)
- [PHP 8.0+](https://www.apachefriends.org/index.html)
- [Composer](https://getcomposer.org)
- [Imagick](https://www.php.net/imagick)

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
]
},
"require": {
"php": "^7.4|^8.0",
"php": "^8.0",
"ext-imagick": "*",
"vlucas/phpdotenv": "^5.3"
},
Expand Down
4 changes: 2 additions & 2 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

100 changes: 39 additions & 61 deletions src/card.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,47 +2,6 @@

declare(strict_types=1);

/**
* Set headers and echo response based on type
*
* @param string|array $output
* @return void
*/
function renderOutput(string|array $output): void
{
$requestedType = $_REQUEST['type'] ?? 'svg';

$card = gettype($output) === "string" ? generateErrorCard($output) : generateCard($output);

if ($requestedType === "json") {
$data = gettype($output) === "string" ? array("error" => $output) : $output;
echoAsJson($data);
exit;
}

if ($requestedType === "png") {
echoAsPng($card);
exit;
}

echoAsSvg($card);
exit;
}

/**
* Displays an array as JSON
*
* @param array $data The data array to output
*/
function echoAsJson(array $data): void
{
// set content type to JSON
header('Content-Type: application/json');
// echo JSON data
echo json_encode($data);
}


/**
* Convert date from Y-M-D to more human-readable format
*
Expand Down Expand Up @@ -334,27 +293,15 @@ function generateErrorCard(string $message, array $params = null): string
}

/**
* Displays a card as an SVG image
* Converts an SVG card to a PNG image
*
* @param string $svg The SVG for the card to display
*/
function echoAsSvg(string $svg): void
{
// set content type to SVG image
header("Content-Type: image/svg+xml");

// echo SVG data for streak stats
echo $svg;
}

/**
* Displays a card as a PNG image
*
* @param string $svg The SVG for the card to display
* @param string $svg The SVG for the card as a string
*
* @return string The generated PNG data
*
* @throws ImagickException
*/
function echoAsPng(string $svg): void
function convertSvgToPng(string $svg): string
{
// trim off all whitespaces to make it a valid SVG string
$svg = trim($svg);
Expand All @@ -374,11 +321,42 @@ function echoAsPng(string $svg): void
$imagick->readImageBlob('<?xml version="1.0" encoding="UTF-8" standalone="no"?>' . $svg);
$imagick->setFormat('png');

// echo PNG data
header('Content-Type: image/png');
echo $imagick->getImageBlob();
// get PNG data
$png = $imagick->getImageBlob();

// clean up memory
$imagick->clear();
$imagick->destroy();

return $png;
}

/**
* Set headers and echo response based on type
*
* @param string|array $output The stats (array) or error message (string) to display
*/
function renderOutput(string|array $output): void
{
$requestedType = $_REQUEST['type'] ?? 'svg';

// output JSON data
if ($requestedType === "json") {
// set content type to JSON
header('Content-Type: application/json');
// generate array from output
$data = gettype($output) === "string" ? array("error" => $output) : $output;
// output as JSON
echo json_encode($data);
}
// output SVG or PNG card
else {
// set content type to SVG or PNG
header("Content-Type: image/" . ($requestedType === "png" ? "png" : "svg+xml"));
// render SVG card
$svg = gettype($output) === "string" ? generateErrorCard($output) : generateCard($output);
// output PNG if PNG is requested, otherwise output SVG
echo $requestedType === "png" ? convertSvgToPng($svg) : $svg;
}
exit;
}
8 changes: 5 additions & 3 deletions src/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,12 @@
// get streak stats for user given in query string
$contributionGraphs = getContributionGraphs($_REQUEST["user"]);
$contributions = getContributionDates($contributionGraphs);
// if no contributions, display error
if (count($contributions) === 0) {
throw new AssertionError("No contributions found.");
}
$stats = getContributionStats($contributions);
renderOutput($stats);
} catch (InvalidArgumentException $error) {
} catch (InvalidArgumentException|AssertionError $error) {
renderOutput($error->getMessage());
}


17 changes: 8 additions & 9 deletions src/stats.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,15 +110,14 @@ function getGitHubApiResponse(string $url): string
curl_setopt($ch, CURLOPT_VERBOSE, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
$response = curl_exec($ch);

//Handles curl errors
if($response === false) {
if(str_contains(curl_error($ch), 'unable to get local issuer certificate')) {
throw new InvalidArgumentException("You don't have valid SSL Certificate installed or XAMPP.");
// handle curl errors
if ($response === false) {
if (str_contains(curl_error($ch), 'unable to get local issuer certificate')) {
throw new InvalidArgumentException("You don't have a valid SSL Certificate installed or XAMPP.");
}
throw new InvalidArgumentException("Something is wrong with getGitHubApiResponse().");
throw new InvalidArgumentException("An error occurred when getting a response from GitHub.");
}

// close curl handle and return response
curl_close($ch);
return $response;
}
Expand Down Expand Up @@ -163,8 +162,8 @@ function getYearJoined(string $user): int
*/
function getContributionStats(array $contributions): array
{
$today = array_key_last($contributions) ?? date("Y-m-d");
$first = array_key_first($contributions) ?? date("Y-m-d");
$today = array_key_last($contributions);
$first = array_key_first($contributions);
$stats = [
"totalContributions" => 0,
"firstContribution" => "",
Expand Down
22 changes: 11 additions & 11 deletions src/themes.php
Original file line number Diff line number Diff line change
Expand Up @@ -783,17 +783,17 @@
"dates" => "#000000",
],
"Javascript-dark" => [
"background" => "#000000FF",
"border" => "#F7DF1EFF",
"stroke" => "#F7DF1EFF",
"ring" => "#F7DF1EFF",
"currStreakNum" => "#FFFFFFFF",
"fire" => "#F7DF1EFF",
"sideNums" => "#FFFFFFFF",
"currStreakLabel" => "#F7DF1EFF",
"sideLabels" => "#F7DF1EFF",
"dates" => "#F7DF1EFF",
],
"background" => "#000000FF",
"border" => "#F7DF1EFF",
"stroke" => "#F7DF1EFF",
"ring" => "#F7DF1EFF",
"currStreakNum" => "#FFFFFFFF",
"fire" => "#F7DF1EFF",
"sideNums" => "#FFFFFFFF",
"currStreakLabel" => "#F7DF1EFF",
"sideLabels" => "#F7DF1EFF",
"dates" => "#F7DF1EFF",
],
"noctis-minimus" => [
"background" => "#1b2932",
"border" => "#F0F0F0FF",
Expand Down