Completed
Push — 4-cactus ( 59c50f...21a27c )
by Alberto
02:40
created

Core/src/Model/Behavior/PriorityBehavior.php (3 issues)

Severity
1
<?php
2
namespace BEdita\Core\Model\Behavior;
3
4
use Cake\Datasource\EntityInterface;
5
use Cake\Event\Event;
6
use Cake\ORM\Behavior;
7
use Cake\Utility\Hash;
8
9
/**
10
 * Behavior to manage priorities.
11
 *
12
 * @since 4.0.0
13
 */
14
class PriorityBehavior extends Behavior
15
{
16
17
    /**
18
     * {@inheritDoc}
19
     */
20
    protected $_defaultConfig = [
21
        'fields' => [],
22
    ];
23
24
    /**
25
     * {@inheritDoc}
26
     */
27
    public function initialize(array $config)
28
    {
29
        parent::initialize($config);
30
31
        $defaultConfig = (array)$this->getConfig('fields._all');
32
        $defaultConfig += [
33
            'startFrom' => 0,
34
            'scope' => false,
35
        ];
36
37
        $fields = Hash::normalize($this->getConfig('fields'));
38
        unset($fields['_all']);
39
        foreach ($fields as $field => &$config) {
40
            $config = (array)$config + $defaultConfig;
41
        }
42
        unset($config);
43
44
        $this->setConfig('fields', $fields, false);
45
    }
46
47
    /**
48
     * Set up priorities before an entity is saved.
49
     *
50
     * @todo At the present state, priorities are simply set as `0` if missing.
51
     * @param \Cake\Event\Event $event Dispatched event.
52
     * @param \Cake\Datasource\EntityInterface $entity Entity instance.
53
     * @return void
54
     */
55
    public function beforeSave(Event $event, EntityInterface $entity)
0 ignored issues
show
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

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

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...
56
    {
57
        $fields = $this->getConfig('fields');
58
59
        foreach ($fields as $field => $config) {
60
            if ($entity->has($field)) {
61
                continue;
62
            }
63
64
            $entity->set($field, $config['startFrom']);
65
        }
66
    }
67
68
    /**
69
     * Compact other priorities before the entity is deleted.
70
     *
71
     * @todo Implement this method.
72
     * @param \Cake\Event\Event $event Dispatched event.
73
     * @param \Cake\Datasource\EntityInterface $entity Entity instance.
74
     * @return void
75
     */
76
    public function beforeDelete(Event $event, EntityInterface $entity)
0 ignored issues
show
The parameter $entity 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

76
    public function beforeDelete(Event $event, /** @scrutinizer ignore-unused */ EntityInterface $entity)

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...
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

76
    public function beforeDelete(/** @scrutinizer ignore-unused */ Event $event, EntityInterface $entity)

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...
77
    {
78
        return;
79
    }
80
}
81