22
33namespace DPRMC \WebBot ;
44
5+ use DPRMC \WebBot \Exceptions \WebBot \ResponseObjectNotSetForIndex ;
56use GuzzleHttp \Client ;
7+ use GuzzleHttp \Psr7 \Response ;
8+
69
710class WebBot {
11+
12+
813 /**
9- * @var \GuzzleHttp\Client The Client that sends the HTTP requests.
14+ * @var \GuzzleHttp\Client $client
1015 */
1116 protected $ client ;
1217
18+
1319 /**
1420 * @var array $steps An array of Step objects where the index is the step name.
1521 */
@@ -25,6 +31,11 @@ class WebBot {
2531 */
2632 protected $ responses ;
2733
34+
35+ public static function instance () {
36+ return new static ;
37+ }
38+
2839 /**
2940 * WebBot constructor.
3041 * @link http://docs.guzzlephp.org/en/stable/quickstart.html#cookies Explains the cookies setting in the Client.
@@ -40,13 +51,16 @@ public function __construct() {
4051 *
4152 * @param string $name
4253 * @param \DPRMC\WebBot\Step $step
54+ * @param boolean $debug
4355 *
4456 * @return $this Required for Fluent interface.
4557 */
46- public function addStep ( $ name , $ step ) {
58+ public function addStep ( $ name , $ step , $ debug = false ) {
59+ $ step ->setDebug ( $ debug );
4760 $ this ->steps [ $ name ] = $ step ;
4861 $ this ->initResponseElement ( $ name );
4962
63+
5064 return $ this ;
5165 }
5266
@@ -65,6 +79,12 @@ public function run() {
6579 return $ this ;
6680 }
6781
82+ protected function initStepResultElements () {
83+ foreach ( $ this ->steps as $ name => $ step ):
84+ $ this ->initStepResultElement ( $ name );
85+ endforeach ;
86+ }
87+
6888 /**
6989 * @param string $name
7090 * @param \DPRMC\WebBot\Step $step
@@ -96,20 +116,25 @@ protected function initStepResultElement( $name ) {
96116 * @param string $name
97117 */
98118 protected function initResponseElement ( $ name ) {
99- $ this ->responses [ $ name ] = null ;
119+ $ this ->responses [ $ name ] = new Response () ;
100120 }
101121
102122 /**
103123 * @param string $name The step name of the Response who's body you want.
104124 *
105125 * @return \GuzzleHttp\Psr7\Stream|\Psr\Http\Message\StreamInterface
126+ * @throws \DPRMC\WebBot\Exceptions\WebBot\ResponseObjectNotSetForIndex
106127 */
107128 public function getResponseBody ( $ name ) {
108129 /**
109130 * @var \GuzzleHttp\Psr7\Response $response
110131 */
111132 $ response = $ this ->responses [ $ name ];
112133
134+ if ( Response::class !== get_class ( $ response ) ):
135+ throw new ResponseObjectNotSetForIndex ( "The response object was not set for the step labeled: " . $ name );
136+ endif ;
137+
113138 return $ response ->getBody ();
114139 }
115140
@@ -121,4 +146,11 @@ public function getResponseBody( $name ) {
121146 public function getStepResult ( $ name ) {
122147 return $ this ->stepResults [ $ name ];
123148 }
149+
150+ /**
151+ * @return array An array of StepResult objects.
152+ */
153+ public function getStepResults () {
154+ return $ this ->stepResults ;
155+ }
124156}
0 commit comments