Skip to content

Commit

Permalink
PSR updates
Browse files Browse the repository at this point in the history
  • Loading branch information
joshhornby committed May 31, 2016
1 parent 1677040 commit ecc1eac
Show file tree
Hide file tree
Showing 36 changed files with 169 additions and 218 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Expand Up @@ -8,3 +8,5 @@ Homestead.json
.sass-cache
.DS_Store
.idea

*.cache
4 changes: 1 addition & 3 deletions app/Console/Commands/ColorTags.php
Expand Up @@ -3,7 +3,7 @@
namespace Astral\Console\Commands;

use Illuminate\Console\Command;
use \Colors\RandomColor;
use Colors\RandomColor;
use Astral\Models\Tag;

class ColorTags extends Command
Expand All @@ -24,8 +24,6 @@ class ColorTags extends Command

/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
Expand Down
3 changes: 1 addition & 2 deletions app/Console/Kernel.php
Expand Up @@ -19,8 +19,7 @@ class Kernel extends ConsoleKernel
/**
* Define the application's command schedule.
*
* @param \Illuminate\Console\Scheduling\Schedule $schedule
* @return void
* @param \Illuminate\Console\Scheduling\Schedule $schedule
*/
protected function schedule(Schedule $schedule)
{
Expand Down
19 changes: 10 additions & 9 deletions app/Exceptions/Handler.php
Expand Up @@ -28,26 +28,27 @@ class Handler extends ExceptionHandler
*
* This is a great spot to send exceptions to Sentry, Bugsnag, etc.
*
* @param \Exception $e
* @return void
* @param \Exception $e
*/
public function report(Exception $e)
{
parent::report($e);
}

/**
* Render an exception into an HTTP response.
*
* @param \Illuminate\Http\Request $request
* @param \Exception $e
* @return \Illuminate\Http\Response
*/
/**
* Render an exception into an HTTP response.
*
* @param \Illuminate\Http\Request $request
* @param \Exception $e
*
* @return \Illuminate\Http\Response
*/
public function render($request, Exception $e)
{
if ($e instanceof \Symfony\Component\HttpKernel\Exception\NotFoundHttpException) {
return response()->view('index');
}

return parent::render($request, $e);
}
}
17 changes: 10 additions & 7 deletions app/Helpers/GithubClient.php
Expand Up @@ -5,7 +5,6 @@
use Auth;
use Cache;
use GuzzleHttp\Client;
use JWTAuth;

class GithubClient
{
Expand All @@ -16,12 +15,12 @@ class GithubClient
private $starsPerPage = 50;

/**
* @param int $page
* @param string $token
* @param int $page
*
* @return array
*/
public function getStars($page = 1, $token)
public function getStars($token, $page = 1)
{
$cacheKey = $this->starsCacheKey();
$cacheExpiry = $this->starsCacheExpiry;
Expand All @@ -36,12 +35,13 @@ public function getStars($page = 1, $token)
$cachedStars['stars'] = $uniqueStars;
// Add a "cached" key so we can check on the front-end whether we should paginate or not. We set it to the number of pages currently cached, so we fetch only what we need in subsequent requests
$cachedPages = count($cachedStars['stars']);
$cachedStars['cached'] = (int)ceil($cachedPages / $this->starsPerPage);
$cachedStars['cached'] = (int) ceil($cachedPages / $this->starsPerPage);

return $cachedStars;
}
}

$starsUrl = 'https://api.github.com/user/starred?per_page=' . $this->starsPerPage . '&page=' . $page . '&access_token=' . $token;
$starsUrl = 'https://api.github.com/user/starred?per_page='.$this->starsPerPage.'&page='.$page.'&access_token='.$token;
$client = $this->getClient();
$res = $client->get(
$starsUrl,
Expand All @@ -53,6 +53,7 @@ public function getStars($page = 1, $token)
$pageCount = $res->hasHeader('link') ? $this->getTotalPages($res->getHeader('link')[0]) : 1;
$starsArray['page_count'] = $pageCount;
Cache::put($cacheKey, $starsArray, $cacheExpiry);

return $starsArray;
} else {
$cachedStars = Cache::get($cacheKey);
Expand All @@ -65,6 +66,7 @@ public function getStars($page = 1, $token)
$mergedStarsArray['stars'] = $uniqueStars;
$mergedStarsArray['page_count'] = $cachedStars['page_count'];
Cache::put($cacheKey, $mergedStarsArray, $cacheExpiry);

return $mergedStarsArray;
}
}
Expand All @@ -74,7 +76,7 @@ public function getStars($page = 1, $token)
*/
private function starsCacheKey()
{
return 'user_' . Auth::id() . '.github_stars';
return 'user_'.Auth::id().'.github_stars';
}

