Completed
Push — master ( a38144...cd2367 )
by Fwolf
02:48
created

AbstractPluginFactory::createPlugin()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
1
<?php
2
3
namespace Fwolf\Tools\TtSync\PluginApi;
4
5
/**
6
 * One factory for one plugin class
7
 *
8
 * @copyright   Copyright 2017 Fwolf
9
 * @license     https://opensource.org/licenses/MIT MIT
10
 */
11
abstract class AbstractPluginFactory implements PluginFactoryInterface
12
{
13
    /**
14
     * Class name of plugin this factory will create
15
     */
16
    const PLUGIN_CLASS_NAME = '';
17
18
19
    /**
20
     * Configure plugin instance
21
     *
22
     * @param   PluginInterface $plugin
23
     * @return  PluginInterface
24
     */
25
    protected function configPlugin(PluginInterface $plugin): PluginInterface
26
    {
27
        // Do config
28
29
        return $plugin;
30
    }
31
32
33
    /**
34
     * @inheritDoc
35
     */
36
    public function createPlugin(): PluginInterface
37
    {
38
        $className = $this->getPluginClass();
39
40
        $plugin = new $className;
41
42
        $plugin = $this->configPlugin($plugin);
43
44
        return $plugin;
45
    }
46
47
48
    /**
49
     * Get or reuse instance of self
50
     *
51
     * @return PluginFactoryInterface
52
     */
53
    public static function getInstance(): PluginFactoryInterface
54
    {
55
        static $instances = [];
56
57
        $className = get_called_class();
58
59
        if (!isset($instances[$className])) {
60
            $instances[$className] = new $className();
61
        }
62
63
        return $instances[$className];
64
    }
65
66
67
    /**
68
     * @inheritDoc
69
     */
70
    public function getPluginClass(): string
71
    {
72
        return static::PLUGIN_CLASS_NAME;
73
    }
74
}
75