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

ChecksExistingAssets::looksLikeAnAssetId()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 8
rs 10
cc 3
nc 2
nop 1
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