<commit_before><?php
namespace selvinortiz\swipe\services;

use Craft;
use craft\web\View;
use craft\base\Component;

use Aws\CloudFront\CloudFrontClient;

class SwipeVideoService extends Component {
    const BASE_URL = 'https://d20c5t1dxoiuw3.cloudfront.net/';
    const SIGNING_KEY_PAIR_ID = '<KEY>';
    const EXPIRATION_TIME_IN_SECONDS = 3600; // 1hr

    /**
     * @var string Private key file path
     */
    private $_privateKeyFile;

    /**
     * @var CloudFrontClient
     */
    private $_cloudFrontClient;

    public function init() {
        parent::init();

        $this->_privateKeyFile = Craft::$app->path->getConfigPath().'/swipe.pem';

        $this->_cloudFrontClient = new CloudFrontClient([
            'region' => 'us-west-2',
            'version' => '2014-11-06'
        ]);
    }

    public function getSignedUrl(string $resource): string {
        $resourceUrl = self::BASE_URL.$resource;

        $policyVars = [
            'serverIp' => $this->getServerIp(),
            'resourceUrl' => $resourceUrl,
            'expirationTimestamp' => $this->getExpiration()
        ];

        return $this->_cloudFrontClient->getSignedUrl([
            'url' => $resourceUrl,
            'private_key' => $this->_privateKeyFile,
            'key_pair_id' => self::SIGNING_KEY_PAIR_ID,
            'policy' => $this->renderTemplate($policyVars)
        ]);
    }

    /**
     * Renders a Swipe template even if request originates from the front end
     *
     * @param array $vars
     *
     * @return string
     */
    private function renderTemplate(array $vars = []): string {
        $mode = Craft::$app->view->getTemplateMode();

        Craft::$app->view->setTemplateMode(View::TEMPLATE_MODE_CP);

        $rendered = Craft::$app->view->renderTemplate('swipe/_special/policy', $vars);

        Craft::$app->view->setTemplateMode($mode);

        return $rendered;
    }

    private function getServerIp(): string {
        return Craft::$app->config->get('env') == 'dev' ? '172.16.31.10' : $_SERVER['REMOTE_ADDR'];
    }

    private function getExpiration(): int {<commit_msg>Renames getServerIp() to getClientIp()<commit_after><?php
namespace selvinortiz\swipe\services;

use Craft;
use craft\web\View;
use craft\base\Component;

use Aws\CloudFront\CloudFrontClient;

class SwipeVideoService extends Component {
    const BASE_URL = 'https://d20c5t1dxoiuw3.cloudfront.net/';
    const SIGNING_KEY_PAIR_ID = '<KEY>';
    const EXPIRATION_TIME_IN_SECONDS = 3600; // 1hr

    /**
     * @var string Private key file path
     */
    private $_privateKeyFile;

    /**
     * @var CloudFrontClient
     */
    private $_cloudFrontClient;

    public function init() {
        parent::init();

        $this->_privateKeyFile = Craft::$app->path->getConfigPath().'/swipe.pem';

        $this->_cloudFrontClient = new CloudFrontClient([
            'region' => 'us-west-2',
            'version' => '2014-11-06'
        ]);
    }

    public function getSignedUrl(string $resource): string {
        $resourceUrl = self::BASE_URL.$resource;

        $policyVars = [
            'serverIp' => $this->getClientIp(),
            'resourceUrl' => $resourceUrl,
            'expirationTimestamp' => $this->getExpiration()
        ];

        return $this->_cloudFrontClient->getSignedUrl([
            'url' => $resourceUrl,
            'private_key' => $this->_privateKeyFile,
            'key_pair_id' => self::SIGNING_KEY_PAIR_ID,
            'policy' => $this->renderTemplate($policyVars)
        ]);
    }

    /**
     * Renders a Swipe template even if request originates from the front end
     *
     * @param array $vars
     *
     * @return string
     */
    private function renderTemplate(array $vars = []): string {
        $mode = Craft::$app->view->getTemplateMode();

        Craft::$app->view->setTemplateMode(View::TEMPLATE_MODE_CP);

        $rendered = Craft::$app->view->renderTemplate('swipe/_special/policy', $vars);

        Craft::$app->view->setTemplateMode($mode);

        return $rendered;
    }

    private function getClientIp(): string {
        return Craft::$app->config->get('env') == 'dev' ? '172.16.31.10' : $_SERVER['REMOTE_ADDR'];
    }

    private function getExpiration(): int {
{'fraction_non_alphanumeric': 0.14638738526712167, 'fraction_numerical': 0.013179571663920923, 'mean_word_length': 2.6139455782312924, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 63, 'https://': 2, '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': '6979851', 'n_tokens_mistral': 1336, 'n_tokens_neox': 1290, 'n_words': 239}