Completed
Push — 5.x ( b7e0f5...831ff0 )
by Lars
16:50 queued 10:32
created

Swift_KeyCache_ArrayKeyCache::_prepareCache()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2
Metric Value
dl 0
loc 6
ccs 5
cts 5
cp 1
rs 9.4286
cc 2
eloc 3
nc 2
nop 1
crap 2
1
<?php
2
3
/*
4
 * This file is part of SwiftMailer.
5
 * (c) 2004-2009 Chris Corbyn
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
/**
12
 * A basic KeyCache backed by an array.
13
 *
14
 * @author Chris Corbyn
15
 */
16
class Swift_KeyCache_ArrayKeyCache implements Swift_KeyCache
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
17
{
18
    /**
19
     * Cache contents.
20
     *
21
     * @var array
22
     */
23
    private $_contents = array();
24
25
    /**
26
     * An InputStream for cloning.
27
     *
28
     * @var Swift_KeyCache_KeyCacheInputStream
29
     */
30
    private $_stream;
31
32
    /**
33
     * Create a new ArrayKeyCache with the given $stream for cloning to make
34
     * InputByteStreams.
35
     *
36
     * @param Swift_KeyCache_KeyCacheInputStream $stream
37
     */
38 66
    public function __construct(Swift_KeyCache_KeyCacheInputStream $stream)
39
    {
40 66
        $this->_stream = $stream;
41 66
    }
42
43
    /**
44
     * Set a string into the cache under $itemKey for the namespace $nsKey.
45
     *
46
     * @see MODE_WRITE, MODE_APPEND
47
     *
48
     * @param string $nsKey
49
     * @param string $itemKey
50
     * @param string $string
51
     * @param int    $mode
52
     *
53
     * @throws Swift_SwiftException
54
     */
55 28
    public function setString($nsKey, $itemKey, $string, $mode)
56
    {
57 28
        $this->_prepareCache($nsKey);
58
        switch ($mode) {
59 28
            case self::MODE_WRITE:
60 25
                $this->_contents[$nsKey][$itemKey] = $string;
61 25
                break;
62 5
            case self::MODE_APPEND:
63 5
                if (!$this->hasKey($nsKey, $itemKey)) {
64 3
                    $this->_contents[$nsKey][$itemKey] = '';
65 3
                }
66 5
                $this->_contents[$nsKey][$itemKey] .= $string;
67 5
                break;
68
            default:
69
                throw new Swift_SwiftException(
70
                    'Invalid mode [' . $mode . '] used to set nsKey=' .
71
                    $nsKey . ', itemKey=' . $itemKey
72
                );
73
        }
74 28
    }
75
76
    /**
77
     * Set a ByteStream into the cache under $itemKey for the namespace $nsKey.
78
     *
79
     * @see MODE_WRITE, MODE_APPEND
80
     *
81
     * @param string                 $nsKey
82
     * @param string                 $itemKey
83
     * @param Swift_OutputByteStream $os
84
     * @param int                    $mode
85
     *
86
     * @throws Swift_SwiftException
87
     */
88 6
    public function importFromByteStream($nsKey, $itemKey, Swift_OutputByteStream $os, $mode)
89
    {
90 6
        $this->_prepareCache($nsKey);
91
        switch ($mode) {
92 6
            case self::MODE_WRITE:
0 ignored issues
show
Coding Style introduced by
There must be a comment when fall-through is intentional in a non-empty case body
Loading history...
93 2
                $this->clearKey($nsKey, $itemKey);
94 6
            case self::MODE_APPEND:
95 6
                if (!$this->hasKey($nsKey, $itemKey)) {
96 4
                    $this->_contents[$nsKey][$itemKey] = '';
97 4
                }
98 6
                while (false !== $bytes = $os->read(8192)) {
99 6
                    $this->_contents[$nsKey][$itemKey] .= $bytes;
100 6
                }
101 6
                break;
102
            default:
103
                throw new Swift_SwiftException(
104
                    'Invalid mode [' . $mode . '] used to set nsKey=' . $nsKey . ', itemKey=' . $itemKey
105
                );
106
        }
107 6
    }
108
109
    /**
110
     * Provides a ByteStream which when written to, writes data to $itemKey.
111
     *
112
     * NOTE: The stream will always write in append mode.
113
     *
114
     * @param string                $nsKey
115
     * @param string                $itemKey
116
     * @param Swift_InputByteStream $writeThrough
117
     *
118
     * @return Swift_InputByteStream
119
     */
120 1 View Code Duplication
    public function getInputByteStream($nsKey, $itemKey, Swift_InputByteStream $writeThrough = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
121
    {
122 1
        $is = clone $this->_stream;
123
124 1
        $is->setKeyCache($this);
125 1
        $is->setNsKey($nsKey);
126 1
        $is->setItemKey($itemKey);
127
128 1
        if (isset($writeThrough)) {
129
            $is->setWriteThroughStream($writeThrough);
130
        }
131
132 1
        return $is;
133
    }
134
135
    /**
136
     * Get data back out of the cache as a string.
137
     *
138
     * @param string $nsKey
139
     * @param string $itemKey
140
     *
141
     * @return string
142
     */
143 19
    public function getString($nsKey, $itemKey)
144
    {
145 19
        $this->_prepareCache($nsKey);
146 19
        if ($this->hasKey($nsKey, $itemKey)) {
147 19
            return $this->_contents[$nsKey][$itemKey];
148
        }
149
    }
150
151
    /**
152
     * Get data back out of the cache as a ByteStream.
153
     *
154
     * @param string                $nsKey
155
     * @param string                $itemKey
156
     * @param Swift_InputByteStream $is      to write the data to
157
     */
158 2
    public function exportToByteStream($nsKey, $itemKey, Swift_InputByteStream $is)
159
    {
160 2
        $this->_prepareCache($nsKey);
161 2
        $is->write($this->getString($nsKey, $itemKey));
162 2
    }
163
164
    /**
165
     * Check if the given $itemKey exists in the namespace $nsKey.
166
     *
167
     * @param string $nsKey
168
     * @param string $itemKey
169
     *
170
     * @return bool
171
     */
172 32
    public function hasKey($nsKey, $itemKey)
173
    {
174 32
        $this->_prepareCache($nsKey);
175
176 32
        return isset($this->_contents[$nsKey][$itemKey]);
177
    }
178
179
    /**
180
     * Clear data for $itemKey in the namespace $nsKey if it exists.
181
     *
182
     * @param string $nsKey
183
     * @param string $itemKey
184
     */
185 21
    public function clearKey($nsKey, $itemKey)
186
    {
187 21
        unset($this->_contents[$nsKey][$itemKey]);
188 21
    }
189
190
    /**
191
     * Clear all data in the namespace $nsKey if it exists.
192
     *
193
     * @param string $nsKey
194
     */
195 19
    public function clearAll($nsKey)
196
    {
197 19
        unset($this->_contents[$nsKey]);
198 19
    }
199
200
    /**
201
     * Initialize the namespace of $nsKey if needed.
202
     *
203
     * @param string $nsKey
204
     */
205 32
    private function _prepareCache($nsKey)
206
    {
207 32
        if (!isset($this->_contents[$nsKey])) {
208 32
            $this->_contents[$nsKey] = array();
209 32
        }
210 32
    }
211
}
212