vendor/sentry/sentry/src/ClientBuilder.php line 152

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Sentry;
  4. use Http\Discovery\Psr17FactoryDiscovery;
  5. use Psr\Log\LoggerInterface;
  6. use Sentry\HttpClient\HttpClientFactory;
  7. use Sentry\Serializer\RepresentationSerializerInterface;
  8. use Sentry\Serializer\SerializerInterface;
  9. use Sentry\Transport\DefaultTransportFactory;
  10. use Sentry\Transport\TransportFactoryInterface;
  11. use Sentry\Transport\TransportInterface;
  12. /**
  13. * The default implementation of {@link ClientBuilderInterface}.
  14. *
  15. * @author Stefano Arlandini <sarlandini@alice.it>
  16. */
  17. final class ClientBuilder implements ClientBuilderInterface
  18. {
  19. /**
  20. * @var Options The client options
  21. */
  22. private $options;
  23. /**
  24. * @var TransportFactoryInterface|null The transport factory
  25. */
  26. private $transportFactory;
  27. /**
  28. * @var TransportInterface|null The transport
  29. */
  30. private $transport;
  31. /**
  32. * @var SerializerInterface|null The serializer to be injected in the client
  33. */
  34. private $serializer;
  35. /**
  36. * @var RepresentationSerializerInterface|null The representation serializer to be injected in the client
  37. */
  38. private $representationSerializer;
  39. /**
  40. * @var LoggerInterface|null A PSR-3 logger to log internal errors and debug messages
  41. */
  42. private $logger;
  43. /**
  44. * @var string The SDK identifier, to be used in {@see Event} and {@see SentryAuth}
  45. */
  46. private $sdkIdentifier = Client::SDK_IDENTIFIER;
  47. /**
  48. * @var string The SDK version of the Client
  49. */
  50. private $sdkVersion = Client::SDK_VERSION;
  51. /**
  52. * Class constructor.
  53. *
  54. * @param Options|null $options The client options
  55. */
  56. public function __construct(Options $options = null)
  57. {
  58. $this->options = $options ?? new Options();
  59. }
  60. /**
  61. * {@inheritdoc}
  62. */
  63. public static function create(array $options = []): ClientBuilderInterface
  64. {
  65. return new self(new Options($options));
  66. }
  67. /**
  68. * {@inheritdoc}
  69. */
  70. public function getOptions(): Options
  71. {
  72. return $this->options;
  73. }
  74. /**
  75. * {@inheritdoc}
  76. */
  77. public function setSerializer(SerializerInterface $serializer): ClientBuilderInterface
  78. {
  79. $this->serializer = $serializer;
  80. return $this;
  81. }
  82. /**
  83. * {@inheritdoc}
  84. */
  85. public function setRepresentationSerializer(RepresentationSerializerInterface $representationSerializer): ClientBuilderInterface
  86. {
  87. $this->representationSerializer = $representationSerializer;
  88. return $this;
  89. }
  90. /**
  91. * {@inheritdoc}
  92. */
  93. public function setLogger(LoggerInterface $logger): ClientBuilderInterface
  94. {
  95. $this->logger = $logger;
  96. return $this;
  97. }
  98. /**
  99. * {@inheritdoc}
  100. */
  101. public function setSdkIdentifier(string $sdkIdentifier): ClientBuilderInterface
  102. {
  103. $this->sdkIdentifier = $sdkIdentifier;
  104. return $this;
  105. }
  106. /**
  107. * {@inheritdoc}
  108. */
  109. public function setSdkVersion(string $sdkVersion): ClientBuilderInterface
  110. {
  111. $this->sdkVersion = $sdkVersion;
  112. return $this;
  113. }
  114. /**
  115. * {@inheritdoc}
  116. */
  117. public function setTransportFactory(TransportFactoryInterface $transportFactory): ClientBuilderInterface
  118. {
  119. $this->transportFactory = $transportFactory;
  120. return $this;
  121. }
  122. /**
  123. * {@inheritdoc}
  124. */
  125. public function getClient(): ClientInterface
  126. {
  127. $this->transport = $this->transport ?? $this->createTransportInstance();
  128. return new Client($this->options, $this->transport, $this->sdkIdentifier, $this->sdkVersion, $this->serializer, $this->representationSerializer, $this->logger);
  129. }
  130. /**
  131. * Creates a new instance of the transport mechanism.
  132. */
  133. private function createTransportInstance(): TransportInterface
  134. {
  135. if (null !== $this->transport) {
  136. return $this->transport;
  137. }
  138. $transportFactory = $this->transportFactory ?? $this->createDefaultTransportFactory();
  139. return $transportFactory->create($this->options);
  140. }
  141. /**
  142. * Creates a new instance of the {@see DefaultTransportFactory} factory.
  143. */
  144. private function createDefaultTransportFactory(): DefaultTransportFactory
  145. {
  146. $streamFactory = Psr17FactoryDiscovery::findStreamFactory();
  147. $httpClientFactory = new HttpClientFactory(
  148. null,
  149. null,
  150. $streamFactory,
  151. null,
  152. $this->sdkIdentifier,
  153. $this->sdkVersion
  154. );
  155. return new DefaultTransportFactory(
  156. $streamFactory,
  157. Psr17FactoryDiscovery::findRequestFactory(),
  158. $httpClientFactory,
  159. $this->logger
  160. );
  161. }
  162. }