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

AbstractMediaFieldHandler   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
eloc 11
c 1
b 0
f 0
dl 0
loc 30
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A sluggifyFilename() 0 8 2
A __construct() 0 5 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