Test Failed
Push — master ( 894c40...e5d2d2 )
by Julien
11:34
created

Slug::initializeSlug()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 9
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 13
ccs 0
cts 9
cp 0
crap 12
rs 9.9666
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 Phalcon\Security;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Zemit\Mvc\Model\Security. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
15
use Zemit\Mvc\Model;
16
use Zemit\Mvc\Model\AbstractTrait\AbstractBehavior;
17
use Zemit\Mvc\Model\AbstractTrait\AbstractInjectable;
18
use Zemit\Mvc\Model\Behavior\Transformable;
19
20
trait Slug
21
{
22
    use Options;
23
    use AbstractBehavior;
24
    use AbstractInjectable;
25
    
26
    public Transformable $slugBehavior;
27
    
28
    /**
29
     * Initializing Slug
30
     */
31
    public function initializeSlug(?array $options = null): void
32
    {
33
        $options ??= $this->getOptionsManager()->get('slug') ?? [];
34
        $field = $options['field'] ?? 'slug';
35
        
36
        $security = $this->getDI()->get('security');
37
        assert($security instanceof Security);
38
        
39
        $this->setSlugBehavior(new Transformable([
40
            'beforeValidation' => [
41
                $field => function (Model $model, $field) {
42
                    $value = $model->readAttribute($field);
43
                    return $value && is_string($value) ? \Zemit\Utils\Slug::generate($value) : $value;
44
                },
45
            ],
46
        ]));
47
    }
48
    
49
    /**
50
     * Set Slug Behavior
51
     */
52
    public function setSlugBehavior(Transformable $slugBehavior): void
53
    {
54
        $this->slugBehavior = $slugBehavior;
55
        $this->addBehavior($this->slugBehavior);
56
    }
57
    
58
    /**
59
     * Get Slug Behavior
60
     */
61
    public function getSlugBehavior(): Transformable
62
    {
63
        return $this->slugBehavior;
64
    }
65
}
66