| 1 |  |  | <?php namespace CalDAVClient\Facade\Utils; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * Copyright 2017 OpenStack Foundation | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * Licensed under the Apache License, Version 2.0 (the "License"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * you may not use this file except in compliance with the License. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * You may obtain a copy of the License at | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * http://www.apache.org/licenses/LICENSE-2.0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * Unless required by applicable law or agreed to in writing, software | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * distributed under the License is distributed on an "AS IS" BASIS, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * See the License for the specific language governing permissions and | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * limitations under the License. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  **/ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use GuzzleHttp\Psr7\Request; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * Class RequestFactory | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  * @package CalDAVClient\Facade\Utils | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | final class RequestFactory | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      * @param string $http_method | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      * @param array $params | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     static private function createHeadersFor($http_method, array $params = []){ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |         switch ($http_method){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |             case HttpMethods::PropFind: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |             case HttpMethods::Options: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |             case HttpMethods::Report: | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |             { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |                 return  [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |                     Headers::Depth        => $params[0], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |                     Headers::Prefer       => "return-minimal", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |                     Headers::ContentType  => ContentTypes::Xml | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |                 ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |             break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |             case HttpMethods::Delete: | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |             { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |                 return [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |                     Headers::IfMatch =>  $params[0], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |                 ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |             break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |             case HttpMethods::MakeCalendar: | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |             { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |                 return [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |                     Headers::ContentType  => ContentTypes::Xml | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |                 ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |             break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |             case HttpMethods::Put: | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |             { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |                $etag = $params[0]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |                if(empty($etag)){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |                    return [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |                        Headers::ContentType  => ContentTypes::Calendar, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |                        Headers::IfNotMatch   => '*', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |                    ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |                } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |                return [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |                    Headers::ContentType  => ContentTypes::Calendar, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |                    Headers::IfMatch      => $etag | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |                ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |             break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         return []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     * @param string $url | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     * @param string $body | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     * @param int $depth | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |     * @return Request | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     static public function createPropFindRequest($url , $body, $depth = 1){ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         return new Request | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         ( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |             HttpMethods::PropFind, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |             $url , | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |             self::createHeadersFor(HttpMethods::PropFind, [$depth]), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |             $body | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |      * @param string $url | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |      * @param string $body | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |      * @return Request | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 94 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 95 |  |  |     static public function createMakeCalendarRequest($url , $body){ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 96 |  |  |         return new Request | 
            
                                                                        
                            
            
                                    
            
            
                | 97 |  |  |         ( | 
            
                                                                        
                            
            
                                    
            
            
                | 98 |  |  |             HttpMethods::MakeCalendar, | 
            
                                                                        
                            
            
                                    
            
            
                | 99 |  |  |             $url, | 
            
                                                                        
                            
            
                                    
            
            
                | 100 |  |  |             self::createHeadersFor(HttpMethods::MakeCalendar), | 
            
                                                                        
                            
            
                                    
            
            
                | 101 |  |  |             $body | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |      * @param string $url | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |      * @param int $depth | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |      * @return Request | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |     static public function createOptionsRequest($url, $depth = 1){ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |         return new Request | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         ( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |             HttpMethods::Options, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |             $url, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |             self::createHeadersFor(HttpMethods::Options, [$depth]) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |      * @param string $url | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |      * @param string $body | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |      * @param int $depth | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |      * @return Request | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |     static public function createReportRequest($url , $body, $depth = 1){ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |         return new Request | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |         ( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |             HttpMethods::Report, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |             $url, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |             self::createHeadersFor(HttpMethods::Report, [$depth]), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |             $body | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |      * @param string $url | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |      * @param string $etag | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |      * @return Request | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |     static public function createDeleteRequest($url , $etag){ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |         return new Request | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |         ( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |             HttpMethods::Delete, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |             $url, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |             self::createHeadersFor(HttpMethods::Delete, [$etag]) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |      * @param string $url | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |      * @return Request | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |     static public function createGetRequest($url){ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |         return new Request | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |         ( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |             HttpMethods::Get, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |             $url, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |             self::createHeadersFor(HttpMethods::Get) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |      * @param string $url | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |      * @param string $body | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |      * @param string $etag | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |      * @return Request | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |     static public function createPutRequest($url, $body, $etag = null){ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |         return new Request | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |         ( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |             HttpMethods::Put, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |             $url, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |             self::createHeadersFor(HttpMethods::Put, [$etag]), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |             $body | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 177 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 178 |  |  | } |