AnalyticsToken.php 1.97 KB
Newer Older
Ketan's avatar
Ketan committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Magento\Analytics\Model;

use Magento\Framework\App\Config\ReinitableConfigInterface;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\App\Config\Storage\WriterInterface;

/**
 * Model for handling Magento BI token value into config.
 */
class AnalyticsToken
{
    /**
     * Path to value of Magento BI token into config.
     */
    private $tokenPath = 'analytics/general/token';

    /**
     * Reinitable Config Model.
     *
     * @var ReinitableConfigInterface
     */
    private $reinitableConfig;

    /**
     * Scope config model.
     *
     * @var ScopeConfigInterface
     */
    private $config;

    /**
     * Service which allows to write values into config.
     *
     * @var WriterInterface
     */
    private $configWriter;

    /**
     * @param ReinitableConfigInterface $reinitableConfig
     * @param ScopeConfigInterface $config
     * @param WriterInterface $configWriter
     */
    public function __construct(
        ReinitableConfigInterface $reinitableConfig,
        ScopeConfigInterface $config,
        WriterInterface $configWriter
    ) {
        $this->reinitableConfig = $reinitableConfig;
        $this->config = $config;
        $this->configWriter = $configWriter;
    }

    /**
     * Get Magento BI token value.
     *
     * @return string|null
     */
    public function getToken()
    {
        return $this->config->getValue($this->tokenPath);
    }

    /**
     * Stores Magento BI token value.
     *
     * @param string $value
     *
     * @return bool
     */
    public function storeToken($value)
    {
        $this->configWriter->save($this->tokenPath, $value);
        $this->reinitableConfig->reinit();

        return true;
    }

    /**
     * Check Magento BI token value exist.
     *
     * @return bool
     */
    public function isTokenExist()
    {
        return (bool)$this->getToken();
    }
}