Skip to content

Commit

Permalink
updated site request
Browse files Browse the repository at this point in the history
  • Loading branch information
denisdulici committed Dec 4, 2019
1 parent fe2ed7b commit b8018dd
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 139 deletions.
8 changes: 4 additions & 4 deletions app/Events/Install/UpdateFinished.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,21 @@ class UpdateFinished

public $alias;

public $old;

public $new;

public $old;

/**
* Create a new event instance.
*
* @param $alias
* @param $old
* @param $new
*/
public function __construct($alias, $old, $new)
public function __construct($alias, $new, $old)
{
$this->alias = $alias;
$this->old = $old;
$this->new = $new;
$this->old = $old;
}
}
157 changes: 58 additions & 99 deletions app/Traits/Modules.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,16 @@

namespace App\Traits;

use App\Models\Module\Module as Model;
use App\Models\Module\Module;
use App\Traits\SiteApi;
use App\Utilities\Console;
use App\Utilities\Info;
use App\Models\Module\Module as Model;
use App\Models\Module\Module;
use Artisan;
use Cache;
use Date;
use File;
use Illuminate\Support\Str;
use GuzzleHttp\Exception\RequestException;
use ZipArchive;

trait Modules
Expand All @@ -27,15 +26,13 @@ public function checkToken($apiKey)
]
];

$response = static::getRemote('token/check', 'POST', $data);

if ($response && ($response->getStatusCode() == 200)) {
$result = json_decode($response->getBody());

return ($result->success) ? true : false;
if (!$response = static::getResponse('POST', 'token/check', $data)) {
return false;
}

return false;
$result = json_decode($response->getBody());

return $result->success ? true : false;
}

// Get All Modules
Expand All @@ -48,7 +45,7 @@ public function getModules()
return $items;
}

$items = $this->remote('apps/items');
$items = static::getResponseData('GET', 'apps/items');

Cache::put('apps.items', $items, Date::now()->addHour());

Expand All @@ -65,7 +62,7 @@ public function getModule($alias)
return $item;
}

$item = $this->remote('apps/' . $alias);
$item = static::getResponseData('GET', 'apps/' . $alias);

Cache::put('apps.' . $alias, $item, Date::now()->addHour());

Expand All @@ -81,7 +78,7 @@ public function getDocumentation($alias)
return $documentation;
}

$documentation = $this->remote('apps/docs/' . $alias);
$documentation = static::getResponseData('GET', 'apps/docs/' . $alias);

Cache::put('apps.docs.' . $alias, $documentation, Date::now()->addHour());

Expand All @@ -97,7 +94,7 @@ public function getModuleReviews($alias, $data = [])
return $reviews;
}

$reviews = $this->remote('apps/' . $alias . '/reviews', 'GET', $data);
$reviews = static::getResponseData('GET', 'apps/' . $alias . '/reviews', $data);

Cache::put('apps.' . $alias . '.reviews', $reviews, Date::now()->addHour());

Expand All @@ -113,7 +110,7 @@ public function getCategories()
return $categories;
}

$categories = $this->remote('apps/categories');
$categories = static::getResponseData('GET', 'apps/categories');

Cache::put('apps.categories', $categories, Date::now()->addHour());

Expand All @@ -129,7 +126,7 @@ public function getModulesByCategory($alias, $data = [])
return $category;
}

$category = $this->remote('apps/categories/' . $alias, 'GET', $data);
$category = static::getResponseData('GET', 'apps/categories/' . $alias, $data);

Cache::put('apps.categories.' . $alias, $category, Date::now()->addHour());

Expand All @@ -145,7 +142,7 @@ public function getVendors()
return $vendors;
}

$vendors = $this->remote('apps/vendors');
$vendors = static::getResponseData('GET', 'apps/vendors');

Cache::put('apps.vendors', $vendors, Date::now()->addHour());

Expand All @@ -161,7 +158,7 @@ public function getModulesByVendor($alias, $data = [])
return $vendor;
}

$vendor = $this->remote('apps/vendors/' . $alias, 'GET', $data);
$vendor = static::getResponseData('GET', 'apps/vendors/' . $alias, $data);

Cache::put('apps.vendors.' . $alias, $vendor, Date::now()->addHour());

Expand All @@ -170,7 +167,7 @@ public function getModulesByVendor($alias, $data = [])

public function getMyModules($data = [])
{
return $this->remote('apps/my', 'GET', $data);
return static::getResponseData('GET', 'apps/my', $data);
}

public function getInstalledModules($data = [])
Expand Down Expand Up @@ -216,7 +213,7 @@ public function getPreSaleModules($data = [])
return $pre_sale;
}

$pre_sale = $this->remote('apps/pre_sale', 'GET', $data);
$pre_sale = static::getResponseData('GET', 'apps/pre_sale', $data);

Cache::put('apps.pre_sale', $pre_sale, Date::now()->addHour());

Expand All @@ -232,7 +229,7 @@ public function getPaidModules($data = [])
return $paid;
}

