Completed
Push — master ( 1f3653...b3192f )
by Hong
03:13
created

ShareableTrait::getOwnScopes()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 2
eloc 4
nc 2
nop 0
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
use Phossa2\Shared\Message\Message;
18
use Phossa2\Shared\Exception\RuntimeException;
19
20
/**
21
 * Implementation of ShareableInterface
22
 *
23
 * @package Phossa2\Shared
24
 * @author  Hong Zhang <[email protected]>
25
 * @see     ShareableInterface
26
 * @version 2.0.10
27
 * @since   2.0.10 added
28
 */
29
trait ShareableTrait
30
{
31
    /**
32
     * Shareables' pool
33
     *
34
     * @var    ShareableInstance[]
35
     * @access protected
36
     * @staticvar
37
     */
38
    protected static $shareables = [];
39
40
    /**
41
     * Is this shared instance, store its $scope here
42
     *
43
     * @var    string
44
     * @access protected
45
     */
46
    protected $shared_in;
47
48
    /**
49
     * Scopes of this instance belongs to
50
     *
51
     * @var    string[]
52
     * @access protected
53
     */
54
    protected $scopes = [];
55
56
    /*
57
     * {@inheritDoc}
58
     */
59
    public static function getShareable(
60
        /*# string */ $scope = ''
61
    )/*# : ShareableInterface */ {
62
        if (!static::hasShareable($scope)) {
63
            (new static())->setShareable($scope);
64
        }
65
        return self::$shareables[get_called_class()][$scope];
66
    }
67
68
    /**
69
     * {@inheritDoc}
70
     */
71
    public static function getScopes()/*# : array */
72
    {
73
        if (isset(self::$shareables[get_called_class()])) {
74
            return array_keys(self::$shareables[get_called_class()]);
75
        } else {
76
            return [];
77
        }
78
    }
79
80
    /**
81
     * {@inheritDoc}
82
     */
83
    public function setShareable(/*# string */ $scope = '')
84
    {
85
        // this $scope has shareable already or $this is a shareable
86
        if (static::hasShareable($scope) || $this->isShareable() !== false) {
87
            throw new RuntimeException(
88
                Message::get(Message::MSG_SHAREABLE_FAIL, $scope),
89
                Message::MSG_SHAREABLE_FAIL
90
            );
91
        }
92
93
        $this->shared_in = $scope;
94
        self::$shareables[get_class($this)][$scope] = $this;
95
96
        return $this;
97
    }
98
99
    /**
100
     * {@inheritDoc}
101
     */
102
    public function isShareable()
103
    {
104
        return is_null($this->shared_in) ? false : $this->shared_in;
105
    }
106
107
    /**
108
     * {@inheritDoc}
109
     */
110
    public function addScope(/*# string */ $scope)
111
    {
112
        if ($this->isShareable() === false &&
113
            !in_array($scope, $this->scopes)
114
        ) {
115
            $this->scopes[] = $scope;
116
        }
117
        return $this;
118
    }
119
120
    /**
121
     * Checking with $scope explicitly
122
     *
123
     * {@inheritDoc}
124
     */
125
    public function hasScope(/*# string */ $scope = '')/*# : bool */
126
    {
127
        return in_array($scope, $this->getOwnScopes());
128
    }
129
130
    /**
131
     * {@inheritDoc}
132
     */
133
    public function getShareables()/*# : array */
134
    {
135
        $result = [];
136
        if (false === $this->isShareable()) {
137
            foreach ($this->getOwnScopes() as $scope) {
138
                $result[] = static::getShareable($scope);
139
            }
140
        }
141
        return $result;
142
    }
143
144
    /**
145
     * Test existense of shareable in $scope
146
     *
147
     * @param  string $scope
148
     * @return bool
149
     * @access protected
150
     */
151
    protected static function hasShareable(
152
        /*# string */ $scope = ''
153
    )/*# : bool */ {
154
        return isset(self::$shareables[get_called_class()][$scope]);
155
    }
156
157
    /**
158
     * Get all unique scopes for $this
159
     *
160
     * @return array
161
     * @access protected
162
     */
163
    protected function getOwnScopes()/*# : array */
164
    {
165
        // alway add global scope ''
166
        if (!in_array('', $this->scopes)) {
167
            $this->addScope('');
168
        }
169
        return $this->scopes;
170
    }
171
}
172