Completed
Push — master ( 655053...db47c8 )
by Ben
02:17
created

ObjectStorage::getIndirectObject()   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.4286
cc 2
eloc 4
nc 2
nop 1
1
<?php
2
/**
3
 * BaconPdf
4
 *
5
 * @link      http://github.com/Bacon/BaconPdf For the canonical source repository
6
 * @copyright 2015 Ben 'DASPRiD' Scholzen
7
 * @license   http://opensource.org/licenses/BSD-2-Clause Simplified BSD License
8
 */
9
10
namespace Bacon\Pdf\Object;
11
12
use Bacon\Pdf\Exception\DomainException;
13
use Bacon\Pdf\Exception\OutOfBoundsException;
14
use Countable;
15
use IteratorAggregate;
16
use SplObjectStorage;
17
18
/**
19
 * Storage for mapping indirect objects to concrete objects.
20
 */
21
final class ObjectStorage implements IteratorAggregate, Countable
22
{
23
    /**
24
     * @var ObjectInterface[]
25
     */
26
    private $objects = [];
27
28
    /**
29
     * @var SplObjectStorage|IndirectObject[]
30
     */
31
    private $indirectObjects = [];
32
33
    /**
34
     * @var int
35
     */
36
    private $objectCount = 0;
37
38
    public function __construct()
39
    {
40
        $this->indirectObjects = new SplObjectStorage();
41
    }
42
43
    /**
44
     * Adds an object to the storage.
45
     *
46
     * @param  ObjectInterface $object
47
     * @return IndirectObject
48
     */
49
    public function addObject(ObjectInterface $object)
50
    {
51
        $id = ++$this->objectCount;
52
        $this->objects[$id] = $object;
53
        $this->indirectObjects[$object] = new IndirectObject($id, 0, $this);
54
    }
55
56
    /**
57
     * @return int
58
     */
59
    public function reserveSlot()
60
    {
61
        $id = ++$this->objectCount;
62
        $this->objects[$id] = null;
63
        return $id;
64
    }
65
66
    /**
67
     * @param  IndirectObject $indirectObject
68
     * @throws DomainException
69
     */
70
    public function fillSlot(IndirectObject $indirectObject)
71
    {
72
        if ($indirectObject->getObjectStorage() !== $this) {
73
            throw new DomainException('Object storage of indirect object must equal this object storage');
74
        }
75
76
        $id = $indirectObject->getId();
77
78
        if (!isset($this->objects[$id])) {
79
            throw new DomainException(sprintf('No reserved slot for id %d found', $id));
80
        }
81
82
        $this->objects[$id] = $indirectObject;
83
    }
84
85
    /**
86
     * @param  IndirectObject $indirectObject
87
     * @return ObjectInterface
88
     */
89
    public function getObject(IndirectObject $indirectObject)
90
    {
91
        if ($indirectObject->getObjectStorage() !== $this) {
92
            throw new DomainException('Object storage of indirect object must equal this object storage');
93
        }
94
95
        return $this->objects[$indirectObject->getId()];
96
    }
97
98
    /**
99
     * @param  ObjectInterface $object
100
     * @return IndirectObject
101
     * @throws OutOfBoundsException
102
     */
103
    public function getIndirectObject(ObjectInterface $object)
104
    {
105
        if (!$this->indirectObjects->contains($object)) {
106
            throw new OutOfBoundsException('Object is not registered in the storage');
107
        }
108
109
        return $this->indirectObjects[$object];
110
    }
111
112
    /**
113
     * {@inheritdoc}
114
     */
115
    public function getIterator()
116
    {
117
        foreach ($this->objects as $id => $object) {
118
            if (null === $object) {
119
                throw new DomainException(sprintf('Object slot for id %d was reserved but not filled', $id));
120
            }
121
122
            yield $id => $object;
123
        }
124
    }
125
126
    /**
127
     * {@inheritdoc}
128
     */
129
    public function count($mode = 'COUNT_NORMAL')
130
    {
131
        return $this->objectCount;
132
    }
133
}
134