Pool   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
dl 0
loc 96
rs 10
c 0
b 0
f 0
wmc 17
lcom 1
cbo 4

6 Methods

Rating   Name   Duplication   Size   Complexity  
A RPCall() 0 7 2
A onReady() 0 6 2
A onConfigUpdated() 0 7 2
A onShutdown() 0 7 2
A getConfigDefaults() 0 4 1
B init() 0 23 8
1
<?php
2
namespace PHPDaemon\Core;
3
4
/**
5
 * Pool application instance
6
 *
7
 * @package Core
8
 *
9
 * @author  Vasily Zorin <[email protected]>
10
 */
11
class Pool extends AppInstance
12
{
13
    /**
14
     * @var Pool
15
     */
16
    public $pool;
17
18
    /**
19
     * Function handles incoming Remote Procedure Calls
20
     * You can override it
21
     * @param string $method Method name.
22
     * @param array $args Arguments.
23
     * @return mixed Result
24
     */
25
    public function RPCall($method, $args)
26
    {
27
        if (!is_callable($f = [$this->pool, 'RPCall'])) {
28
            return false;
29
        }
30
        return $f($method, $args);
31
    }
32
33
    /**
34
     * Called when the worker is ready to go.
35
     * @return void
36
     */
37
    public function onReady()
38
    {
39
        if ($this->pool) {
40
            $this->pool->onReady();
41
        }
42
    }
43
44
    /**
45
     * Called when worker is going to update configuration.
46
     * @return void
47
     */
48
    public function onConfigUpdated()
49
    {
50
        if ($this->pool) {
51
            $this->pool->config = $this->config;
52
            $this->pool->onConfigUpdated();
53
        }
54
    }
55
56
    /**
57
     * Called when application instance is going to shutdown.
58
     * @return boolean Ready to shutdown?
59
     */
60
    public function onShutdown($graceful = false)
61
    {
62
        if ($this->pool) {
63
            return $this->pool->onShutdown($graceful);
64
        }
65
        return true;
66
    }
67
68
    /**
69
     * Setting default config options
70
     * Overriden from AppInstance::getConfigDefaults
71
     * Uncomment and return array with your default options
72
     * @return boolean
73
     */
74
    protected function getConfigDefaults()
75
    {
76
        return false;
77
    }
78
79
    /**
80
     * Constructor.
81
     * @return void
82
     */
83
    protected function init()
84
    {
85
        if ($this->isEnabled()) {
86
            list($class, $name) = explode(':', $this->name . ':');
87
            $realclass = ClassFinder::find($class);
88
            $e = explode('\\', $realclass);
89
            if (($e[sizeof($e) - 1] !== 'Pool') && class_exists($realclass . '\\Pool')) {
90
                $realclass .= '\\Pool';
91
            }
92
            if ($realclass !== $class) {
93
                $base = '\\PHPDaemon\\Core\\Pool:';
94
                Daemon::$config->renameSection($base . $class . ($name !== '' ? ':' . $name : ''),
95
                    $base . $realclass . ($name !== '' ? ':' . $name : ''));
96
            }
97
            if (!class_exists($realclass)) {
98
                Daemon::log($realclass . ' class not exists.');
99
                return;
100
            }
101
            $func = [$realclass, 'getInstance'];
102
            $this->pool = $func($name);
103
            $this->pool->appInstance = $this;
104
        }
105
    }
106
}
107