<commit_before><?php

/**
 * Class: SentryAdaptor.
 *
 * @author  <NAME> 2017-2019 <<EMAIL>>
 * @package phptek/sentry
 */

namespace PhpTek\Sentry\Adaptor;

use Sentry\State\Hub;
use Sentry\ClientBuilder;
use Sentry\State\Scope;
use Sentry\Severity;
use Sentry\ClientInterface;
use SilverStripe\Core\Config\Configurable;
use SilverStripe\Core\Injector\Injector;

/**
 * The SentryAdaptor provides functionality bridge between the PHP SDK and
 * {@link SentryLogger} itself.
 */
class SentryAdaptor
{
    use Configurable;

    /**
     * It's an ERROR unless proven otherwise!
     *
     * @var    string
     * @config
     */
    private static $default_error_level = 'ERR';

    /**
     * @var ClientInterface
     */
    protected $sentry;

    /**
     * A mapping of log-level values between Zend_Log => Raven_Client
     *
     * @var array
     */
    protected $logLevels = [
        'NOTICE' => Severity::INFO,
        'WARN'   => Severity::WARNING,
        'ERR'    => Severity::ERROR,
        'EMERG'  => Severity::FATAL
    ];

    /**
     * @return void
     */
    public function __construct()
    {
        $client = ClientBuilder::create($this->getOpts() ?: [])->getClient();
        Hub::setCurrent(new Hub($client));

        $this->sentry = $client;
    }

    /**
     * @return ClientInterface
     */
    public function getSDK() : ClientInterface
    {
        return $this->sentry;
    }

    /**
     * @param  string $field
     * @param  mixed  $data
     * @return void
     * @throws SentryLogWriterException
     */
    public function setData(string $field, $data) : void
    {
        $options = Hub::getCurrent()->getClient()->getOptions();

        switch ($field) {
            case 'env':
                $options->setEnvironment($data);
                break;
            case 'tags':
                Hub::getCurrent()->configureScope(function (Scope $scope) use($data) : void {
                    foreach ($data as $tagName => $tagData) {
                        $scope->setTag($tagName, $tagData);
                    }
                });
                break;
            case 'user':
                Hub::getCurrent()->configureScope(function (Scope $scope) use($data) : void {
                    $scope->setUser($data);
                });
                break;
            case 'extra':
                Hub::getCurrent()->configureScope(function (Scope $scope) use($data) : void {
                    foreach ($data as $extraKey => $extraData) {
                        $scope->setExtra($extraKey, $extraData);
                    }
                });
                break;
            case 'level':
                Hub::getCurrent()->configureScope(function (Scope $scope) use($data) : void {
                    $scope->setLevel(new Severity(strtolower($data)));
                });
                break;
            default:
                $msg = sprintf('Unknown field "%s" passed to %s().', $field, __FUNCTION__);
                throw new SentryLogWriterException($msg);
        }
    }

    /**
     * Simple getter for data set to / on the sentry client.
     *
     * @return array
     */
    public function getData() : array
    {
        $options = Hub::getCurrent()->getClient()->getOptions();
        $data = [];

        Hub::getCurrent()->configureScope(function (Scope $scope) use (&$data) : void {
                $data['user'] = $scope->getUser();
                $data['tags'] = $scope->getTags();
                $data['extra'] = $scope->getExtra();
        });

        return [
            'env'   => $options->getEnvironment(),
            'tags'  => $data['tags'] ?? [],
            'user'  => $data['user'] ?? [],
            'extra' => $data['extra'] ?? [],
        ];
    }

    /**
     * @param  string $level
     * @return string
     */
    public function getLevel(string $level) : string
    {
        return isset($this->logLevels[$level]) ?
            $this->logLevels[$level] :
            $this->logLevels[self::$default_error_level];
    }

