Passed
Push — master ( 859573...a81ea2 )
by Gabriel
03:21
created

HasValidatorTrait::hydrateValidator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace ByTIC\MediaLibrary\Validation\Traits;
4
5
use ByTIC\MediaLibrary\Validation\Validators\AbstractValidator;
6
use ByTIC\MediaLibrary\Validation\Validators\FileValidator;
7
use ByTIC\MediaLibrary\Validation\Validators\GenericValidator;
8
use ByTIC\MediaLibrary\Validation\Validators\ImageValidator;
9
10
/**
11
 * Trait HasValidatorTrait.
12
 */
13
trait HasValidatorTrait
14
{
15
    /**
16
     * @return AbstractValidator|null
17
     */
18 5
    public function getValidator()
19
    {
20 5
        $validator = $this->generateValidator();
21
22 5
        return $validator;
23
    }
24
25
    /**
26
     * @return AbstractValidator
27
     */
28 5
    protected function generateValidator()
29
    {
30 5
        $validator = $this->newValidator();
31 5
        $this->hydrateValidator($validator);
32
33 5
        return $validator;
34
    }
35
36
    /**
37
     * @return AbstractValidator
38
     */
39 5
    protected function newValidator()
40
    {
41 5
        $class = $this->getValidatorClassName();
42
43 5
        return new $class();
44
    }
45
46
    /**
47
     * @return string
48
     */
49 5
    protected function getValidatorClassName()
50
    {
51 5
        $mediaType = $this->getMediaType();
0 ignored issues
show
Bug introduced by
It seems like getMediaType() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

51
        /** @scrutinizer ignore-call */ 
52
        $mediaType = $this->getMediaType();
Loading history...
52 5
        switch ($mediaType) {
53 5
            case 'images':
54
                return ImageValidator::class;
55 4
            case 'files':
56
                return FileValidator::class;
57
        }
58
59
        return GenericValidator::class;
60
    }
61
62
    /**
63
     * @param AbstractValidator $validator
64
     */
65 5
    protected function hydrateValidator($validator)
66
    {
67 5
        $validator->setCollection($this);
68 5
    }
69
70
    /**
71
     * @return bool
72
     */
73
    public function hasCustomValidator()
74
    {
75
        return false;
76
    }
77
}
78