Test Failed
Push — master ( ce60e5...378563 )
by Julien
12:41 queued 07:49
created

Events   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 5
Bugs 2 Features 1
Metric Value
wmc 9
eloc 21
c 5
b 2
f 1
dl 0
loc 54
ccs 0
cts 26
cp 0
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A count() 0 3 1
A fireEventCancelCall() 0 15 2
A average() 0 3 1
A findFirst() 0 4 2
A sum() 0 3 1
A find() 0 12 2
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\Mvc\Model\Resultset\Simple;
15
use Phalcon\Mvc\Model\ResultsetInterface;
16
use Phalcon\Mvc\ModelInterface;
17
use Zemit\Support\Helper;
18
19
trait Events
20
{
21
    abstract public function fireEventCancel(string $eventName): bool;
22
    
23
    public static function find($parameters = null): ResultsetInterface
24
    {
25
        $ret = self::fireEventCancelCall(__FUNCTION__, func_get_args());
26
        
27
        if (!$ret) {
28
            $that = new self();
29
            assert($that instanceof ModelInterface);
30
            $columnMap = $that->getModelsMetaData()->getColumnMap($that);
31
            return new Simple($columnMap, $that, false);
32
        }
33
        
34
        return $ret;
35
    }
36
37
    public static function findFirst($parameters = null): ?ModelInterface
38
    {
39
        $ret = self::fireEventCancelCall(__FUNCTION__, func_get_args());
40
        return $ret ?: null;
41
    }
42
43
    public static function count($parameters = null)
44
    {
45
        return self::fireEventCancelCall(__FUNCTION__, func_get_args());
46
    }
47
48
    public static function sum($parameters = null)
49
    {
50
        return self::fireEventCancelCall(__FUNCTION__, func_get_args());
51
    }
52
    
53
    public static function average($parameters = null)
54
    {
55
        return self::fireEventCancelCall(__FUNCTION__, func_get_args());
56
    }
57
58
    public static function fireEventCancelCall(string $method, array $arguments = [])
59
    {
60
        $class = get_called_class();
61
        $that = new $class();
62
        $event = ucfirst(Helper::camelize($method));
63
        
64
        if ($that->fireEventCancel('before' . $event) === false) {
65
            return false;
66
        }
67
68
        $ret = @parent::$method(...$arguments); // @todo fix this for php 83+
69
        
70
        $that->fireEvent('after' . $event);
71
        
72
        return $ret;
73
    }
74
}
75