    /**
     * Get various userland options to pass to Raven. Includes detecting and setting
     * proxy options too.
     *
     * @param  string $opt
     * @return mixed  string|array|null depending on whether $opts is passed.
     */
    protected function getOpts(string $opt = '')
    {
        // Extract env-vars from YML config
        $opts = Injector::inst()->convertServiceProperty($this->config()->get('opts'));<commit_msg>MINOR: Removing unused cruft.<commit_after><?php

/**
 * Class: SentryAdaptor.
 *
 * @author  <NAME> 2017-2019 <<EMAIL>>
 * @package phptek/sentry
 */

namespace PhpTek\Sentry\Adaptor;

use Sentry\State\Hub;
use Sentry\ClientBuilder;
use Sentry\State\Scope;
use Sentry\Severity;
use Sentry\ClientInterface;
use SilverStripe\Core\Config\Configurable;
use SilverStripe\Core\Injector\Injector;

/**
 * The SentryAdaptor provides a functionality bridge between the getsentry/sentry
 * PHP SDK and {@link SentryLogger} itself.
 */
class SentryAdaptor
{
    use Configurable;

    /**
     * @var ClientInterface
     */
    protected $sentry;

    /**
     * @return void
     */
    public function __construct()
    {
        $client = ClientBuilder::create($this->getOpts() ?: [])->getClient();
        Hub::setCurrent(new Hub($client));

        $this->sentry = $client;
    }

    /**
     * @return ClientInterface
     */
    public function getSDK() : ClientInterface
    {
        return $this->sentry;
    }

    /**
     * @param  string $field
     * @param  mixed  $data
     * @return void
     * @throws SentryLogWriterException
     */
    public function setData(string $field, $data) : void
    {
        $options = Hub::getCurrent()->getClient()->getOptions();

        switch ($field) {
            case 'env':
                $options->setEnvironment($data);
                break;
            case 'tags':
                Hub::getCurrent()->configureScope(function (Scope $scope) use($data) : void {
                    foreach ($data as $tagName => $tagData) {
                        $scope->setTag($tagName, $tagData);
                    }
                });
                break;
            case 'user':
                Hub::getCurrent()->configureScope(function (Scope $scope) use($data) : void {
                    $scope->setUser($data);
                });
                break;
            case 'extra':
                Hub::getCurrent()->configureScope(function (Scope $scope) use($data) : void {
                    foreach ($data as $extraKey => $extraData) {
                        $scope->setExtra($extraKey, $extraData);
                    }
                });
                break;
            case 'level':
                Hub::getCurrent()->configureScope(function (Scope $scope) use($data) : void {
                    $scope->setLevel(new Severity(strtolower($data)));
                });
                break;
            default:
                $msg = sprintf('Unknown field "%s" passed to %s().', $field, __FUNCTION__);
                throw new SentryLogWriterException($msg);
        }
    }

    /**
     * Simple getter for data set to / on the sentry client.
     *
     * @return array
     */
    public function getData() : array
    {
        $options = Hub::getCurrent()->getClient()->getOptions();
        $data = [];

        Hub::getCurrent()->configureScope(function (Scope $scope) use (&$data) : void {
                $data['user'] = $scope->getUser();
                $data['tags'] = $scope->getTags();
                $data['extra'] = $scope->getExtra();
        });

        return [
            'env'   => $options->getEnvironment(),
            'tags'  => $data['tags'] ?? [],
            'user'  => $data['user'] ?? [],
            'extra' => $data['extra'] ?? [],
        ];
    }

    /**
     * Get various userland options to pass to Raven. Includes detecting and setting
     * proxy options too.
     *
     * @param  string $opt
     * @return mixed  string|array|null depending on whether $opts is passed.
     */
    protected function getOpts(string $opt = '')
    {
        // Extract env-vars from YML config
        $opts = Injector::inst()->convertServiceProperty($this->config()->get('opts'));
{'fraction_non_alphanumeric': 0.14527845036319612, 'fraction_numerical': 0.001937046004842615, 'mean_word_length': 1.8108200068050357, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 79, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2949353', 'n_tokens_mistral': 2357, 'n_tokens_neox': 2278, 'n_words': 511}