<commit_before><?php
/**
 * Copyright © 2016 Sectionio. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Sectionio\Metrics\Helper;

use Magento\Framework\App\Helper\AbstractHelper;

class Data extends AbstractHelper
{
    /** @var \Sectionio\Metrics\Model\SettingsFactory $settingsFactory */
    protected $settingsFactory;
    /** @var \Sectionio\Metrics\Model\AccountFactory $accountFactory */
    protected $accountFactory;
    /** @var \Sectionio\Metrics\Model\ApplicationFactory $applicationFactory */
    protected $applicationFactory;
    /** @var \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig */
    protected $scopeConfig;
    /** @var \Magento\Framework\Encryption\EncryptorInterface $encryptor */
    protected $encryptor;
    // var \Magento\Framework\Filesystem\DirectoryList $directoryList
    protected $directoryList;
    // var \Magento\Store\Model\StoreManagerInterface $storeManager
    protected $storeManager;

    /**
     * @param \Magento\Framework\App\Helper\Context $context
     * @param \Sectionio\Metrics\Model\SettingsFactory $settingsFactory
     * @param \Sectionio\Metrics\Model\AccountFactory $accountFactory
     * @param \Sectionio\Metrics\Model\ApplicationFactory $applicationFactory
     * @param \Magento\Framework\Encryption\EncryptorInterface $encryptor
     */
    public function __construct(
        \Magento\Framework\App\Helper\Context $context,
        \Sectionio\Metrics\Model\SettingsFactory $settingsFactory,
        \Sectionio\Metrics\Model\AccountFactory $accountFactory,
        \Sectionio\Metrics\Model\ApplicationFactory $applicationFactory,
        \Magento\Framework\Encryption\EncryptorInterface $encryptor,
        \Magento\Framework\Filesystem\DirectoryList $directoryList,
        \Magento\Store\Model\StoreManagerInterface $storeManager
    ) {
        parent::__construct($context);
        $this->settingsFactory = $settingsFactory;
        $this->accountFactory = $accountFactory;
        $this->applicationFactory = $applicationFactory;
        $this->scopeConfig = $context->getScopeConfig();
        $this->encryptor = $encryptor;
        $this->directoryList = $directoryList;
        $this->storeManager = $storeManager;
    }

    private function getPluginConfig() {
        $cache_expiry_seconds = 60 * 60; // 1 hour

        // http://blog.belvg.com/how-to-get-access-to-working-directories-in-magento-2-0.html
        $cache_dir = $this->directoryList->getPath('cache') . '/section.io';
        if (!is_dir($cache_dir)) {
            mkdir($cache_dir);
        }
        $cached_config_file = $cache_dir . '/magento-section-io-plugin-config.json';

        if (is_file($cached_config_file)) {
            $mtime = filemtime($cached_config_file);
            if (time() - $mtime < $cache_expiry_seconds) {
                return file_get_contents($cached_config_file);
            }
        }

        /** @var string $service_url */
        $service_url = 'https://www.section.io/magento-section-io-plugin-config.json';

        // setup curl call
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $service_url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_FAILONERROR, false);
        curl_setopt($ch, CURLOPT_HEADER, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 15);
        curl_setopt($ch, CURLOPT_TIMEOUT, 30);

        // if response received
        if ($curl_response = curl_exec($ch)) {
            file_put_contents($cached_config_file, $curl_response);
            return $curl_response;
        }
    }

    // to find all the identifiers used with GetCopy, use:
    // $ grep -ho "getCopy([\"']\([^\"']\|\\[\"']\)*" --include=*.php -r /var/www/html/app/code/Sectionio/Metrics | cut -c10-
    public function getCopy($identifier, $default) {
        $decoded = json_decode($this->getPluginConfig(), true);
        $copy = [];
        if (array_key_exists('copy', $decoded)) {
            $copy = $decoded['copy'];
        }
        if (array_key_exists($identifier, $copy)) {
            return $copy[$identifier];
        }<commit_msg>Log and continue if cache directory is not available<commit_after><?php
/**
 * Copyright © 2016 Sectionio. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Sectionio\Metrics\Helper;

use Magento\Framework\App\Helper\AbstractHelper;

class Data extends AbstractHelper
{
    /** @var \Sectionio\Metrics\Model\SettingsFactory $settingsFactory */
    protected $settingsFactory;
    /** @var \Sectionio\Metrics\Model\AccountFactory $accountFactory */
    protected $accountFactory;
    /** @var \Sectionio\Metrics\Model\ApplicationFactory $applicationFactory */
    protected $applicationFactory;
    /** @var \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig */
    protected $scopeConfig;
    /** @var \Magento\Framework\Encryption\EncryptorInterface $encryptor */
    protected $encryptor;
    // var \Magento\Framework\Filesystem\DirectoryList $directoryList
    protected $directoryList;
    // var \Magento\Store\Model\StoreManagerInterface $storeManager
    protected $storeManager;

    /**
     * @param \Magento\Framework\App\Helper\Context $context
     * @param \Sectionio\Metrics\Model\SettingsFactory $settingsFactory
     * @param \Sectionio\Metrics\Model\AccountFactory $accountFactory
     * @param \Sectionio\Metrics\Model\ApplicationFactory $applicationFactory
     * @param \Magento\Framework\Encryption\EncryptorInterface $encryptor
     */
    public function __construct(
        \Magento\Framework\App\Helper\Context $context,
        \Sectionio\Metrics\Model\SettingsFactory $settingsFactory,
        \Sectionio\Metrics\Model\AccountFactory $accountFactory,
        \Sectionio\Metrics\Model\ApplicationFactory $applicationFactory,
        \Magento\Framework\Encryption\EncryptorInterface $encryptor,
        \Magento\Framework\Filesystem\DirectoryList $directoryList,
        \Magento\Store\Model\StoreManagerInterface $storeManager
    ) {
        parent::__construct($context);
        $this->settingsFactory = $settingsFactory;
        $this->accountFactory = $accountFactory;
        $this->applicationFactory = $applicationFactory;
        $this->scopeConfig = $context->getScopeConfig();
        $this->encryptor = $encryptor;
        $this->directoryList = $directoryList;
        $this->storeManager = $storeManager;
    }

    private function getPluginConfig() {
        $cache_expiry_seconds = 60 * 60; // 1 hour

        // http://blog.belvg.com/how-to-get-access-to-working-directories-in-magento-2-0.html
        $cache_dir = $this->directoryList->getPath('cache') . '/section.io';
        if (!is_dir($cache_dir)) {
            try {
                mkdir($cache_dir);
            } catch (Exception $e) {
                $this->_logger->warning('Could not create cache directory. Skipping cache.', [ 'exception' => $e ]);
            }
        }
        $cached_config_file = $cache_dir . '/magento-section-io-plugin-config.json';

        if (is_file($cached_config_file)) {
            $mtime = filemtime($cached_config_file);
            if (time() - $mtime < $cache_expiry_seconds) {
                return file_get_contents($cached_config_file);
            }
        }

        /** @var string $service_url */
        $service_url = 'https://www.section.io/magento-section-io-plugin-config.json';

        // setup curl call
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $service_url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_FAILONERROR, false);
        curl_setopt($ch, CURLOPT_HEADER, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 15);
        curl_setopt($ch, CURLOPT_TIMEOUT, 30);

        // if response received
        if ($curl_response = curl_exec($ch)) {
            try {
                file_put_contents($cached_config_file, $curl_response);
            } catch (Exception $e) {
                $this->_logger->warning('Could not write cache file. Skipping cache.', [ 'exception' => $e ]);
            }
            return $curl_response;
        }
    }

    // to find all the identifiers used with GetCopy, use:
    // $ grep -ho "getCopy([\"']\([^\"']\|\\[\"']\)*" --include=*.php -r /var/www/html/app/code/Sectionio/Metrics | cut -c10-
    public function getCopy($identifier, $default) {
        $decoded = json_decode($this->getPluginConfig(), true);
        $copy = [];
        if (array_key_exists('copy', $decoded)) {
            $copy = $decoded['copy'];
        }
        if (array_key_exists($identifier, $copy)) {
            return $copy[$identifier];
        }
{'fraction_non_alphanumeric': 0.12961094293082184, 'fraction_numerical': 0.0038120865567888777, 'mean_word_length': 2.8498057833405266, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 31, 'https://': 2, 'lorem ipsum': 0, 'www.': 4, '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': '2432501', 'n_tokens_mistral': 2629, 'n_tokens_neox': 2563, 'n_words': 454}