Completed
Pull Request — master (#557)
by Adrien
03:10
created

InMemoryStrategy::addStringForIndex()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 2
crap 2
1
<?php
2
3
namespace Box\Spout\Reader\XLSX\Manager\SharedStringsCaching;
4
5
use Box\Spout\Reader\Exception\SharedStringNotFoundException;
6
7
/**
8
 * Class InMemoryStrategy
9
 *
10
 * This class implements the in-memory caching strategy for shared strings.
11
 * This strategy is used when the number of unique strings is low, compared to the memory available.
12
 */
13
class InMemoryStrategy implements CachingStrategyInterface
14
{
15
    /** @var \SplFixedArray Array used to cache the shared strings */
16
    protected $inMemoryCache;
17
18
    /** @var bool Whether the cache has been closed */
19
    protected $isCacheClosed;
20
21
    /**
22
     * @param int $sharedStringsUniqueCount Number of unique shared strings
23
     */
24 36
    public function __construct($sharedStringsUniqueCount)
25
    {
26 36
        $this->inMemoryCache = new \SplFixedArray($sharedStringsUniqueCount);
27 36
        $this->isCacheClosed = false;
28 36
    }
29
30
    /**
31
     * Adds the given string to the cache.
32
     *
33
     * @param string $sharedString The string to be added to the cache
34
     * @param int $sharedStringIndex Index of the shared string in the sharedStrings.xml file
35
     * @return void
36
     */
37 25
    public function addStringForIndex($sharedString, $sharedStringIndex)
38
    {
39 25
        if (!$this->isCacheClosed) {
40 25
            $this->inMemoryCache->offsetSet($sharedStringIndex, $sharedString);
41
        }
42 25
    }
43
44
    /**
45
     * Closes the cache after the last shared string was added.
46
     * This prevents any additional string from being added to the cache.
47
     *
48
     * @return void
49
     */
50 34
    public function closeCache()
51
    {
52 34
        $this->isCacheClosed = true;
53 34
    }
54
55
    /**
56
     * Returns the string located at the given index from the cache.
57
     *
58
     * @param int $sharedStringIndex Index of the shared string in the sharedStrings.xml file
59
     * @throws \Box\Spout\Reader\Exception\SharedStringNotFoundException If no shared string found for the given index
60
     * @return string The shared string at the given index
61
     */
62 25
    public function getStringAtIndex($sharedStringIndex)
63
    {
64
        try {
65 25
            return $this->inMemoryCache->offsetGet($sharedStringIndex);
66 1
        } catch (\RuntimeException $e) {
67 1
            throw new SharedStringNotFoundException("Shared string not found for index: $sharedStringIndex");
68
        }
69
    }
70
71
    /**
72
     * Destroys the cache, freeing memory and removing any created artifacts
73
     *
74
     * @return void
75
     */
76 34
    public function clearCache()
77
    {
78 34
        unset($this->inMemoryCache);
79 34
        $this->isCacheClosed = false;
80 34
    }
81
}
82