Passed
Push — master ( c0f43d...08ed67 )
by Julien
05:01
created

Slug   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Test Coverage

Coverage 64.71%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 5
eloc 15
dl 0
loc 41
ccs 11
cts 17
cp 0.6471
rs 10
c 2
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getSlugBehavior() 0 5 1
A setSlugBehavior() 0 3 1
A initializeSlug() 0 11 3
1
<?php
2
3
/**
4
 * This file is part of the Zemit Framework.
5
 *
6
 * (c) Zemit Team <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE.txt
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Zemit\Mvc\Model;
13
14
use Zemit\Mvc\Model;
15
use Zemit\Mvc\Model\AbstractTrait\AbstractBehavior;
16
use Zemit\Mvc\Model\AbstractTrait\AbstractInjectable;
17
use Zemit\Mvc\Model\Behavior\Transformable;
18
19
trait Slug
20
{
21
    use AbstractInjectable;
22
    use AbstractBehavior;
23
    use Options;
24
    
25
    /**
26
     * Initializing Slug
27
     */
28 2
    public function initializeSlug(?array $options = null): void
29
    {
30 2
        $options ??= $this->getOptionsManager()->get('slug') ?? [];
31
        
32 2
        $field = $options['field'] ?? 'slug';
33
        
34 2
        $this->setSlugBehavior(new Transformable([
35 2
            'beforeValidation' => [
36 2
                $field => function (Model $model, string $field) {
37
                    $value = $model->readAttribute($field);
38
                    return $value && is_string($value) ? \Zemit\Support\Slug::generate($value) : $value;
39 2
                },
40 2
            ],
41 2
        ]));
42
    }
43
    
44
    /**
45
     * Set Slug Behavior
46
     */
47 2
    public function setSlugBehavior(Transformable $slugBehavior): void
48
    {
49 2
        $this->setBehavior('slug', $slugBehavior);
0 ignored issues
show
Bug introduced by
The method setBehavior() does not exist on Zemit\Mvc\Model\Slug. Did you maybe mean setSlugBehavior()? ( Ignorable by Annotation )

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

49
        $this->/** @scrutinizer ignore-call */ 
50
               setBehavior('slug', $slugBehavior);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
50
    }
51
    
52
    /**
53
     * Get Slug Behavior
54
     */
55
    public function getSlugBehavior(): Transformable
56
    {
57
        $slugBehavior = $this->getBehavior('slug');
0 ignored issues
show
Bug introduced by
The method getBehavior() does not exist on Zemit\Mvc\Model\Slug. Did you maybe mean getSlugBehavior()? ( Ignorable by Annotation )

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

57
        /** @scrutinizer ignore-call */ 
58
        $slugBehavior = $this->getBehavior('slug');

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
58
        assert($slugBehavior instanceof Transformable);
59
        return $slugBehavior;
60
    }
61
}
62