Completed
Push — master ( 579725...36331d )
by Renato
07:19
created

src/VCR/LibraryHooks/SoapHook.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace VCR\LibraryHooks;
4
5
use VCR\Util\Assertion;
6
use VCR\VCRException;
7
use VCR\Request;
8
use VCR\CodeTransform\AbstractCodeTransform;
9
use VCR\Util\StreamProcessor;
10
11
/**
12
 * Library hook for curl functions.
13
 */
14
class SoapHook implements LibraryHook
15
{
16
    /**
17
     * @var callable
18
     */
19
    private static $requestCallback;
20
21
    /**
22
     * @var string
23
     */
24
    private $status = self::DISABLED;
25
26
    /**
27
     * @var AbstractCodeTransform
28
     */
29
    private $codeTransformer;
30
31
    /**
32
     * @var \VCR\Util\StreamProcessor
33
     */
34
    private $processor;
35
36
    /**
37
     * Creates a SOAP hook instance.
38
     *
39
     * @param AbstractCodeTransform  $codeTransformer
40
     * @param StreamProcessor $processor
41
     *
42
     * @throws \BadMethodCallException in case the Soap extension is not installed.
43
     */
44 28
    public function __construct(AbstractCodeTransform $codeTransformer, StreamProcessor $processor)
45
    {
46 28
        if (!class_exists('\SoapClient')) {
47
            throw new \BadMethodCallException('For soap support you need to install the soap extension.');
48
        }
49
50 28
        if (!class_exists('\DOMDocument')) {
51
            throw new \BadMethodCallException('For soap support you need to install the xml extension.');
52
        }
53
54 28
        $this->processor = $processor;
55 28
        $this->codeTransformer = $codeTransformer;
56 28
    }
57
58
    /**
59
     * @param string $request
60
     * @param string $location
61
     * @param string $action
62
     * @param integer $version
63
     * @param int $one_way
64
     *
65
     * @throws \VCR\VCRException It this method is called although VCR is disabled.
66
     *
67
     * @return string SOAP response.
68
     */
69 35
    public function doRequest($request, $location, $action, $version, $one_way = 0, $options = array())
0 ignored issues
show
The parameter $one_way is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
70
    {
71 35
        if ($this->status === self::DISABLED) {
72
            throw new VCRException('Hook must be enabled.', VCRException::LIBRARY_HOOK_DISABLED);
73
        }
74
75 35
        $vcrRequest = new Request('POST', $location);
76
77 35
        if ($version === SOAP_1_1) {
78 14
            $vcrRequest->setHeader('Content-Type', 'text/xml; charset=utf-8;');
79 14
            $vcrRequest->setHeader('SOAPAction', $action);
80 10
        } else { // >= SOAP_1_2
81 21
            $vcrRequest->setHeader(
82 21
                'Content-Type',
83 21
                sprintf('application/soap+xml; charset=utf-8; action="%s"', $action)
84 15
            );
85
        }
86
87 35
        $vcrRequest->setBody($request);
88
89 35
        if (!empty($options['login'])) {
90
            $vcrRequest->setAuthorization($options['login'], $options['password']);
91
        }
92
93
        /* @var \VCR\Response $response */
94 35
        $requestCallback = self::$requestCallback;
95 35
        $response = $requestCallback($vcrRequest);
96
97 35
        return $response->getBody();
98
    }
99
100
    /**
101
     * @inheritDoc
102
     */
103 35
    public function enable(\Closure $requestCallback)
104
    {
105 35
        Assertion::isCallable($requestCallback, 'No valid callback for handling requests defined.');
106 35
        self::$requestCallback = $requestCallback;
107
108 35
        if ($this->status == self::ENABLED) {
109 7
            return;
110
        }
111
112 35
        $this->codeTransformer->register();
113 35
        $this->processor->appendCodeTransformer($this->codeTransformer);
114 35
        $this->processor->intercept();
115
116 35
        $this->status = self::ENABLED;
117 35
    }
118
119
    /**
120
     * @inheritDoc
121
     */
122 21
    public function disable()
123
    {
124 21
        if (!$this->isEnabled()) {
125
            return;
126
        }
127
128 21
        self::$requestCallback = null;
129
130 21
        $this->status = self::DISABLED;
131 21
    }
132
133
    /**
134
     * @inheritDoc
135
     */
136 35
    public function isEnabled()
137
    {
138 35
        return $this->status == self::ENABLED;
139
    }
140
141
    /**
142
     * Cleanup.
143
     *
144
     * @return  void
145
     */
146 49
    public function __destruct()
147
    {
148 49
        self::$requestCallback = null;
149 49
    }
150
}
151