Completed
Push — master ( f4309a...8ddcca )
by Hong
03:09
created

ShareableTrait   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 4
c 1
b 0
f 1
lcom 1
cbo 0
dl 0
loc 43
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getShareable() 0 9 2
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
     * @final
42
     */
43
    final public function __construct()
44
    {
45
    }
46
47
    /*
48
     * {@inheritDoc}
49
     */
50
    public static function getShareable(
51
        /*# string */ $scope = '__GLOBAL__'
52
    )/*# : ShareableInterface */ {
53
        $class = get_called_class();
54
        if (!isset(self::$shareables[$scope][$class])) {
55
            self::$shareables[$scope][$class] = new static();
56
        }
57
        return self::$shareables[$class];
58
    }
59
60
    /*
61
     * {@inheritDoc}
62
     */
63
    public function isShareable(
64
        /*# string */ $scope = '__GLOBAL__'
65
    )/*# : bool */ {
66
        return $this === static::getShareable($scope);
67
    }
68
}
69