Config.php 2.16 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
<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Magento\Directory\Model\Currency\Import;

/**
 * Configured currency import services
 *
 * @api
 * @since 100.0.2
 */
class Config
{
    /**
     * @var array
     */
    private $_servicesConfig;

    /**
     * Validate format of services configuration array
     *
     * @param array $servicesConfig
     * @throws \InvalidArgumentException
     */
    public function __construct(array $servicesConfig)
    {
        foreach ($servicesConfig as $serviceName => $serviceInfo) {
            if (!is_string($serviceName) || empty($serviceName)) {
                throw new \InvalidArgumentException('Name for a currency import service has to be specified.');
            }
            if (empty($serviceInfo['class'])) {
                throw new \InvalidArgumentException('Class for a currency import service has to be specified.');
            }
            if (empty($serviceInfo['label'])) {
                throw new \InvalidArgumentException('Label for a currency import service has to be specified.');
            }
        }
        $this->_servicesConfig = $servicesConfig;
    }

    /**
     * Retrieve unique names of all available currency import services
     *
     * @return array
     */
    public function getAvailableServices()
    {
        return array_keys($this->_servicesConfig);
    }

    /**
     * Retrieve name of a class that corresponds to service name
     *
     * @param string $serviceName
     * @return string|null
     */
    public function getServiceClass($serviceName)
    {
        if (isset($this->_servicesConfig[$serviceName]['class'])) {
            return $this->_servicesConfig[$serviceName]['class'];
        }
        return null;
    }

    /**
     * Retrieve already translated label that corresponds to service name
     *
     * @param string $serviceName
     * @return \Magento\Framework\Phrase|null
     */
    public function getServiceLabel($serviceName)
    {
        if (isset($this->_servicesConfig[$serviceName]['label'])) {
            return __($this->_servicesConfig[$serviceName]['label']);
        }
        return null;
    }
}