Completed
Pull Request — master (#49)
by Joel
07:29
created

MessageCloner   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 18
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 1
c 1
b 0
f 1
lcom 0
cbo 2
dl 0
loc 18
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A cloneMessage() 0 4 1
1
<?php
2
3
namespace Http\Message;
4
5
use Psr\Http\Message\MessageInterface;
6
7
/**
8
 * Allow to clone request and response.
9
 *
10
 * A message cloner is only necessary when you also want to duplicate the stream of a request or a response, as by
11
 * default object returned by `clone $message` call will have the same stream as the cloned one, so reading the body of
12
 * one of the message will affect the other.
13
 */
14
class MessageCloner
15
{
16
    /**
17
     * Clone a message.
18
     *
19
     * When cloning you have to be careful that the original stream is rewindable. If not the original stream will be
20
     * readed and cannot be readed one more time. To avoid this behavior, when the original stream is not seekable, you
21
     * can clone the cloned message, and replace the original message with one of the clone, as there are always rewindable.
22
     *
23
     * @param MessageInterface $message
24
     *
25
     * @return MessageInterface|static
26
     */
27
    public function cloneMessage(MessageInterface $message)
28
    {
29
        return $message->withBody(new MemoryClonedStream($message->getBody()));
30
    }
31
}
32