1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453
<?php
namespace Gedcomx\Rs\Client;
use Gedcomx\Gedcomx;
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Response;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Handler\CurlHandler;
/**
* The state factory is responsible for instantiating state classes from REST API responses.
*/
class StateFactory
{
/**
* The default production environment URI for the family tree.
*/
const PRODUCTION_URI = "https://api.familysearch.org/platform/collections/tree";
/**
* The default integration environment URI for the family tree.
*/
const INTEGRATION_URI = "https://api-integ.familysearch.org/platform/collections/tree";
/**
* The default production environment URI for the discovery endpoint.
*/
const PRODUCTION_DISCOVERY_URI = "https://api.familysearch.org/platform/collection";
/**
* The default integration environment URI for the discovery endpoint.
*/
const INTEGRATION_DISCOVERY_URI = "https://api-integ.familysearch.org/platform/collection";
/**
* @var boolean Are we in a production environment
*/
protected $production;
/**
* Constructs a new instance of this state factory, using the environment specified (defaults to integration).
*
* @param bool $production
*/
public function __construct($production = false)
{
$this->production = $production;
}
/**
* Returns a new collection state by invoking the specified URI and method, using the specified client.
*
* @param string $uri Optional URI
* @param \GuzzleHttp\Client $client The client to use.
* @param string $method The method.
*
* @return CollectionState The collection state.
*/
public function newCollectionState($uri = null, $method = "GET", Client $client = null)
{
if (!$client) {
$client = $this->defaultClient();
}
if ($uri == null) {
$uri = $this->production ? self::PRODUCTION_URI : self::INTEGRATION_URI;
}
/** @var Request $request */
$request = new Request($method, $uri, ['Accept' => Gedcomx::JSON_MEDIA_TYPE]);
return new CollectionState($client, $request, GedcomxApplicationState::send($client, $request), null, $this);
}
/**
* Returns a new collections state by invoking the specified URI and method, using the specified client.
*
* @param string $uri Optional URI
* @param \GuzzleHttp\Client $client The client to use.
* @param string $method The method.
*
* @return CollectionsState The collections state.
*/
public function newCollectionsState($uri = null, $method = "GET", Client $client = null)
{
if (!$client) {
$client = $this->defaultClient();
}
if ($uri == null) {
$uri = $this->production ? self::PRODUCTION_DISCOVERY_URI : self::INTEGRATION_DISCOVERY_URI;
}
/** @var Request $request */
$request = new Request($method, $uri, ['Accept' => Gedcomx::JSON_MEDIA_TYPE]);
return new CollectionsState($client, $request, GedcomxApplicationState::send($client, $request), null, $this);
}
/**
* Returns a new discovery state by invoking the specified URI and method, using the specified client.
*
* @param string $uri Optional URI
* @param \GuzzleHttp\Client $client The client to use.
* @param string $method The method.
*
* @return CollectionState The collection state.
*/
public function newDiscoveryState($uri = null, $method = "GET", Client $client = null)
{
if (!$client) {
$client = $this->defaultClient();
}
if ($uri == null) {
$uri = $this->production ? self::PRODUCTION_DISCOVERY_URI : self::INTEGRATION_DISCOVERY_URI;
}
/** @var Request $request */
$request = new Request($method, $uri, ['Accept' => Gedcomx::JSON_MEDIA_TYPE]);
return new CollectionState($client, $request, GedcomxApplicationState::send($client, $request), null, $this);
}
/**
* Loads the default client for executing REST API requests.
*
* @return \GuzzleHttp\Client
*/
protected function defaultClient()
{
return new Client([
'handler' => HandlerStack::create(new CurlHandler()),
'http_errors' => false
]);
}
/**
* Returns a new person state by invoking the specified URI and method, using the specified client.
*
* @param string $uri The URI to the person.
* @param \GuzzleHttp\Client $client The client to use.
* @param string $method The method.
*
* @return PersonState The person state.
*/
public function newPersonState($uri, Client $client = null, $method = "GET")
{
if (!$client) {
$client = $this->defaultClient();
}
/** @var Request $request */
$request = new Request($method, $uri, ['Accept' => Gedcomx::JSON_MEDIA_TYPE]);
return new PersonState($client, $request, GedcomxApplicationState::send($client, $request), null, $this);
}
/**
* Dynamically creates and returns a state instance for the specified class, if a state builder is defined.
*
* @param string $class The name of the state class to create
* @param \GuzzleHttp\Client $client
* @param \GuzzleHttp\Psr7\Request $request
* @param \GuzzleHttp\Psr7\Response $response
* @param string $accessToken The access token for this session
*
* @return mixed
*/
public function createState($class, Client $client, Request $request, Response $response, $accessToken)
{
$functionName = "build{$class}";
return $this->$functionName($client, $request, $response, $accessToken);
}
/**
* Builds a new collection state from the specified client, request, response, and access token.
*
* @param \GuzzleHttp\Client $client
* @param \GuzzleHttp\Psr7\Request $request
* @param \GuzzleHttp\Psr7\Response $response
* @param string $accessToken The access token for this session
*
* @return \Gedcomx\Rs\Client\SourceDescriptionsState
*/
protected function buildCollectionState(Client $client, Request $request, Response $response, $accessToken)
{
return new CollectionState($client, $request, $response, $accessToken, $this);
}
/**
* Builds a new collections state from the specified client, request, response, and access token.
*
* @param \GuzzleHttp\Client $client
* @param \GuzzleHttp\Psr7\Request $request
* @param \GuzzleHttp\Psr7\Response $response
* @param string $accessToken The access token for this session
*
* @return \Gedcomx\Rs\Client\SourceDescriptionsState
*/
protected function buildCollectionsState( Client $client, Request $request, Response $response, $accessToken ){
return new CollectionsState( $client, $request, $response, $accessToken, $this );
}
/**
* Builds a new source descriptions state from the specified client, request, response, and access token.
*
* @param \GuzzleHttp\Client $client
* @param \GuzzleHttp\Psr7\Request $request
* @param \GuzzleHttp\Psr7\Response $response
* @param string $accessToken The access token for this session
*
* @return \Gedcomx\Rs\Client\SourceDescriptionsState
*/
protected function buildSourceDescriptionsState(Client $client, Request $request, Response $response, $accessToken)
{
return new SourceDescriptionsState($client, $request, $response, $accessToken, $this);
}
/**
* Builds a new source description state from the specified client, request, response, and access token.
*
* @param \GuzzleHttp\Client $client
* @param \GuzzleHttp\Psr7\Request $request
* @param \GuzzleHttp\Psr7\Response $response
* @param string $accessToken The access token for this session
*
* @return \Gedcomx\Rs\Client\SourceDescriptionsState
*/
protected function buildSourceDescriptionState(Client $client, Request $request, Response $response, $accessToken)
{
return new SourceDescriptionState($client, $request, $response, $accessToken, $this);
}
/**
* Builds a new persons state from the specified client, request, response, and access token.
*
* @param \GuzzleHttp\Client $client
* @param \GuzzleHttp\Psr7\Request $request
* @param \GuzzleHttp\Psr7\Response $response
* @param string $accessToken The access token for this session
*
* @return \Gedcomx\Rs\Client\PersonsState
*/
protected function buildPersonsState(Client $client, Request $request, Response $response, $accessToken)
{
return new PersonsState($client, $request, $response, $accessToken, $this);
}
/**
* Builds a new person children state from the specified client, request, response, and access token.
*
* @param \GuzzleHttp\Client $client
* @param \GuzzleHttp\Psr7\Request $request
* @param \GuzzleHttp\Psr7\Response $response
* @param string $accessToken The access token for this session
*
* @return \Gedcomx\Rs\Client\PersonChildrenState
*/
protected function buildPersonChildrenState(Client $client, Request $request, Response $response, $accessToken)
{
return new PersonChildrenState($client, $request, $response, $accessToken, $this);
}
/**
* Builds a new person state from the specified client, request, response, and access token.
*
* @param \GuzzleHttp\Client $client
* @param \GuzzleHttp\Psr7\Request $request
* @param \GuzzleHttp\Psr7\Response $response
* @param string $accessToken The access token for this session
*
* @return \Gedcomx\Rs\Client\PersonState
*/
protected function buildPersonState(Client $client, Request $request, Response $response, $accessToken)
{
return new PersonState($client, $request, $response, $accessToken, $this);
}
/**
* Builds a new person parents state from the specified client, request, response, and access token.
*
* @param \GuzzleHttp\Client $client
* @param \GuzzleHttp\Psr7\Request $request
* @param \GuzzleHttp\Psr7\Response $response
* @param string $accessToken The access token for this session
*
* @return \Gedcomx\Rs\Client\PersonParentsState
*/
protected function buildPersonParentsState(Client $client, Request $request, Response $response, $accessToken)
{
return new PersonParentsState($client, $request, $response, $accessToken, $this);
}
/**
* Builds a new person spouses state from the specified client, request, response, and access token.
*
* @param \GuzzleHttp\Client $client
* @param \GuzzleHttp\Psr7\Request $request
* @param \GuzzleHttp\Psr7\Response $response
* @param string $accessToken The access token for this session
*
* @return \Gedcomx\Rs\Client\PersonSpousesState
*/
protected function buildPersonSpousesState(Client $client, Request $request, Response $response, $accessToken)
{
return new PersonSpousesState($client, $request, $response, $accessToken, $this);
}
/**
* Builds a new ancestry results state from the specified client, request, response, and access token.
*
* @param \GuzzleHttp\Client $client
* @param \GuzzleHttp\Psr7\Request $request
* @param \GuzzleHttp\Psr7\Response $response
* @param string $accessToken The access token for this session
*
* @return \Gedcomx\Rs\Client\AncestryResultsState
*/
protected function buildAncestryResultsState(Client $client, Request $request, Response $response, $accessToken)
{
return new AncestryResultsState($client, $request, $response, $accessToken, $this);
}
/**
* Builds a new descendancy results state from the specified client, request, response, and access token.
*
* @param \GuzzleHttp\Client $client
* @param \GuzzleHttp\Psr7\Request $request
* @param \GuzzleHttp\Psr7\Response $response
* @param string $accessToken The access token for this session
*
* @return \Gedcomx\Rs\Client\AncestryResultsState
*/
protected function buildDescendancyResultsState(Client $client, Request $request, Response $response, $accessToken)
{
return new DescendancyResultsState($client, $request, $response, $accessToken, $this);
}
/**
* Builds a new person search results state from the specified client, request, response, and access token.
*
* @param \GuzzleHttp\Client $client
* @param \GuzzleHttp\Psr7\Request $request
* @param \GuzzleHttp\Psr7\Response $response
* @param string $accessToken The access token for this session
*
* @return \Gedcomx\Rs\Client\PersonSearchResultsState
*/
protected function buildPersonSearchResultsState(Client $client, Request $request, Response $response, $accessToken)
{
return new PersonSearchResultsState($client, $request, $response, $accessToken, $this);
}
/**
* Builds a new record state from the specified client, request, response, and access token.
*
* @param \GuzzleHttp\Client $client
* @param \GuzzleHttp\Psr7\Request $request
* @param \GuzzleHttp\Psr7\Response $response
* @param string $accessToken The access token for this session
*
* @return \Gedcomx\Rs\Client\RecordState
*/
protected function buildRecordState(Client $client, Request $request, Response $response, $accessToken)
{
return new RecordState($client, $request, $response, $accessToken, $this);
}
/**
* Builds a new relationship state from the specified client, request, response, and access token.
*
* @param \GuzzleHttp\Client $client
* @param \GuzzleHttp\Psr7\Request $request
* @param \GuzzleHttp\Psr7\Response $response
* @param string $accessToken The access token for this session
*
* @return \Gedcomx\Rs\Client\RecordState
*/
protected function buildRelationshipState(Client $client, Request $request, Response $response, $accessToken)
{
return new RelationshipState($client, $request, $response, $accessToken, $this);
}
/**
* Builds a new agent state from the specified client, request, response, and access token.
*
* @param \GuzzleHttp\Client $client
* @param \GuzzleHttp\Psr7\Request $request
* @param \GuzzleHttp\Psr7\Response $response
* @param string $accessToken The access token for this session
*
* @return \Gedcomx\Rs\Client\RecordState
*/
protected function buildAgentState(Client $client, Request $request, Response $response, $accessToken)
{
return new AgentState($client, $request, $response, $accessToken, $this);
}
/**
* Builds a new place search state from the specified client, request, response, and access token.
*
* @param \GuzzleHttp\Client $client
* @param \GuzzleHttp\Psr7\Request $request
* @param \GuzzleHttp\Psr7\Response $response
* @param string $accessToken The access token for this session
*
* @return \Gedcomx\Rs\Client\RecordState
*/
protected function buildPlaceSearchResultState(Client $client, Request $request, Response $response, $accessToken)
{
return new PlaceSearchResultState($client, $request, $response, $accessToken, $this);
}
/**
* Builds a new place description state from the specified client, request, response, and access token.
*
* @param \GuzzleHttp\Client $client
* @param \GuzzleHttp\Psr7\Request $request
* @param \GuzzleHttp\Psr7\Response $response
* @param string $accessToken The access token for this session
*
* @return \Gedcomx\Rs\Client\PlaceDescriptionState
*/
protected function buildPlaceDescriptionState(Client $client, Request $request, Response $response, $accessToken)
{
return new PlaceDescriptionState($client, $request, $response, $accessToken, $this);
}
/**
* Builds a new vocab element list state from the specified client, request, response, and access token.
*
* @param \GuzzleHttp\Client $client
* @param \GuzzleHttp\Psr7\Request $request
* @param \GuzzleHttp\Psr7\Response $response
* @param string $accessToken The access token for this session
*
* @return \Gedcomx\Rs\Client\PlaceDescriptionState
*/
protected function buildVocabElementListState(Client $client, Request $request, Response $response, $accessToken)
{
return new VocabElementListState($client, $request, $response, $accessToken, $this);
}
/**
* Builds a new vocab element state from the specified client, request, response, and access token.
*
* @param \GuzzleHttp\Client $client
* @param \GuzzleHttp\Psr7\Request $request
* @param \GuzzleHttp\Psr7\Response $response
* @param string $accessToken The access token for this session
*
* @return \Gedcomx\Rs\Client\PlaceDescriptionState
*/
protected function buildVocabElementState(Client $client, Request $request, Response $response, $accessToken)
{
return new VocabElementState($client, $request, $response, $accessToken, $this);
}
}