Skip to content

Commit

Permalink
psr-2, replace split with explode
Browse files Browse the repository at this point in the history
  • Loading branch information
vmitchell85 committed Sep 27, 2017
1 parent 2ffe01c commit 20b55a8
Show file tree
Hide file tree
Showing 10 changed files with 44 additions and 82 deletions.
10 changes: 4 additions & 6 deletions src/alforge/Deploy.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,17 @@

namespace AlForge;

use AlForge\Forge;
use Alfred\Workflows\Workflow;

class Deploy extends Forge
{
public function search($query)
{

$workflow = new Workflow;

foreach ($this->data->servers as $server) {
foreach ($server->sites as $site) {
if( strpos($site->name, $query) > -1 || strpos($server->name, $query) > -1 ){
if (strpos($site->name, $query) > -1 || strpos($server->name, $query) > -1) {
$workflow->result()
->uid($site->id)
->title('Site: ' . $site->name)
Expand All @@ -31,12 +29,12 @@ public function search($query)

public function execute($command)
{
$cmdParts = split(' ', $command);
$cmdParts = explode(' ', $command);

$server = $this->getServerInfo($cmdParts[0]);
$site = $this->getSiteInfo($cmdParts[0], $cmdParts[1]);

if($this->confirm("Are you sure you want to deploy the site `$site->name` on `$server->name`?")){
if ($this->confirm("Are you sure you want to deploy the site `$site->name` on `$server->name`?")) {
$response = $this->apiRequest("https://forge.laravel.com/api/v1/servers/$cmdParts[0]/sites/$cmdParts[1]/deployment/deploy");

$data = json_decode($response);
Expand All @@ -47,4 +45,4 @@ public function execute($command)
);
}
}
}
}
9 changes: 2 additions & 7 deletions src/alforge/Env.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@

namespace AlForge;

use AlForge\Forge;
use Alfred\Workflows\Workflow;

class Env extends Forge
{
public function search($query)
Expand All @@ -14,15 +11,13 @@ public function search($query)

public function execute($command)
{

$cmdParts = split(' ', $command);
$cmdParts = explode(' ', $command);

$server = $this->getServerInfo($cmdParts[0]);
$site = $this->getSiteInfo($cmdParts[0], $cmdParts[1]);

$response = $this->apiRequest("https://forge.laravel.com/api/v1/servers/$cmdParts[0]/sites/$cmdParts[1]/env", "GET");

$this->respond($response);

}
}
}
52 changes: 24 additions & 28 deletions src/alforge/Forge.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ public function __construct()
$this->dataCache = getenv('alfred_workflow_cache') . '/data.json';
$this->authCache = getenv('alfred_workflow_cache') . '/auth.txt';

if( !file_exists($this->cacheDir) ){
if (!file_exists($this->cacheDir)) {
mkdir($this->cacheDir);
}

if( !file_exists($this->dataCache) ){
if (!file_exists($this->dataCache)) {
file_put_contents($this->dataCache, json_encode(["servers" => []]));
}

if( !file_exists($this->authCache) ){
if (!file_exists($this->authCache)) {
file_put_contents($this->authCache, '');
}

Expand All @@ -46,8 +46,8 @@ public function loadCache()

$serverCount = 0;

if( $data->servers ){
foreach( $data->servers as $server ){
if ($data->servers) {
foreach ($data->servers as $server) {
$serverCount++;
$sitesResponse = $this->apiRequest('https://forge.laravel.com/api/v1/servers/'.$server->id.'/sites/', 'GET');
$sitesData = json_decode($sitesResponse);
Expand All @@ -66,16 +66,16 @@ public function apiRequest($url, $method = 'POST', $data = '')
$authorization = "Authorization: Bearer " . $this->token;

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json' , $authorization ));
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json' , $authorization ]);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS,$data);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$result = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

if($this->requestHasError($result, $httpcode)){
if ($this->requestHasError($result, $httpcode)) {
die();
}

Expand All @@ -84,7 +84,7 @@ public function apiRequest($url, $method = 'POST', $data = '')

public function requestHasError($result, $code)
{
if($code >= 400 || strpos($result, '<html') > -1){
if ($code >= 400 || strpos($result, '<html') > -1) {
$errorMap = [
400 => "Valid data was given but the request has failed.",
401 => "No valid API Key was given.",
Expand All @@ -95,17 +95,16 @@ public function requestHasError($result, $code)
503 => "Forge is offline for maintenance."
];

if($errorMap[$code]){
if ($errorMap[$code]) {
$this->emitError($errorMap[$code]);
}
else{
} else {
$this->emitError("An unknown error occured, maybe try re-setting your API Key in Alfred");
}

return True;
return true;
}

return False;
return false;
}

public function setKey($key)
Expand Down Expand Up @@ -144,7 +143,7 @@ public function allSearch($query)
$workflow = new Workflow;

foreach ($this->data->servers as $server) {
if( strpos($server->name, $query) > -1){
if (strpos($server->name, $query) > -1) {
$workflow->result()
->uid($server->id)
->title('Server: ' . $server->name)
Expand All @@ -154,7 +153,7 @@ public function allSearch($query)
}

foreach ($server->sites as $site) {
if( strpos($site->name, $query) > -1 || strpos($server->name, $query) > -1 ){
if (strpos($site->name, $query) > -1 || strpos($server->name, $query) > -1) {
$workflow->result()
->uid($site->id)
->title('Site: ' . $site->name)
Expand All @@ -172,12 +171,11 @@ public function allSearch($query)

public function siteSearch($query)
{

$workflow = new Workflow;

foreach ($this->data->servers as $server) {
foreach ($server->sites as $site) {
if( strpos($site->name, $query) > -1 || strpos($server->name, $query) > -1 ){
if (strpos($site->name, $query) > -1 || strpos($server->name, $query) > -1) {
$workflow->result()
->uid($site->id)
->title('Site: ' . $site->name)
Expand All @@ -193,11 +191,10 @@ public function siteSearch($query)

public function serverSearch($query)
{

$workflow = new Workflow;

foreach ($this->data->servers as $server) {
if( strpos($server->name, $query) > -1){
if (strpos($server->name, $query) > -1) {
$workflow->result()
->uid($server->id)
->title('Server: ' . $server->name)
Expand All @@ -214,12 +211,12 @@ public function confirm($text = "Are you sure?")
{
$response = exec("echo $(osascript -e 'display dialog \"$text\" buttons {\"Cancel\",\"Confirm\"} default button 2 with title \"Confirm alForge Command\" with icon file \"System:Library:CoreServices:CoreTypes.bundle:Contents:Resources:AlertStopIcon.icns\"')");

if(!$response){
if (!$response) {
$this->respond('Action Cancelled');
return False;
return false;
}

return True;
return true;
}

public function emitError($text)
Expand All @@ -230,7 +227,7 @@ public function emitError($text)
public function getServerInfo($server_id)
{
foreach ($this->data->servers as $server) {
if( $server->id == $server_id ){
if ($server->id == $server_id) {
return $server;
}
}
Expand All @@ -239,14 +236,13 @@ public function getServerInfo($server_id)
public function getSiteInfo($server_id, $site_id)
{
foreach ($this->data->servers as $server) {
if( $server->id == $server_id ){
if ($server->id == $server_id) {
foreach ($server->sites as $site) {
if($site->id == $site_id){
if ($site->id == $site_id) {
return $site;
}
}
}
}
}

}
}
6 changes: 1 addition & 5 deletions src/alforge/IpAddress.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@

namespace AlForge;

use AlForge\Forge;
use Alfred\Workflows\Workflow;

class IpAddress extends Forge
{
public function search($query)
Expand All @@ -17,6 +14,5 @@ public function execute($server_id)
$server = $this->getServerInfo($server_id);

$this->respond($server->ip_address);

}
}
}
8 changes: 2 additions & 6 deletions src/alforge/Mysql.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@

namespace AlForge;

use AlForge\Forge;
use Alfred\Workflows\Workflow;

class Mysql extends Forge
{
public function search($query)
Expand All @@ -16,7 +13,7 @@ public function execute($server_id)
{
$server = $this->getServerInfo($server_id);

if($this->confirm("Are you sure you want to restart MYSQL on `$server->name`?")){
if ($this->confirm("Are you sure you want to restart MYSQL on `$server->name`?")) {
$response = $this->apiRequest("https://forge.laravel.com/api/v1/servers/$server->id/mysql/reboot");

$data = json_decode($response);
Expand All @@ -26,6 +23,5 @@ public function execute($server_id)
["push_title" => "Restarting MYSQL on `$server->name`"]
);
}

}
}
}
8 changes: 2 additions & 6 deletions src/alforge/Nginx.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@

namespace AlForge;

use AlForge\Forge;
use Alfred\Workflows\Workflow;

class Nginx extends Forge
{
public function search($query)
Expand All @@ -16,7 +13,7 @@ public function execute($server_id)
{
$server = $this->getServerInfo($server_id);

if($this->confirm("Are you sure you want to restart Nginx on `$server->name`?")){
if ($this->confirm("Are you sure you want to restart Nginx on `$server->name`?")) {
$response = $this->apiRequest("https://forge.laravel.com/api/v1/servers/$server->id/nginx/reboot");

$data = json_decode($response);
Expand All @@ -26,6 +23,5 @@ public function execute($server_id)
["push_title" => "Restarting Nginx on `$server->name`"]
);
}

}
}
}
12 changes: 4 additions & 8 deletions src/alforge/Open.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@

namespace AlForge;

use AlForge\Forge;
use Alfred\Workflows\Workflow;

class Open extends Forge
{
public function search($query)
Expand All @@ -14,13 +11,12 @@ public function search($query)

public function execute($command)
{
$cmdParts = split(' ', $command);
$cmdParts = explode(' ', $command);

if( $cmdParts[1] ){
if ($cmdParts[1]) {
$this->respond("https://forge.laravel.com/servers/$cmdParts[0]/sites/$cmdParts[1]");
}
else{
} else {
$this->respond("https://forge.laravel.com/servers/$cmdParts[0]");
}
}
}
}
8 changes: 2 additions & 6 deletions src/alforge/Postgres.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@

namespace AlForge;

use AlForge\Forge;
use Alfred\Workflows\Workflow;

class Postgres extends Forge
{
public function search($query)
Expand All @@ -16,7 +13,7 @@ public function execute($server_id)
{
$server = $this->getServerInfo($server_id);

if($this->confirm("Are you sure you want to restart Postgres on `$server->name`?")){
if ($this->confirm("Are you sure you want to restart Postgres on `$server->name`?")) {
$response = $this->apiRequest("https://forge.laravel.com/api/v1/servers/$server->id/postgres/reboot");

$data = json_decode($response);
Expand All @@ -26,6 +23,5 @@ public function execute($server_id)
["push_title" => "Restarting Postgres on `$server->name`"]
);
}

}
}
}
Loading

0 comments on commit 20b55a8

Please sign in to comment.