Skip to content

Commit

Permalink
Update ProcessStamp.php (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander White committed May 19, 2020
1 parent dc355ef commit 2c0df53
Showing 1 changed file with 19 additions and 16 deletions.
35 changes: 19 additions & 16 deletions src/ProcessStamp.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;

class ProcessStamp extends Model
{
Expand All @@ -21,7 +22,7 @@ class ProcessStamp extends Model
*
* @return string
*/
public function getKeyName(): string
public function getKeyName() : string
{
return config('process-stamps.columns.primary_key');
}
Expand All @@ -31,7 +32,7 @@ public function getKeyName(): string
*
* @return string
*/
public function getTable(): string
public function getTable() : string
{
return config('process-stamps.table');
}
Expand All @@ -42,7 +43,7 @@ public function getTable(): string
*
* @return ProcessStamp
*/
public static function firstOrCreateByProcess(array $process, ?string $hash = null): self
public static function firstOrCreateByProcess(array $process, ?string $hash = null) : self
{
if (empty($process['type'])) {
$process['type'] = 'other';
Expand All @@ -57,19 +58,21 @@ public static function firstOrCreateByProcess(array $process, ?string $hash = nu
$parent = static::firstOrCreateByProcess(static::getProcessName($process['type'], $process['parent_name']));
}

return static::firstOrCreate(['hash' => $hash], [
'name' => trim($process['name']),
'type' => $process['type'],
'parent_id' => optional($parent)->getKey(),
]);
return DB::transaction(function () use ($hash, $process, $parent) {
return static::lockForUpdate()->firstOrCreate(['hash' => $hash], [
'name' => trim($process['name']),
'type' => $process['type'],
'parent_id' => optional($parent)->getKey(),
]);
}, 5);
}

/**
* @param array $process
*
* @return string
*/
public static function makeProcessHash(array $process): string
public static function makeProcessHash(array $process) : string
{
return sha1($process['type'] . '-' . trim($process['name']));
}
Expand All @@ -79,7 +82,7 @@ public static function makeProcessHash(array $process): string
*
* @return BelongsTo|null
*/
public function parent(): ?BelongsTo
public function parent() : ?BelongsTo
{
return $this->belongsTo(self::class, 'parent_id');
}
Expand All @@ -89,15 +92,15 @@ public function parent(): ?BelongsTo
*
* @return HasMany|null
*/
public function children(): ?HasMany
public function children() : ?HasMany
{
return $this->hasMany(self::class, 'parent_id');
}

/**
* @return string
*/
public static function getType(): string
public static function getType() : string
{
if (isset($_SERVER['REQUEST_URI'])) {
return 'url';
Expand All @@ -122,7 +125,7 @@ public static function getType(): string
*
* @return array
*/
public static function getProcessName(?string $type = null, ?string $raw_process = null): array
public static function getProcessName(?string $type = null, ?string $raw_process = null) : array
{
$parent_name = null;
$type = $type ?? static::getType();
Expand Down Expand Up @@ -159,7 +162,7 @@ public static function getProcessName(?string $type = null, ?string $raw_process
/**
* @return int
*/
public static function getCurrentProcessId(): int
public static function getCurrentProcessId() : int
{
$process = static::getProcessName();
$hash = ProcessStamp::makeProcessHash($process);
Expand All @@ -179,7 +182,7 @@ public static function getCurrentProcessId(): int
*
* @return string|null
*/
public static function getParentUrl(string $url): ?string
public static function getParentUrl(string $url) : ?string
{
if (strpos($stripped = trim($url, '/'), '/')) {
$parts = preg_split("/(\/|\?)/", $stripped);
Expand All @@ -193,7 +196,7 @@ public static function getParentUrl(string $url): ?string
return null;
}

public static function getParentArtisan(string $command): ?string
public static function getParentArtisan(string $command) : ?string
{
$command = trim($command);
if (strpos($command, ' --')) {
Expand Down

0 comments on commit 2c0df53

Please sign in to comment.