1<?php
2
3/**
4 * Licensed to Jasig under one or more contributor license
5 * agreements. See the NOTICE file distributed with this work for
6 * additional information regarding copyright ownership.
7 *
8 * Jasig licenses this file to you under the Apache License,
9 * Version 2.0 (the "License"); you may not use this file except in
10 * compliance with the License. You may obtain a copy of the License at:
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 *
20 * PHP Version 5
21 *
22 * @file     CAS/Request/AbstractRequest.php
23 * @category Authentication
24 * @package  PhpCAS
25 * @author   Adam Franco <afranco@middlebury.edu>
26 * @license  http://www.apache.org/licenses/LICENSE-2.0  Apache License 2.0
27 * @link     https://wiki.jasig.org/display/CASC/phpCAS
28 */
29
30/**
31 * This interface defines a class library for performing multiple web requests
32 * in batches. Implementations of this interface may perform requests serially
33 * or in parallel.
34 *
35 * @class    CAS_Request_CurlMultiRequest
36 * @category Authentication
37 * @package  PhpCAS
38 * @author   Adam Franco <afranco@middlebury.edu>
39 * @license  http://www.apache.org/licenses/LICENSE-2.0  Apache License 2.0
40 * @link     https://wiki.jasig.org/display/CASC/phpCAS
41 */
42class CAS_Request_CurlMultiRequest
43implements CAS_Request_MultiRequestInterface
44{
45    private $_requests = array();
46    private $_sent = false;
47
48    /*********************************************************
49     * Add Requests
50    *********************************************************/
51
52    /**
53     * Add a new Request to this batch.
54     * Note, implementations will likely restrict requests to their own concrete
55     * class hierarchy.
56     *
57     * @param CAS_Request_RequestInterface $request reqest to add
58     *
59     * @return void
60     * @throws CAS_OutOfSequenceException If called after the Request has been sent.
61     * @throws CAS_InvalidArgumentException If passed a Request of the wrong
62     * implmentation.
63     */
64    public function addRequest (CAS_Request_RequestInterface $request)
65    {
66        if ($this->_sent) {
67            throw new CAS_OutOfSequenceException(
68                'Request has already been sent cannot '.__METHOD__
69            );
70        }
71        if (!$request instanceof CAS_Request_CurlRequest) {
72            throw new CAS_InvalidArgumentException(
73                'As a CAS_Request_CurlMultiRequest, I can only work with CAS_Request_CurlRequest objects.'
74            );
75        }
76
77        $this->_requests[] = $request;
78    }
79
80    /**
81     * Retrieve the number of requests added to this batch.
82     *
83     * @return int number of request elements
84     * @throws CAS_OutOfSequenceException if the request has already been sent
85     */
86    public function getNumRequests()
87    {
88        if ($this->_sent) {
89            throw new CAS_OutOfSequenceException(
90                'Request has already been sent cannot '.__METHOD__
91            );
92        }
93        return count($this->_requests);
94    }
95
96    /*********************************************************
97     * 2. Send the Request
98    *********************************************************/
99
100    /**
101     * Perform the request. After sending, all requests will have their
102     * responses poulated.
103     *
104     * @return bool TRUE on success, FALSE on failure.
105     * @throws CAS_OutOfSequenceException If called multiple times.
106     */
107    public function send ()
108    {
109        if ($this->_sent) {
110            throw new CAS_OutOfSequenceException(
111                'Request has already been sent cannot send again.'
112            );
113        }
114        if (!count($this->_requests)) {
115            throw new CAS_OutOfSequenceException(
116                'At least one request must be added via addRequest() before the multi-request can be sent.'
117            );
118        }
119
120        $this->_sent = true;
121
122        // Initialize our handles and configure all requests.
123        $handles = array();
124        $multiHandle = curl_multi_init();
125        foreach ($this->_requests as $i => $request) {
126            $handle = $request->initAndConfigure();
127            curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
128            $handles[$i] = $handle;
129            curl_multi_add_handle($multiHandle, $handle);
130        }
131
132        // Execute the requests in parallel.
133        do {
134            curl_multi_exec($multiHandle, $running);
135        } while ($running > 0);
136
137        // Populate all of the responses or errors back into the request objects.
138        foreach ($this->_requests as $i => $request) {
139            $buf = curl_multi_getcontent($handles[$i]);
140            $request->_storeResponseBody($buf);
141            curl_multi_remove_handle($multiHandle, $handles[$i]);
142            curl_close($handles[$i]);
143        }
144
145        curl_multi_close($multiHandle);
146    }
147}
148