1: <?php
2: namespace Worldline\Connect\Sdk;
3:
4: /**
5: * Class BodyHandler
6: * A utility class that can be used to support binary responses. Its handleBodyPart method can be used as
7: * callback to methods that require a body handler callable.
8: *
9: * @package Worldline\Connect\Sdk
10: */
11: class BodyHandler
12: {
13: /** @var bool */
14: private $initialized = false;
15:
16: /**
17: * Initializes this body handler if not done yet, then calls doHandleBodyPart.
18: * @param string $bodyPart
19: * @param array $headers
20: */
21: final public function handleBodyPart($bodyPart, $headers)
22: {
23: if (!$this->initialized) {
24: $this->initialize($headers);
25: $this->initialized = true;
26: }
27: $this->doHandleBodyPart($bodyPart);
28: }
29:
30: /**
31: * Calls doCleanup, then marks this body handler as not initialized.
32: * Afterwards this instance can be reused again.
33: */
34: final public function close()
35: {
36: $this->doCleanup();
37: $this->initialized = false;
38: }
39:
40: /**
41: * Can be used to initialize this body handler based on the given headers.
42: * The default implementation does nothing.
43: * @param array $headers
44: */
45: protected function initialize($headers)
46: {
47: }
48:
49: /**
50: * Can be used to handle a single body part.
51: * The default implementation does nothing.
52: * @param string $bodyPart
53: */
54: protected function doHandleBodyPart($bodyPart)
55: {
56: }
57:
58: /**
59: * Can be used to do cleanup resources allocated by this body handler.
60: * The default implementation does nothing.
61: */
62: protected function doCleanup()
63: {
64: }
65: }
66: