Completed
Pull Request — 5.x (#16)
by Lars
05:10
created

Swift_KeyCache_ArrayKeyCache::hasKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4286
cc 1
eloc 3
nc 1
nop 2
crap 1
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
                }
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 4
            case self::MODE_APPEND:
95 6
                if (!$this->hasKey($nsKey, $itemKey)) {
96 4
                    $this->_contents[$nsKey][$itemKey] = '';
97
                }
98 6
                while (false !== $bytes = $os->read(8192)) {
99 6
                    $this->_contents[$nsKey][$itemKey] .= $bytes;
100
                }
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 1
        $is->setKeyCache($this);
124 1
        $is->setNsKey($nsKey);
125 1
        $is->setItemKey($itemKey);
126 1
        if (isset($writeThrough)) {
127
            $is->setWriteThroughStream($writeThrough);
128
        }
129
130 1
        return $is;
131
    }
132
133
    /**
134
     * Get data back out of the cache as a string.
135
     *
136
     * @param string $nsKey
137
     * @param string $itemKey
138
     *
139
     * @return string
140
     */
141 19
    public function getString($nsKey, $itemKey)
142
    {
143 19
        $this->_prepareCache($nsKey);
144 19
        if ($this->hasKey($nsKey, $itemKey)) {
145 19
            return $this->_contents[$nsKey][$itemKey];
146
        }
147
    }
148
149
    /**
150
     * Get data back out of the cache as a ByteStream.
151
     *
152
     * @param string                $nsKey
153
     * @param string                $itemKey
154
     * @param Swift_InputByteStream $is      to write the data to
155
     */
156 2
    public function exportToByteStream($nsKey, $itemKey, Swift_InputByteStream $is)
157
    {
158 2
        $this->_prepareCache($nsKey);
159 2
        $is->write($this->getString($nsKey, $itemKey));
160 2
    }
161
162
    /**
163
     * Check if the given $itemKey exists in the namespace $nsKey.
164
     *
165
     * @param string $nsKey
166
     * @param string $itemKey
167
     *
168
     * @return bool
169
     */
170 32
    public function hasKey($nsKey, $itemKey)
171
    {
172 32
        $this->_prepareCache($nsKey);
173
174 32
        return array_key_exists($itemKey, $this->_contents[$nsKey]);
175
    }
176
177
    /**
178
     * Clear data for $itemKey in the namespace $nsKey if it exists.
179
     *
180
     * @param string $nsKey
181
     * @param string $itemKey
182
     */
183 21
    public function clearKey($nsKey, $itemKey)
184
    {
185 21
        unset($this->_contents[$nsKey][$itemKey]);
186 21
    }
187
188
    /**
189
     * Clear all data in the namespace $nsKey if it exists.
190
     *
191
     * @param string $nsKey
192
     */
193 19
    public function clearAll($nsKey)
194
    {
195 19
        unset($this->_contents[$nsKey]);
196 19
    }
197
198
    /**
199
     * Initialize the namespace of $nsKey if needed.
200
     *
201
     * @param string $nsKey
202
     */
203 32
    private function _prepareCache($nsKey)
204
    {
205 32
        if (!array_key_exists($nsKey, $this->_contents)) {
206 32
            $this->_contents[$nsKey] = array();
207
        }
208 32
    }
209
}
210