Skip to content

Commit

Permalink
Adjust Sentry Component to work with version
Browse files Browse the repository at this point in the history
What
Add typehint
  • Loading branch information
papppeter authored Jul 30, 2021
1 parent 3926b03 commit 0da3d65
Showing 1 changed file with 23 additions and 34 deletions.
57 changes: 23 additions & 34 deletions src/SentryComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Sentry\Transport\TransportFactoryInterface;
use yii\base\Component;
use yii\base\InvalidConfigException;
use yii\helpers\ArrayHelper;

/**
* Class SentryComponent
Expand All @@ -22,59 +23,36 @@ class SentryComponent extends Component
{
public const NULL_TRANSPORT = 'nullTransport';

/**
* @var string Sentry client key.
*/
public $dns;
public string $dns;

/**
* @var string $transportMode
*/
public $transportMode;
public ?string $transportMode = null;

public array $clientOptions = [];

/**
*
*/
public function init()
{

$options = [
$options = ArrayHelper::merge($this->clientOptions, [
'dsn' => $this->dns,
];
]);

$builder = ClientBuilder::create($options);
if($this->transportMode === self::NULL_TRANSPORT) {
$transportFactory = new class implements TransportFactoryInterface {
public function create(\Sentry\Options $options): \Sentry\Transport\TransportInterface
{
return new NullTransport();
}
};

$builder->setTransportFactory($transportFactory);
if ($this->transportMode) {
$transport = $this->{$this->transportMode}($options);
$builder->setTransportFactory($transport);
}

SentrySdk::setCurrentHub(new Hub($builder->getClient()));
SentrySdk::getCurrentHub()->bindClient($builder->getClient());

parent::init(); // TODO: Change the autogenerated stub
}

/**
* @param $options
* @return NullTransport
*/
protected function nullTransport($options): NullTransport
{
return new NullTransport();
}

/**
* @return \Sentry\ClientInterface
* @throws InvalidConfigException
*/
public function getClient(): \Sentry\ClientInterface
{
$client = Hub::getCurrent()->getClient();
$client = SentrySdk::getCurrentHub()->getClient();

if(!$client) {
throw new InvalidConfigException('Client setup is missing');
Expand Down Expand Up @@ -105,4 +83,15 @@ public function captureMessage(string $message, ?Severity $level = null, ?Scope
{
return $this->getClient()->captureMessage($message, $level, $scope);
}

protected function nullTransport($options)
{
return new class implements TransportFactoryInterface {

public function create(\Sentry\Options $options): \Sentry\Transport\TransportInterface
{
return new NullTransport();
}
};
}
}

0 comments on commit 0da3d65

Please sign in to comment.