Passed
Branch 1.0.0 (a1adee)
by Zaahid
08:34
created

PartStreamRegistry::attachEncodingFilterToStream()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 17
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 17
rs 9.2
cc 4
eloc 14
nc 4
nop 2
1
<?php
2
/**
3
 * This file is part of the ZBateson\MailMimeParser project.
4
 *
5
 * @license http://opensource.org/licenses/bsd-license.php BSD
6
 */
7
namespace ZBateson\MailMimeParser\Stream;
8
9
/**
10
 * Factory class for PartStream objects and registration class for Message
11
 * handles.
12
 * 
13
 * PartStreamRegistry is used for \ZBateson\MailMimeParser\Message\MessageParser to
14
 * register Message stream handles for opening with PartStreams, and to open
15
 * file handles for specific mime parts of a message.  The PartStreamRegistry
16
 * maintains a list of opened resources, closing them either when unregistering
17
 * a Message or on destruction.
18
 *
19
 * @author Zaahid Bateson
20
 */
21
class PartStreamRegistry
22
{
23
    /**
24
     * @var array Array of handles, with message IDs as keys.
25
     */
26
    private $registeredHandles;
27
    
28
    /**
29
     * @var int[] number of registered part stream handles with message IDs as
30
     * keys
31
     */
32
    private $numRefCountsForHandles;
33
34
    /**
35
     * Registers an ID for the passed resource handle.
36
     * 
37
     * @param string $id
38
     * @param resource $handle
39
     */
40 1
    public function register($id, $handle)
41
    {
42 1
        if (!isset($this->registeredHandles[$id])) {
43 1
            $this->registeredHandles[$id] = $handle;
44 1
            $this->numRefCountsForHandles[$id] = 0;
45 1
        }
46 1
    }
47
48
    /**
49
     * Unregisters the given message ID and closes the associated resource
50
     * handle.
51
     * 
52
     * @param string $id
53
     */
54 1
    protected function unregister($id)
55
    {
56 1
        fclose($this->registeredHandles[$id]);
57 1
        unset($this->registeredHandles[$id], $this->registeredPartStreamHandles[$id]);
58 1
    }
59
    
60
    /**
61
     * Increases the reference count for streams using the resource handle
62
     * associated with the message id.
63
     * 
64
     * @param int $messageId
65
     */
66 1
    public function increaseHandleRefCount($messageId)
67
    {
68 1
        $this->numRefCountsForHandles[$messageId] += 1;
69 1
    }
70
    
71
    /**
72
     * Decreases the reference count for streams using the resource handle
73
     * associated with the message id.  Once the reference count hits 0,
74
     * unregister is called.
75
     * 
76
     * @param int $messageId
77
     */
78 1
    public function decreaseHandleRefCount($messageId)
79
    {
80 1
        $this->numRefCountsForHandles[$messageId] -= 1;
81 1
        if ($this->numRefCountsForHandles[$messageId] === 0) {
82 1
            $this->unregister($messageId);
83 1
        }
84 1
    }
85
86
    /**
87
     * Returns the resource handle with the passed $id.
88
     * 
89
     * @param string $id
90
     * @return resource
91
     */
92 1
    public function get($id)
93
    {
94 1
        if (!isset($this->registeredHandles[$id])) {
95 1
            return null;
96
        }
97 1
        return $this->registeredHandles[$id];
98
    }
99
}
100