$paid = $this->remote('apps/paid', 'GET', $data);
$paid = static::getResponseData('GET', 'apps/paid', $data);

Cache::put('apps.paid', $paid, Date::now()->addHour());

Expand All @@ -248,7 +245,7 @@ public function getNewModules($data = [])
return $new;
}

$new = $this->remote('apps/new', 'GET', $data);
$new = static::getResponseData('GET', 'apps/new', $data);

Cache::put('apps.new', $new, Date::now()->addHour());

Expand All @@ -264,7 +261,7 @@ public function getFreeModules($data = [])
return $free;
}

$free = $this->remote('apps/free', 'GET', $data);
$free = static::getResponseData('GET', 'apps/free', $data);

Cache::put('apps.free', $free, Date::now()->addHour());

Expand All @@ -280,7 +277,7 @@ public function getFeaturedModules($data = [])
return $featured;
}

$featured = $this->remote('apps/featured', 'GET', $data);
$featured = static::getResponseData('GET', 'apps/featured', $data);

Cache::put('apps.featured', $featured, Date::now()->addHour());

Expand All @@ -289,63 +286,62 @@ public function getFeaturedModules($data = [])

public function getSearchModules($data = [])
{
return $this->remote('apps/search', 'GET', $data);
return static::getResponseData('GET', 'apps/search', $data);
}

public function getCoreVersion()
{
$data['query'] = Info::all();

$response = static::getRemote('core/version', 'GET', $data);

if ($response && ($response->getStatusCode() == 200)) {
return $response->json();
if (!$response = static::getResponse('GET', 'core/version', $data)) {
return [];
}

return [];
return $response->json();
}

public function downloadModule($path)
{
$response = static::getRemote($path);

if ($response && ($response->getStatusCode() == 200)) {
$file = $response->getBody()->getContents();

$path = 'temp-' . md5(mt_rand());
$temp_path = storage_path('app/temp') . '/' . $path;
if (!$response = static::getResponse('GET', $path)) {
return [
'success' => false,
'error' => true,
'message' => null,
'data' => null,
];
}

$file_path = $temp_path . '/upload.zip';
$file = $response->getBody()->getContents();

// Create tmp directory
if (!File::isDirectory($temp_path)) {
File::makeDirectory($temp_path);
}
$path = 'temp-' . md5(mt_rand());
$temp_path = storage_path('app/temp') . '/' . $path;

// Add content to the Zip file
$uploaded = is_int(file_put_contents($file_path, $file)) ? true : false;
$file_path = $temp_path . '/upload.zip';

if (!$uploaded) {
return false;
}
// Create tmp directory
if (!File::isDirectory($temp_path)) {
File::makeDirectory($temp_path);
}

$data = [
'path' => $path
];
// Add content to the Zip file
$uploaded = is_int(file_put_contents($file_path, $file)) ? true : false;

if (!$uploaded) {
return [
'success' => true,
'error' => false,
'success' => false,
'error' => true,
'message' => null,
'data' => $data,
'data' => null,
];
}

return [
'success' => false,
'error' => true,
'success' => true,
'error' => false,
'message' => null,
'data' => null,
'data' => [
'path' => $path,
],
];
}

Expand Down Expand Up @@ -518,26 +514,12 @@ public function loadSuggestions()

$data = [];

$url = 'apps/suggestions';

$response = static::getRemote($url, 'GET', ['timeout' => 30, 'referer' => true]);

// Exception
if ($response instanceof RequestException) {
return false;
}

// Bad response
if (!$response || ($response->getStatusCode() != 200)) {
return false;
if (!$suggestions = static::getResponseData('GET', 'apps/suggestions')) {
return $data;
}

$suggestions = json_decode($response->getBody())->data;

if ($suggestions) {
foreach ($suggestions as $suggestion) {
$data[$suggestion->path] = $suggestion;
}
foreach ($suggestions as $suggestion) {
$data[$suggestion->path] = $suggestion;
}

Cache::put('suggestions', $data, Date::now()->addHour(6));
Expand All @@ -556,22 +538,10 @@ public function loadNotifications()

$data = [];

$url = 'apps/notifications';

$response = static::getRemote($url, 'GET', ['timeout' => 30, 'referer' => true]);

// Exception
if ($response instanceof RequestException) {
return false;
}

// Bad response
if (!$response || ($response->getStatusCode() != 200)) {
return false;
if (!$notifications = static::getResponse('GET', 'apps/notifications')) {
return $data;
}

$notifications = json_decode($response->getBody())->data;

foreach ($notifications as $notification) {
$data[$notification->path][] = $notification;
}
Expand Down Expand Up @@ -612,15 +582,4 @@ public function getNotifications($path)

return false;
}

protected function remote($path, $method = 'GET', $data = [])
{
$response = static::getRemote($path, $method, $data);

if ($response && ($response->getStatusCode() == 200)) {
return json_decode($response->getBody())->data;
}

return [];
}
}

0 comments on commit b8018dd

Please sign in to comment.