Completed
Push — master ( 8ddcca...9ec735 )
by Hong
02:37
created

ShareableTrait   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 2
Bugs 0 Features 2
Metric Value
c 2
b 0
f 2
dl 0
loc 52
wmc 5
lcom 1
cbo 0
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getShareable() 0 9 2
A setShareable() 0 6 1
A isShareable() 0 5 1
1
<?php
2
/**
3
 * Phossa Project
4
 *
5
 * PHP version 5.4
6
 *
7
 * @category  Library
8
 * @package   Phossa2\Shared
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\Shared\Shareable;
16
17
/**
18
 * Implementation of ShareableInterface
19
 *
20
 * @package Phossa2\Shared
21
 * @author  Hong Zhang <[email protected]>
22
 * @see     ShareableInterface
23
 * @version 2.0.10
24
 * @since   2.0.10 added
25
 */
26
trait ShareableTrait
27
{
28
    /**
29
     * Shareables' pool
30
     *
31
     * @var    ShareableInstance[]
32
     * @access private
33
     * @staticvar
34
     */
35
    private static $shareables = [];
36
37
    /**
38
     * minimum constructor
39
     *
40
     * @access public
41
     */
42
    public function __construct()
43
    {
44
    }
45
46
    /*
47
     * {@inheritDoc}
48
     */
49
    public static function getShareable(
50
        /*# string */ $scope = '__GLOBAL__'
51
    )/*# : ShareableInterface */ {
52
        $class = get_called_class();
53
        if (!isset(self::$shareables[$scope][$class])) {
54
            static::setShareable(new static(), $scope);
55
        }
56
        return self::$shareables[$class];
57
    }
58
59
    /**
60
     * {@inheritDoc}
61
     */
62
    public static function setShareable(
63
        ShareableInterface $instance,
64
        /*# string */ $scope = '__GLOBAL__'
65
    ) {
66
        self::$shareables[$scope][get_called_class()] = $instance;
67
    }
68
69
    /*
70
     * {@inheritDoc}
71
     */
72
    public function isShareable(
73
        /*# string */ $scope = '__GLOBAL__'
74
    )/*# : bool */ {
75
        return $this === static::getShareable($scope);
76
    }
77
}
78