Phalcon::getProfiler()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
/**
3
 * @author @fabfuel <[email protected]>
4
 * @created 18.11.14, 07:31
5
 */
6
namespace Fabfuel\Prophiler\Plugin\Manager;
7
8
use Fabfuel\Prophiler\ProfilerInterface;
9
use Fabfuel\Prophiler\Plugin\Phalcon\Db\AdapterPlugin;
10
use Fabfuel\Prophiler\Plugin\Phalcon\Mvc\DispatcherPlugin;
11
use Fabfuel\Prophiler\Plugin\Phalcon\Mvc\ViewPlugin;
12
use Phalcon\DI\Injectable;
13
14
class Phalcon extends Injectable
15
{
16
    /**
17
     * @var ProfilerInterface
18
     */
19
    protected $profiler;
20
21
    /**
22
     * @param ProfilerInterface $profiler
23
     */
24 3
    public function __construct(ProfilerInterface $profiler)
25
    {
26 3
        $this->setProfiler($profiler);
27 3
    }
28
29
    /**
30
     * Register all event manager plugins
31
     */
32 1
    public function register()
33
    {
34 1
        $this->registerDatabase();
35 1
        $this->registerDispatcher();
36 1
        $this->registerView();
37 1
    }
38
39
    /**
40
     * Register database AdapterPlugin in "db" event manager
41
     *
42
     * @return bool
43
     */
44 4
    public function registerDatabase()
45
    {
46 4
        if ($this->ensureEventsManager($this->db)) {
47 3
            $this->db->getEventsManager()->attach('db', AdapterPlugin::getInstance($this->getProfiler()));
48
49 3
            return true;
50
        }
51
52 1
        return false;
53
    }
54
55
    /**
56
     * Register DispatcherPlugin in "dispatcher" event manager
57
     *
58
     * @return bool
59
     */
60 3
    public function registerDispatcher()
61
    {
62 3
        if ($this->ensureEventsManager($this->dispatcher)) {
63 2
            $this->dispatcher->getEventsManager()->attach('dispatch', DispatcherPlugin::getInstance($this->getProfiler()));
64
65 2
            return true;
66
        }
67
68 1
        return false;
69
    }
70
71
    /**
72
     * Register ViewPlugin in common event manager
73
     *
74
     * We can not get the potentially custom events manager from the view, as the view is set up during
75
     * application and module bootstrapping. It does not exist before $application->handle()
76
     *
77
     * @return bool
78
     */
79 2
    public function registerView()
80
    {
81 2
        $this->eventsManager->attach('view', ViewPlugin::getInstance($this->getProfiler()));
82
83 2
        return true;
84
    }
85
86
    /**
87
     * @param object $service
88
     * @return bool
89
     */
90 5
    protected function ensureEventsManager($service)
91
    {
92 5
        if (!method_exists($service, 'getEventsManager')) {
93 2
            return false;
94
        }
95
96 3
        if (!$service->getEventsManager() && method_exists($service, 'setEventsManager')) {
97 1
            $service->setEventsManager($this->eventsManager);
98 1
        }
99
100 3
        return true;
101
    }
102
103
    /**
104
     * @return ProfilerInterface
105
     */
106 2
    public function getProfiler()
107
    {
108 2
        return $this->profiler;
109
    }
110
111
    /**
112
     * @param ProfilerInterface $profiler
113
     */
114 3
    public function setProfiler(ProfilerInterface $profiler)
115
    {
116 3
        $this->profiler = $profiler;
117 3
    }
118
}
119