<commit_before><?php

/*
 * This file is part of the Access Watch package.
 *
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace AccessWatch;

use Bouncer\Bouncer;

/**
 * Access Watch class
 *
 * @author <NAME> <<EMAIL>>
 */
class AccessWatch extends Bouncer
{

    /**
     * @var object
     */
    protected $apiClient;

    /**
     * {@inheritDoc}
     */
    public function __construct(array $options = array())
    {
        if (empty($options['apiClient'])) {
            // Filter Api Client options
            $apiClientKeys = array('baseUrl', 'apiKey', 'siteUrl', 'httpClient');
            $apiClientOptions = array_intersect_key($options, array_flip($apiClientKeys));
            $options['apiClient'] = new \AccessWatch\Api\ApiClient($apiClientOptions);
        }

        $this->apiClient = $options['apiClient'];

        if (empty($options['profile'])) {
            // Filter Profile options
            $profileKeys = array('baseUrl', 'apiKey', 'siteUrl', 'apiClient');
            $profileOptions = array_intersect_key($options, array_flip($profileKeys));
            $options['profile'] = new \AccessWatch\Profile\BaseProfile($profileOptions);
        }

        parent::__construct($options);
    }

    /*
     * @return object
     */
    public function getApiClient()
    {
        if (empty($this->apiClient)) {
            throw new Exception('No Api Client available.');
        }

        return $this->apiClient;
    }

    /**
     * {@inheritDoc}
     */
    public function getRequest()
    {
        $request = parent::getRequest();

        $configuration = $this->getConfiguration();
        if (isset($configuration['trusted_proxies']) && is_array($configuration['trusted_proxies'])) {
            $request->setTrustedProxies(array_merge(array('127.0.0.1'), $configuration['trusted_proxies']));
        }

        return $request;
    }

    /**
     * {@inheritDoc}
     */
    public function start()
    {
        parent::start();

        $identity = $this->getIdentity();

        if ($identity) {
            if ($identity->getAgentName() == 'accesswatch' && $identity->isNice()) {
                $this->feedback();
            }
        }
    }

    /**
     * @return array
     */
    public function getConfiguration()
    {
        $cache = $this->getCache();
        if ($cache) {
            $configuration = $this->getCache()->get('access_watch_configuration');
        }
        if (empty($configuration)) {
            $configuration = $this->getApiClient()->getConfiguration();
            if ($cache) {
                $this->getCache()->set('access_watch_configuration', $configuration, 86400);
            }
        }
        return $configuration;
    }

    public function feedback()
    {
        $request = $this->getRequest();
        $action = $request->headers->get('Access-Watch-Action');
        if ($action) {
            switch ($action) {
                case 'identity-update':
                    $identityId = $request->headers->get('Access-Watch-Identity');
                    $this->getCache()->deleteIdentity($identityId);
                    break;
                case 'session-update':<commit_msg>optimise getConfiguration, do only once even if no caching<commit_after><?php

/*
 * This file is part of the Access Watch package.
 *
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace AccessWatch;

use Bouncer\Bouncer;

/**
 * Access Watch class
 *
 * @author <NAME> <<EMAIL>>
 */
class AccessWatch extends Bouncer
{

    /**
     * @var object
     */
    protected $apiClient;

    /**
     * @var array
     */
    protected $configuration;

    /**
     * {@inheritDoc}
     */
    public function __construct(array $options = array())
    {
        if (empty($options['apiClient'])) {
            // Filter Api Client options
            $apiClientKeys = array('baseUrl', 'apiKey', 'siteUrl', 'httpClient');
            $apiClientOptions = array_intersect_key($options, array_flip($apiClientKeys));
            $options['apiClient'] = new \AccessWatch\Api\ApiClient($apiClientOptions);
        }

        $this->apiClient = $options['apiClient'];

        if (empty($options['profile'])) {
            // Filter Profile options
            $profileKeys = array('baseUrl', 'apiKey', 'siteUrl', 'apiClient');
            $profileOptions = array_intersect_key($options, array_flip($profileKeys));
            $options['profile'] = new \AccessWatch\Profile\BaseProfile($profileOptions);
        }

        parent::__construct($options);
    }

    /*
     * @return object
     */
    public function getApiClient()
    {
        if (empty($this->apiClient)) {
            throw new Exception('No Api Client available.');
        }

        return $this->apiClient;
    }

    /**
     * {@inheritDoc}
     */
    public function getRequest()
    {
        $request = parent::getRequest();

        $configuration = $this->getConfiguration();
        if (isset($configuration['trusted_proxies']) && is_array($configuration['trusted_proxies'])) {
            $request->setTrustedProxies(array_merge(array('127.0.0.1'), $configuration['trusted_proxies']));
        }

        return $request;
    }

    /**
     * {@inheritDoc}
     */
    public function start()
    {
        parent::start();

        $identity = $this->getIdentity();

        if ($identity) {
            if ($identity->getAgentName() == 'accesswatch' && $identity->isNice()) {
                $this->feedback();
            }
        }
    }

    /**
     * @return array|null
     */
    public function getConfiguration()
    {
        // From local Cache
        if (isset($this->configuration)) {
            return $this->configuration;
        }

        // From Cache
        $cache = $this->getCache();
        if ($cache) {
            $configuration = $this->getCache()->get('configuration');
            if ($configuration) {
                return $this->configuration = $configuration;
            }
        }

        // From API
        if ($this->ended === false) {
            $configuration = $this->getApiClient()->getConfiguration();
            if ($cache) {
                $this->getCache()->set('configuration', $configuration, 86400);
            }
            return $this->configuration = $configuration;
        }
    }

    public function feedback()
    {
        $request = $this->getRequest();
        $action = $request->headers->get('Access-Watch-Action');
        if ($action) {
            switch ($action) {
                case 'identity-update':
                    $identityId = $request->headers->get('Access-Watch-Identity');
                    $this->getCache()->deleteIdentity($identityId);
                    break;
                case 'session-update':
{'fraction_non_alphanumeric': 0.14814814814814814, 'fraction_numerical': 0.003146003146003146, 'mean_word_length': 2.0675438596491227, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 66, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '615537', 'n_tokens_mistral': 1907, 'n_tokens_neox': 1847, 'n_words': 383}