Skip to content

Commit

Permalink
Merge pull request #96 from fleetbase/dev-v1.4.25
Browse files Browse the repository at this point in the history
v1.4.25
  • Loading branch information
roncodes authored May 30, 2024
2 parents 278d22e + d99b707 commit 8069d7c
Show file tree
Hide file tree
Showing 22 changed files with 209 additions and 87 deletions.
1 change: 1 addition & 0 deletions .php-cs-fixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
$rules = [
'@Symfony' => true,
'phpdoc_no_empty_return' => false,
'phpdoc_to_comment' => false,
'array_syntax' => ['syntax' => 'short'],
'yoda_style' => false,
'binary_operator_spaces' => [
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "fleetbase/core-api",
"version": "1.4.24",
"version": "1.4.25",
"description": "Core Framework and Resources for Fleetbase API",
"keywords": [
"fleetbase",
Expand Down
2 changes: 1 addition & 1 deletion config/laravel-model-caching.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

return [
'cache-prefix' => '',
'cache-prefix' => 'fleetbase-model-cache',

'enabled' => env('MODEL_CACHE_ENABLED', true),

Expand Down
2 changes: 1 addition & 1 deletion config/responsecache.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
*
* You may use a string or an array here.
*/
'cache_tag' => '',
'cache_tag' => 'fleetbase-responsecache',

/*
* This class is responsible for generating a hash for a request. This hash
Expand Down
119 changes: 48 additions & 71 deletions src/Expansions/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,30 @@

use Fleetbase\Build\Expansion;
use Fleetbase\Models\Company;
use Fleetbase\Models\File;
use Illuminate\Support\Str;

/**
* Expands the Illuminate\Http\Request class with additional helper methods.
*
* @mixin \Illuminate\Support\Facades\Request
*/
class Request implements Expansion
{
/**
* Get the target class to expand.
* Specifies the class this expansion targets.
*
* @return string|Class
* @return string the name of the class to expand
*/
public static function target()
{
return \Illuminate\Support\Facades\Request::class;
}

/**
* Extends Request to find the current organization/company.
* Retrieves the current company based on the session data.
*
* @return Company|null
* @return \Closure returns a closure that resolves to a Company instance or null
*/
public function company()
{
Expand All @@ -39,21 +42,15 @@ public function company()
}

/**
* Iterates request params until a param is found.
* Attempts to retrieve the first available parameter from a specified set.
*
* @return Closure
* @return \Closure returns a closure that checks for the presence of parameters
* in a specific order and returns the value of the first parameter found
*/
public function or()
{
/*
* Iterates request params until a param is found.
*
* @param array $params
* @param mixed $default
* @return mixed
*/
return function (array $params = [], $default = null) {
/* @var \Illuminate\Http\Request $this */
/** @var \Illuminate\Http\Request $this */
foreach ($params as $param) {
if ($this->has($param)) {
return $this->input($param);
Expand All @@ -65,18 +62,13 @@ public function or()
}

/**
* Retrieve input from the request as a array.
* Converts a specified request parameter into an array by splitting it by commas.
*
* @return Closure
* @return \Closure returns a closure that splits a string parameter into an array,
* or directly returns the array parameter
*/
public function array()
{
/*
* Retrieve input from the request as a array.
*
* @param string $param
* @return array
*/
return function (string $param) {
/** @var \Illuminate\Http\Request $this */
if (is_string($this->input($param)) && Str::contains($this->input($param), ',')) {
Expand All @@ -88,52 +80,40 @@ public function array()
}

/**
* Check if param is string value.
* Checks if a specified parameter is a string.
*
* @return Closure
* @return \Closure returns a closure that determines if a parameter is a string
*/
public function isString()
{
return function ($param) {
/*
* Context.
*
* @var \Illuminate\Support\Facades\Request $this
*/
/** @var \Illuminate\Http\Request $this */
return $this->has($param) && is_string($this->input($param));
};
}

/**
* Check if param is array value.
* Checks if a specified parameter is an array.
*
* @return Closure
* @return \Closure returns a closure that determines if a parameter is an array
*/
public function isArray()
{
return function ($param) {
/*
* Context.
*
* @var \Illuminate\Support\Facades\Request $this
*/
/** @var \Illuminate\Http\Request $this */
return $this->has($param) && is_array($this->input($param));
};
}

/**
* Check value exists in request array param.
* Checks if a specific value exists within an array parameter.
*
* @return Closure
* @return \Closure returns a closure that checks if a value is present in an array parameter
*/
public function inArray()
{
return function ($param, $needle) {
/**
* Context.
*
* @var \Illuminate\Support\Facades\Request $this
*/
/** @var \Illuminate\Http\Request $this */
$haystack = (array) $this->input($param, []);

if (is_array($haystack)) {
Expand All @@ -145,55 +125,39 @@ public function inArray()
}

/**
* Retrieve input from the request as a integer.
* Retrieves an integer value from a specified request parameter.
*
* @return Closure
* @return \Closure returns a closure that fetches an integer from the request
*/
public function integer()
{
/*
* Retrieve input from the request as a integer.
*
* @param string $key
* @return array
*/
return function (string $key, $default = 0) {
/* @var \Illuminate\Http\Request $this */
/** @var \Illuminate\Http\Request $this */
return intval($this->input($key, $default));
};
}

/**
* Removes a param from the request.
* Removes a specified parameter from the request.
*
* @return Closure
* @return \Closure returns a closure that removes a parameter from the request
*/
public function removeParam()
{
/*
* Retrieve input from the request as a integer.
*
* @param string $key
* @return array
*/
return function (string $key) {
/* @var \Illuminate\Http\Request $this */
/** @var \Illuminate\Http\Request $this */
return $this->request->remove($key);
};
}

/**
* Retrieves the search query parameter.
* Retrieves the search query from the request, with prioritization over multiple possible keys.
*
* @return Closure
* @return \Closure returns a closure that fetches a search query parameter, prioritizing
* specific keys and handling potential casing and encoding issues
*/
public function searchQuery()
{
/*
* Retrieve the search query parameter.
*
* @return string
*/
return function () {
/** @var \Illuminate\Http\Request $this */
$searchQueryParam = $this->or(['query', 'searchQuery', 'nestedQuery']);
Expand All @@ -207,9 +171,22 @@ public function searchQuery()
}

/**
* Returns all Fleetbase global filters.
* Fetches File models based on UUIDs provided in a specified request parameter.
*
* @return Closure
* @return \Closure returns a closure that retrieves a collection of File models from UUIDs specified in the request
*/
public function resolveFilesFromIds()
{
return function (string $param = 'files') {
/** @var \Illuminate\Http\Request $this */
return File::fromRequest($this, $param);
};
}

/**
* Retrieves all request parameters except for those related to Fleetbase's global filters.
*
* @return \Closure returns a closure that filters out global parameters and retrieves the rest
*/
public function getFilters()
{
Expand Down Expand Up @@ -244,7 +221,7 @@ public function getFilters()
];
$filters = is_array($additionalFilters) ? array_merge($defaultFilters, $additionalFilters) : $defaultFilters;

/* @var \Illuminate\Http\Request $this */
/** @var \Illuminate\Http\Request $this */
return $this->except($filters);
};
}
Expand Down
23 changes: 23 additions & 0 deletions src/Http/Middleware/ClearCacheAfterDelete.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Fleetbase\Http\Middleware;

use Illuminate\Http\Request;
use Spatie\ResponseCache\Facades\ResponseCache;

class ClearCacheAfterDelete
{
public function handle(Request $request, \Closure $next)
{
// First, handle the request and obtain the response
$response = $next($request);

// Check if the current request method is DELETE
if ($request->isMethod('delete')) {
// Clear the cache after the response has been sent
ResponseCache::clear();
}

return $response;
}
}
18 changes: 17 additions & 1 deletion src/Http/Requests/ImportRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Fleetbase\Http\Requests;

use Fleetbase\Models\File;

class ImportRequest extends FleetbaseRequest
{
/**
Expand All @@ -22,7 +24,21 @@ public function authorize()
public function rules()
{
return [
'files' => ['required', 'array']
'files' => ['required', 'array', 'exists:files,uuid',
function ($attribute, $value, $fail) {
foreach ($value as $uuid) {
$file = File::where('uuid', $uuid)->first();
if (!$file) {
return $fail('One of the files sent for import is invalid.');
}

$validExtensions = ['csv', 'tsv', 'xls', 'xlsx'];
$extension = pathinfo($file->path, PATHINFO_EXTENSION);
if (!in_array($extension, $validExtensions)) {
return $fail('The file (' . $file->original_filename . ') format with the extension ' . $extension . ' is not valid for import.');
}
}
}],
];
}
}
2 changes: 1 addition & 1 deletion src/Http/Requests/OnboardRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class OnboardRequest extends FleetbaseRequest
*
* @return array
*/
protected $excludedWords = ['test', 'testing', 'example', 'trial', 'trialing', 'asdf', '1234', 'asdas', 'dsdsds'];
protected $excludedWords = ['test', 'test123', 'abctest', 'testing', 'example', 'trial', 'trialing', 'asdf', '1234', 'asdas', 'dsdsds', 'dummy', 'xxxx', 'aaa', 'demo', 'zzz', 'zzzz', 'none'];

/**
* Determine if the user is authorized to make this request.
Expand Down
3 changes: 3 additions & 0 deletions src/Jobs/LogApiRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use Spatie\ResponseCache\Facades\ResponseCache;

class LogApiRequest implements ShouldQueue
{
Expand Down Expand Up @@ -58,6 +59,8 @@ public function handle()
{
// Log::info('Logging API Request ' . print_r($this->payload, true));
ApiRequestLog::on($this->dbConnection)->create($this->payload);
// Clear response cache
ResponseCache::clear();
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/Mail/VerificationMail.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public function content(): Content
'currentHour' => now()->hour,
'user' => $this->verificationCode->subject,
'code' => $this->verificationCode->code,
'type' => $this->verificationCode->for,
'content' => $this->content,
]
);
Expand Down
10 changes: 10 additions & 0 deletions src/Models/ApiCredential.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,4 +172,14 @@ public static function generateKeys($encode, $testKey = false)
'secret' => $hash,
];
}

/**
* Update the datetime of the last usage.
*
* @return bool
*/
public function trackLastUsed()
{
return $this->update(['last_used_at' => now()]);
}
}
2 changes: 2 additions & 0 deletions src/Models/ApiEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
use Fleetbase\Casts\Json;
use Fleetbase\Traits\Filterable;
use Fleetbase\Traits\HasApiModelBehavior;
use Fleetbase\Traits\HasPublicId;
use Fleetbase\Traits\HasUuid;
use Fleetbase\Traits\Searchable;

class ApiEvent extends Model
{
use HasUuid;
use HasPublicId;
use HasApiModelBehavior;
use Searchable;
use Filterable;
Expand Down
Loading

0 comments on commit 8069d7c

Please sign in to comment.