SluggableBehavior   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
wmc 2
eloc 14
c 3
b 0
f 1
dl 0
loc 42
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A slug() 0 11 1
A beforeSave() 0 3 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Sluggable\Model\Behavior;
5
6
use ArrayObject;
7
use Cake\Datasource\EntityInterface;
8
use Cake\Event\Event;
9
use Cake\ORM\Behavior;
10
use Cake\ORM\Entity;
11
use Cake\ORM\Query;
12
use Cake\Utility\Text;
13
14
/**
15
 * Sluggable behavior
16
 */
17
class SluggableBehavior extends Behavior
18
{
19
    /**
20
     * Default configuration.
21
     *
22
     * @var array
23
     */
24
    protected $_defaultConfig = [
25
        'field' => 'name',
26
        'slug' => 'slug',
27
        'replacement' => '-',
28
    ];
29
30
    /**
31
     * slug description
32
     *
33
     * @param \Cake\ORM\Entity $entity The entity that is going to be a slug
34
     * @return void
35
     */
36
    public function slug(Entity $entity)
37
    {
38
        $config = $this->getConfig();
39
        $value = $entity->get($config['field']);
40
        $value = str_replace(
41
            ['é', 'è', 'ê', 'ë', 'à', 'â', 'î', 'ï', 'ô', 'ù', 'û', 'ç', 'á', 'í', 'ó', 'ú', 'ñ'],
42
            ['e', 'e', 'e', 'e', 'a', 'a', 'i', 'i', 'o', 'u', 'u', 'c', 'a', 'i', 'o', 'u', 'n'],
43
            $value
44
        );
45
        $value = strtolower(Text::slug($value, $config['replacement']));
46
        $entity->set($config['slug'], substr($value, 0, 100));
47
    }
48
49
    /**
50
     * [beforeSave description]
51
     * @param \Cake\Event\Event $event The beforeSave event that was fired
52
     * @param \Cake\Datasource\EntityInterface $entity  [The entity that is going to be saved]
53
     * @param \ArrayObject $options [description]
54
     * @return void
55
     */
56
    public function beforeSave(Event $event, EntityInterface $entity, ArrayObject $options)
0 ignored issues
show
Unused Code introduced by
The parameter $options is not used and could be removed. ( Ignorable by Annotation )

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

56
    public function beforeSave(Event $event, EntityInterface $entity, /** @scrutinizer ignore-unused */ ArrayObject $options)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $event is not used and could be removed. ( Ignorable by Annotation )

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

56
    public function beforeSave(/** @scrutinizer ignore-unused */ Event $event, EntityInterface $entity, ArrayObject $options)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
57
    {
58
        $this->slug($entity);
59
    }
60
}
61