Test Failed
Push — fix/media-validation ( 3351fe )
by Ben
09:34
created

AbstractMediaFieldHandler::sluggifyFilename()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 8
rs 10
cc 2
nc 2
nop 1
1
<?php declare(strict_types=1);
2
3
namespace Thinktomorrow\Chief\Media\Application;
4
5
use Illuminate\Support\Str;
6
use Thinktomorrow\AssetLibrary\Application\AddAsset;
7
use Thinktomorrow\AssetLibrary\Application\DetachAsset;
8
use Thinktomorrow\AssetLibrary\Application\ReplaceAsset;
9
10
abstract class AbstractMediaFieldHandler
11
{
12
    /** @var ReplaceAsset */
13
    protected $replaceAsset;
14
15
    /** @var AddAsset */
16
    protected $addAsset;
17
18
    /** @var DetachAsset */
19
    protected $detachAsset;
20
21
    final public function __construct(AddAsset $addAsset, ReplaceAsset $replaceAsset, DetachAsset $detachAsset)
22
    {
23
        $this->replaceAsset = $replaceAsset;
24
        $this->addAsset = $addAsset;
25
        $this->detachAsset = $detachAsset;
26
    }
27
28
    /**
29
     * @param string $filename
30
     * @return string
31
     */
32
    protected function sluggifyFilename(string $filename): string
33
    {
34
        if(false === strpos($filename, '.')) return $filename;
35
36
        $extension = substr($filename, strrpos($filename, '.') + 1);
37
        $filename = substr($filename, 0, strrpos($filename, '.'));
38
39
        return Str::slug($filename) . '.' . $extension;
40
    }
41
}
42