/**
Expand All @@ -91,7 +93,8 @@ private function getTotalPages($link)
$queryString = $urlParts['query'];
$qsArray = [];
parse_str($queryString, $qsArray);
return (int)$qsArray['page'];

return (int) $qsArray['page'];
} catch (Exception $e) {
return 1;
}
Expand Down
15 changes: 8 additions & 7 deletions app/Helpers/HTTPHeadersHelper.php
Expand Up @@ -11,23 +11,23 @@ class HTTPHeadersHelper
*/
public static function rels($h)
{
$h = 'Link: ' . $h;
$h = 'Link: '.$h;
$h = preg_replace("/(\r\n|\r)/", "\n", $h);
$h = explode("\n", preg_replace("/(\n)[ \t]+/", ' ', $h));
$rels = [];
foreach ($h as $f) {
if (!strncmp($f, 'X-Pingback: ', 12)) {
if (! strncmp($f, 'X-Pingback: ', 12)) {
// convert to a link header and have common code handle it
$f = 'Link: <' . trim(substr($f, 12)) . '>; rel="pingback"';
$f = 'Link: <'.trim(substr($f, 12)).'>; rel="pingback"';
}
if (!strncmp($f, 'Link: ', 6)) {
if (! strncmp($f, 'Link: ', 6)) {
$links = explode(', ', trim(substr($f, 6)));
foreach ($links as $link) {
$hrefandrel = explode('; ', $link);
$href = trim($hrefandrel[0], '<>');
$relarray = '';
foreach ($hrefandrel as $p) {
if (!strncmp($p, 'rel=', 4)) {
if (! strncmp($p, 'rel=', 4)) {
$relarray = explode(' ', trim(substr($p, 4), '"\''));
break;
}
Expand All @@ -36,10 +36,10 @@ public static function rels($h)
foreach ($relarray as $rel) {
$rel = strtolower(trim($rel));
if ($rel != '') {
if (!array_key_exists($rel, $rels)) {
if (! array_key_exists($rel, $rels)) {
$rels[$rel] = [];
}
if (!in_array($href, $rels[$rel])) {
if (! in_array($href, $rels[$rel])) {
$rels[$rel][] = $href;
}
}
Expand All @@ -48,6 +48,7 @@ public static function rels($h)
}
}
}

return $rels;
}
}
8 changes: 4 additions & 4 deletions app/Http/Controllers/AuthController.php
Expand Up @@ -2,15 +2,13 @@

namespace Astral\Http\Controllers;

use Astral\Http\Requests;
use Astral\Models\User;
use Auth;
use JWTAuth;
use Socialite;

class AuthController extends Controller
{

public function __construct()
{
$this->middleware('jwt.auth', ['only' => ['fetchUser']]);
Expand Down Expand Up @@ -38,7 +36,7 @@ public function handleProviderCallback()
$user = User::where('github_id', $id)->first();
$token = $githubUser->token;
// If the user exists, just update fields that they may have changed in their Github settings
if (!is_null($user)) {
if (! is_null($user)) {
$user->username = $githubUser->getNickname();
if ($githubUser->getName()) {
$user->name = $githubUser->getName();
Expand All @@ -57,7 +55,8 @@ public function handleProviderCallback()
$user->save();
}
$jwt = JWTAuth::fromUser($user);
return redirect('/auth?token=' . $jwt . '&access_token=' . $token);

return redirect('/auth?token='.$jwt.'&access_token='.$token);
}

/**
Expand All @@ -66,6 +65,7 @@ public function handleProviderCallback()
public function fetchUser()
{
$user = Auth::user();

return response()->json(compact('user'), 200);
}

Expand Down
9 changes: 4 additions & 5 deletions app/Http/Controllers/GithubController.php
Expand Up @@ -3,11 +3,9 @@
namespace Astral\Http\Controllers;

use Astral\Helpers\GithubClient;
use Astral\Http\Requests;
use Astral\Models\Star;
use Auth;
use Illuminate\Http\Request;
use JWTAuth;

class GithubController extends Controller
{
Expand All @@ -24,10 +22,10 @@ public function __construct()
*/
public function getStars(Request $request, GithubClient $client)
{
$page = (int)$request->input('page', 1);
$page = (int) $request->input('page', 1);
$access_token = $request->header('Access-Token');
$stars = $client->getStars($page, $access_token);
for ($i = 0; $i <= count($stars['stars']) - 1; $i++) {
$stars = $client->getStars($access_token, $page);
for ($i = 0; $i <= count($stars['stars']) - 1; ++$i) {
$userStar = Star::with('tags')->where('user_id', Auth::id())->where(
'repo_id', $stars['stars'][$i]['id']
)->first();
Expand All @@ -37,6 +35,7 @@ public function getStars(Request $request, GithubClient $client)
$stars['stars'][$i]['tags'] = [];
}
}

return response()->json(compact('stars'), 200);
}
}
17 changes: 8 additions & 9 deletions app/Http/Controllers/StarController.php
Expand Up @@ -3,14 +3,9 @@
namespace Astral\Http\Controllers;

use Illuminate\Http\Request;
use GuzzleHttp\ClientInterface;
use Astral\Http\Requests;
use Astral\Http\Controllers\Controller;
use Astral\Models\Star;
use Astral\Models\Tag;
use Auth;
use JWTAuth;
use Tymon\JWTAuth\Exceptions\JWTException;

class StarController extends Controller
{
Expand All @@ -25,6 +20,7 @@ public function __construct()
public function index()
{
$stars = Star::with('tags')->where('user_id', Auth::id())->get();

return response()->json(compact('stars'), 200);
}

Expand All @@ -39,7 +35,7 @@ public function tag(Request $request)
$star_name = $request->input('repoName');
$tag_id = $request->input('tagId');
$star = Star::where('repo_id', $star_id)->where('user_id', Auth::id())->first();
if (!is_null($star)) {
if (! is_null($star)) {
$star->tags()->sync([$tag_id], false);
$star->save();
} else {
Expand All @@ -51,6 +47,7 @@ public function tag(Request $request)
}
$stars = Star::with('tags')->where('user_id', Auth::id())->get();
$tags = Tag::with('stars')->where('user_id', Auth::user()->id)->orderBy('sort_order', 'asc')->get();

return response()->json(compact('stars', 'tags'), 200);
}

Expand All @@ -64,7 +61,7 @@ public function syncTags(Request $request)
$repo = $request->input('star');
$tags = $request->input('tags');
$star = Star::where('repo_id', $repo['id'])->where('user_id', Auth::id())->first();
if (!$star) {
if (! $star) {
$star = new Star();
$star->repo_id = $repo['id'];
$star->repo_name = $repo['full_name'];
Expand All @@ -77,7 +74,7 @@ public function syncTags(Request $request)
foreach ($tags as $tag) {
$tagName = strtolower($tag['name']);
$userTag = Tag::where('name', $tagName)->where('user_id', Auth::id())->first();
if (!$userTag) {
if (! $userTag) {
$userTag = new Tag();
$userTag->name = $tag['name'];
$userTag->save();
Expand All @@ -87,6 +84,7 @@ public function syncTags(Request $request)
}
}
$stars = Star::with('tags')->where('user_id', Auth::id())->get();

return response()->json(compact('stars'), 200);
}

Expand All @@ -100,7 +98,7 @@ public function editNotes(Request $request)
$repo = $request->input('star');
$text = $request->input('text');
$star = Star::where('repo_id', $repo['id'])->where('user_id', Auth::id())->first();
if (!$star) {
if (! $star) {
$star = new Star();
$star->repo_id = $repo['id'];
$star->repo_name = $repo['full_name'];
Expand All @@ -109,6 +107,7 @@ public function editNotes(Request $request)
$star->notes = $text;
$star->save();
$stars = Star::with('tags')->where('user_id', Auth::id())->get();

return response()->json(compact('stars'), 200);
}
}

0 comments on commit ecc1eac

Please sign in to comment.