CollectorStats   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 1
dl 0
loc 102
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A methodsAvailable() 0 4 1
A beforeMatch() 0 5 1
A afterMatch() 0 5 1
A getStats() 0 9 1
A extensionHandles() 0 13 1
1
<?php
2
/**
3
 * Phossa Project
4
 *
5
 * PHP version 5.4
6
 *
7
 * @category  Library
8
 * @package   Phossa2\Route
9
 * @copyright Copyright (c) 2016 phossa.com
10
 * @license   http://mit-license.org/ MIT License
11
 * @link      http://www.phossa.com/
12
 */
13
/*# declare(strict_types=1); */
14
15
namespace Phossa2\Route\Extension;
16
17
use Phossa2\Event\Interfaces\EventInterface;
18
use Phossa2\Route\Interfaces\ResultInterface;
19
use Phossa2\Event\EventableExtensionAbstract;
20
use Phossa2\Route\Collector\CollectorAbstract;
21
22
/**
23
 * CollectorStats
24
 *
25
 * Collecting statistics of a collector
26
 *
27
 * @package Phossa2\Route
28
 * @author  Hong Zhang <[email protected]>
29
 * @see     EventableExtensionAbstract
30
 * @see     ResultInterface
31
 * @version 2.0.0
32
 * @since   2.0.0 added
33
 */
34
class CollectorStats extends EventableExtensionAbstract
35
{
36
    /**
37
     * Total routes trying to match
38
     * @var    int
39
     * @access protected
40
     */
41
    protected $total = 0;
42
43
    /**
44
     * Number of routes matched
45
     *
46
     * @var    int
47
     * @access protected
48
     */
49
    protected $matched = 0;
50
51
    /**
52
     * {@inheritDoc}
53
     */
54
    public function methodsAvailable()/*# : array */
55
    {
56
        return ['beforeMatch', 'afterMatch', 'getStats'];
57
    }
58
59
    /**
60
     * Extension methods takes an event as input.
61
     *
62
     * Event params has `Phossa2\Route\Result` set as 'result'
63
     *
64
     * MUST RETURN A BOOLEAN VALUE !!!
65
     *
66
     * @param  EventInterface $event
67
     * @return bool
68
     * @access protected
69
     */
70
    public function beforeMatch(EventInterface $event)/*# : bool */
0 ignored issues
show
Unused Code introduced by
The parameter $event is not used and could be removed.

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

Loading history...
71
    {
72
        ++$this->total;
73
        return true;
74
    }
75
76
    /**
77
     * Extension methods takes an event as input.
78
     *
79
     * Event params has `Phossa2\Route\Result` set as 'result'
80
     *
81
     * MUST RETURN A BOOLEAN VALUE !!!
82
     *
83
     * @param  EventInterface $event
84
     * @return bool
85
     * @access protected
86
     */
87
    public function afterMatch(EventInterface $event)/*# : bool */
0 ignored issues
show
Unused Code introduced by
The parameter $event is not used and could be removed.

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

Loading history...
88
    {
89
        ++$this->matched;
90
        return true;
91
    }
92
93
    /**
94
     * Returns the stats collected
95
     * @access public
96
     */
97
    public function getStats()
98
    {
99
        echo sprintf(
100
            "Total %d Matched %d (%s%%)",
101
            $this->total,
102
            $this->matched,
103
            number_format($this->matched * 100.0 / $this->total, 1)
104
        );
105
    }
106
107
    /**
108
     * Return event handlers of this extension handling
109
     *
110
     * ```php
111
     * protected function extensionHandles()
112
     * {
113
     *     return [
114
     *         ['event' => 'cache.*', 'handler' => ['byPassCache', 100]],
115
     *     ];
116
     * }
117
     * ```
118
     *
119
     * @return array
120
     * @access protected
121
     */
122
    protected function extensionHandles()/*# : array */
123
    {
124
        return [
125
            [
126
                'event' => CollectorAbstract::EVENT_BEFORE_MATCH,
127
                'handler' => 'beforeMatch'
128
            ],
129
            [
130
                'event' => CollectorAbstract::EVENT_AFTER_MATCH,
131
                'handler' => 'afterMatch'
132
            ],
133
        ];
134
    }
135
}
136