Passed
Push — fix/slim ( 590b9b...bb7246 )
by Ben
08:37 queued 41s
created

ChecksExistingAssets   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 19
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 9
c 1
b 0
f 0
dl 0
loc 19
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A looksLikeAnAssetId() 0 8 3
A isKeyAnAttachedAssetId() 0 7 2
1
<?php
2
declare(strict_types=1);
3
4
namespace Thinktomorrow\Chief\Media\Application;
5
6
use Illuminate\Support\Collection;
7
8
trait ChecksExistingAssets
9
{
10
    protected function looksLikeAnAssetId($value): bool
11
    {
12
        if (!is_string($value) && !is_int($value)) {
13
            return false;
14
        }
15
16
        // check if passed value is an ID
17
        return (bool)preg_match('/^[1-9][0-9]*$/', (string)$value);
18
    }
19
20
    protected function isKeyAnAttachedAssetId(Collection $existingAttachedAssets, string $locale, string $type, $assetId): bool
21
    {
22
        return ($this->looksLikeAnAssetId($assetId) && $existingAttachedAssets
23
                ->where('pivot.locale', $locale)
24
                ->where('pivot.type', $type)
25
                ->where('pivot.asset_id', $assetId)
26
                ->count());
27
    }
28
}
29