Completed
Push — work-fleets ( b1376e...d6880d )
by SuperNova.WS
11:23
created

IndexedObjectStorage   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 127
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 96.92%

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 127
ccs 63
cts 65
cp 0.9692
rs 10
wmc 21
lcom 1
cbo 0

14 Methods

Rating   Name   Duplication   Size   Complexity  
A indexIsSet() 0 3 1
A indexUnset() 0 5 2
A indexSet() 0 13 3
A setInfo() 0 8 2
A indexGetObject() 0 3 2
A indexRebuild() 0 8 2
A attach() 0 8 2
A detach() 0 4 1
A offsetSet() 0 3 1
A offsetUnset() 0 4 1
A addAll() 0 6 1
A removeAll() 0 4 1
A removeAllExcept() 0 4 1
A unserialize() 0 6 1
1
<?php
2
/**
3
 * Created by Gorlum 17.08.2016 23:08
4
 */
5
6
namespace Common;
7
8
use \SplObjectStorage;
9
10
/**
11
 * Class IndexedObjectStorage
12
 * @package Common
13
 */
14
class IndexedObjectStorage extends \SplObjectStorage {
15
16
  protected $index = array();
17
18
  /**
19
   * @param mixed $index
20
   *
21
   * @return bool
22
   */
23
  public function indexIsSet($index) {
24
    return array_key_exists($index, $this->index);
25
  }
26
27
  /**
28
   * Unassign index from object
29
   *
30
   * @param object $object
31
   */
32 1
  protected function indexUnset($object) {
33 1
    if (($oldData = SplObjectStorage::offsetGet($object)) !== null) {
34 1
      unset($this->index[$oldData]);
35 1
    }
36 1
  }
37
38
  /**
39
   * Function to handle assigning keys
40
   *
41
   * Primary for dealing with duplicates - child can overwrite function to handle duplicates by itself
42
   *
43
   * @param object     $object
44
   * @param mixed|null $data - null means remove index
45
   *
46
   * @throws \Exception
47
   */
48 2
  protected function indexSet($object, $data = null) {
49
    // When calling indexSet - $object already SHOULD not have index associated in this concrete implementation
50
51
    // Checking if index free. NULL index is always free
52 2
    if (isset($this->index[$data])) {
53 1
      throw new \Exception('Duplicate index [' . $data . '] in ' . __CLASS__);
54
    }
55
56 2
    if ($data !== null) {
57
      // Assigning index
58 2
      $this->index[$data] = $object;
59 2
    }
60 2
  }
61
62 1
  public function setInfo($data) {
63 1
    if ($this->valid()) {
64 1
      $this->indexUnset($this->current());
65 1
      $this->indexSet($this->current(), $data);
66 1
    }
67
    // Changing data in storage
68 1
    parent::setInfo($data);
69 1
  }
70
71
  /**
72
   * @param mixed $index
73
   *
74
   * @return mixed|null
75
   */
76 1
  public function indexGetObject($index) {
77 1
    return $this->indexIsSet($index) ? $this->index[$index] : null;
78
  }
79
80
  /**
81
   * Rebuild index
82
   */
83 2
  protected function indexRebuild() {
84 2
    $this->index = array();
85 2
    $this->rewind();
86 2
    while ($this->valid()) {
87 2
      $this->indexSet($this->current(), $this->getInfo());
88 2
      $this->next();
89 2
    }
90 1
  }
91
92 1
  public function attach($object, $data = null) {
93
    // If $object in storage - removing it's index from $indexes
94 1
    if ($this->contains($object)) {
95 1
      $this->indexUnset($object);
96 1
    }
97 1
    $this->indexSet($object, $data);
98 1
    parent::attach($object, $data);
99 1
  }
100
101 1
  public function detach($object) {
102 1
    $this->indexUnset($object);
103 1
    parent::detach($object);
104 1
  }
105
106 1
  public function offsetSet($object, $data = null) {
107 1
    $this->attach($object, $data);
108 1
  }
109
110 1
  public function offsetUnset($object) {
111 1
    $this->detach($object);
112 1
    parent::offsetUnset($object);
113 1
  }
114
115
116 1
  public function addAll($storage) {
117
    // Adding new elements from storage
118 1
    parent::addAll($storage);
119
    // Original addAll overwrites indexes with those in input $storage - so we must refresh current indexes
120 1
    $this->indexRebuild();
121 1
  }
122
123 1
  public function removeAll($storage) {
124 1
    parent::removeAll($storage);
125 1
    $this->indexRebuild();
126 1
  }
127
128 1
  public function removeAllExcept($storage) {
129 1
    parent::removeAllExcept($storage);
130 1
    $this->indexRebuild();
131 1
  }
132
133 1
  public function unserialize($serialized) {
134
    // Unserialize DOES NOT removes existing objects AND creates object copies making duplicates
135
    // So be careful
136 1
    parent::unserialize($serialized);
137 1
    $this->indexRebuild();
138 1
  }
139
140
}
141