Completed
Push — master ( 38accd...2bf9cf )
by Julito
10:36
created

HookObserver::getEntityManager()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
namespace Chamilo\CoreBundle\Hook;
6
7
use Chamilo\CoreBundle\Hook\Interfaces\HookObserverInterface;
8
use Doctrine\ORM\EntityManager;
9
10
/**
11
 * Class HookObserver
12
 * This abstract class implements Hook Observer Interface to build the base
13
 * for Hook Observer. This class have some public static method,
14
 * e.g for create Hook Observers.
15
 * This file contains an abstract Hook observer class
16
 * Used for Hook Observers in plugins, called when a hook event happens
17
 * (e.g Create user, Webservice registration).
18
 */
19
abstract class HookObserver implements HookObserverInterface
20
{
21
    public $path;
22
    public $pluginName;
23
    private $entityManager;
24
25
    /**
26
     * Construct method
27
     * Save the path of Hook Observer class implementation and
28
     * the plugin name where this class is included.
29
     *
30
     * @param string $path
31
     * @param string $pluginName
32
     */
33
    protected function __construct($path, $pluginName)
34
    {
35
        $this->path = $path;
36
        $this->pluginName = $pluginName;
37
    }
38
39
    public function getEntityManager()
40
    {
41
        return $this->entityManager;
42
    }
43
44
    public function setEntityManager(EntityManager $entityManager)
45
    {
46
        $this->entityManager = $entityManager;
47
    }
48
49
    /**
50
     * Return the singleton instance of Hook observer.
51
     * If Hook Management plugin is not enabled, will return NULL.
52
     *
53
     * @return HookObserver
54
     */
55
    public static function create()
56
    {
57
        static $result = null;
58
59
        if ($result) {
60
            return $result;
61
        } else {
62
            try {
63
                $class = get_called_class();
64
65
                return new $class();
66
            } catch (Exception $e) {
0 ignored issues
show
Bug introduced by
The type Chamilo\CoreBundle\Hook\Exception was not found. Did you mean Exception? If so, make sure to prefix the type with \.
Loading history...
67
                return null;
68
            }
69
        }
70
    }
71
72
    /**
73
     * Return the path from the class, needed to store location or autoload later.
74
     *
75
     * @return string
76
     */
77
    public function getPath()
78
    {
79
        return $this->path;
80
    }
81
82
    /**
83
     * Return the plugin name where is the Hook Observer.
84
     *
85
     * @return string
86
     */
87
    public function getPluginName()
88
    {
89
        return $this->pluginName;
90
    }
91
}
92