Completed
Push — master ( 3c1969...5b5792 )
by Schlaefer
03:16 queued 10s
created

AppTable::_setting()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 1
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
1
<?php
2
3
namespace App\Lib\Model\Table;
4
5
use Cake\Core\Configure;
6
use Cake\Core\InstanceConfigTrait;
7
use Cake\Database\Expression\QueryExpression;
8
use Cake\Event\Event;
9
use Cake\Event\EventManager;
10
use Cake\ORM\Entity;
11
use Cake\ORM\Table;
12
use Saito\Event\SaitoEventManager;
13
14
class AppTable extends Table
15
{
16
    use InstanceConfigTrait {
17
        getConfig as private traitGetConfig;
18
    }
19
20
    /** @var array default config for InstanceConfigTrait */
21
    protected $_defaultConfig = [];
22
23
    public $SharedObjects;
24
25
    /**
26
     * @var SaitoEventManager
27
     */
28
    protected $_SEM;
29
30
    /**
31
     * Toggle bool field value.
32
     *
33
     * @param int $recordId The record-ID.
34
     * @param string $field The name of the field to toggle.
35
     * @return int new field The new value after the toggle.
36
     */
37
    public function toggle($recordId, $field)
38
    {
39
        $entity = $this->query()
40
            ->select(['id', $field])
41
            ->where(['id' => $recordId])
42
            ->first();
43
        $new = ($entity->get($field) == 0) ? 1 : 0;
44
        $this->patchEntity($entity, [$field => $new]);
0 ignored issues
show
Bug introduced by
It seems like $entity defined by $this->query()->select(a...=> $recordId))->first() on line 39 can also be of type array or null; however, Cake\ORM\Table::patchEntity() does only seem to accept object<Cake\Datasource\EntityInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
45
        $this->save($entity);
0 ignored issues
show
Bug introduced by
It seems like $entity defined by $this->query()->select(a...=> $recordId))->first() on line 39 can also be of type array or null; however, Cake\ORM\Table::save() does only seem to accept object<Cake\Datasource\EntityInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
46
47
        return $new;
48
    }
49
50
    /**
51
     * Check that a record exists.
52
     *
53
     * @param int|array|\ArrayAccess $conditions Record-ID or query conditions.
54
     * @return bool
55
     */
56
    public function exists($conditions)
57
    {
58
        if (is_int($conditions)) {
59
            $conditions = ['id' => $conditions];
60
        }
61
62
        return parent::exists($conditions);
63
    }
64
65
    /**
66
     * Increments value of a field
67
     *
68
     * @param int|array $where entry-id or array with conditions
69
     * @param string $field fielt to increment
70
     * @param int $amount Increment size.
71
     * @return void
72
     * @throws \InvalidArgumentException
73
     */
74
    public function increment($where, $field, $amount = 1)
75
    {
76
        if (!is_int($amount)) {
77
            throw new \InvalidArgumentException;
78
        }
79
80
        if (is_numeric($where)) {
81
            $where = ['id' => $where];
82
        }
83
84
        $operator = '+';
85
        if ($amount < 0) {
86
            $operator = '-';
87
            $amount *= -1;
88
        }
89
        $expression = new QueryExpression("$field = $field $operator $amount");
90
        $this->updateAll($expression, $where);
0 ignored issues
show
Documentation introduced by
$expression is of type object<Cake\Database\Expression\QueryExpression>, but the function expects a callable.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
91
    }
92
93
    /**
94
     * Dispatches an event
95
     *
96
     * - Always passes the issuing model class as subject
97
     * - Wrapper for CakeEvent boilerplate code
98
     * - Easier to test
99
     *
100
     * @param string $event event identifier `Model.<modelname>.<event>`
101
     * @param array $data additional event data
102
     * @return void
103
     */
104
    protected function _dispatchEvent($event, $data = [])
105
    {
106
        EventManager::instance()->dispatch(new Event($event, $this, $data));
107
        // propagate event on Saito's event bus
108
        $this->dispatchSaitoEvent($event, $data);
109
    }
110
111
    /**
112
     * Dispatch Saito Event
113
     *
114
     * @param string $event event
115
     * @param array $data data
116
     * @return void
117
     */
118
    public function dispatchSaitoEvent($event, $data)
119
    {
120
        if (!$this->_SEM) {
121
            $this->_SEM = SaitoEventManager::getInstance();
122
        }
123
        $this->_SEM->dispatch($event, $data + ['Model' => $this]);
124
    }
125
126
    /**
127
     * {@inheritdoc}
128
     */
129
    public function getConfig($key = null, $default = null)
130
    {
131
        if (is_string($key)) {
132
            $setting = Configure::read('Saito.Settings.' . $key);
133
            if ($setting !== null) {
134
                return $setting;
135
            }
136
        }
137
138
        return $this->traitGetConfig($key, $default);
139
    }
140
}
141