<commit_before><?php

/*
 * Copyright 2005 - 2020 Centreon (https://www.centreon.com/)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * For more information : <EMAIL>
 *
 */

declare(strict_types=1);

namespace Centreon\Application\Controller\Administration;

use Centreon\Application\Controller\AbstractController;
use Centreon\Domain\Option\Interfaces\OptionServiceInterface;
use FOS\RestBundle\View\View;

/**
 * Used to get global parameters
 *
 * @package Centreon\Application\Controller
 */
class ParametersController extends AbstractController
{
    /**
     * @var OptionServiceInterface
     */
    private $optionService;

    private const DEFAULT_DOWNTIME_DURATION = 'monitoring_dwt_duration';
    private const DEFAULT_REFRESH_INTERVAL = 'AjaxTimeReloadMonitoring';

    /**
     * Needed to make response "more readable"
     */
    private const KEY_NAME_CONCORDANCE = [
        self::DEFAULT_REFRESH_INTERVAL => 'monitoring_default_refresh_interval',
        self::DEFAULT_DOWNTIME_DURATION => 'monitoring_default_downtime_duration'
    ];

    /**
     * Parameters constructor.
     *
     * @param OptionServiceInterface $optionService
     */
    public function __construct(OptionServiceInterface $optionService)
    {
        $this->optionService = $optionService;
    }

    /**
     * Entry point to get global parameters stored in options table
     *
     * @return View
     */
    public function getParameters(): View
    {
        $this->denyAccessUnlessGrantedForApiConfiguration();

        $parameters = [];

        $options = $this->optionService->findSelectedOptions([
            self::DEFAULT_DOWNTIME_DURATION,
            self::DEFAULT_REFRESH_INTERVAL
        ]);

        foreach ($options as $option) {
            $parameters[self::KEY_NAME_CONCORDANCE[$option->getName()]] = $option->getValue();
        }

        return $this->view($parameters);
    }
}<commit_msg>fix(api): handle unit for default downtime duration (#9320)<commit_after><?php

/*
 * Copyright 2005 - 2020 Centreon (https://www.centreon.com/)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * For more information : <EMAIL>
 *
 */

declare(strict_types=1);

namespace Centreon\Application\Controller\Administration;

use Centreon\Application\Controller\AbstractController;
use Centreon\Domain\Option\Interfaces\OptionServiceInterface;
use FOS\RestBundle\View\View;

/**
 * Used to get global parameters
 *
 * @package Centreon\Application\Controller
 */
class ParametersController extends AbstractController
{
    /**
     * @var OptionServiceInterface
     */
    private $optionService;

    private const DEFAULT_DOWNTIME_DURATION = 'monitoring_dwt_duration',
                  DEFAULT_DOWNTIME_DURATION_SCALE = 'monitoring_dwt_duration_scale',
                  DEFAULT_REFRESH_INTERVAL = 'AjaxTimeReloadMonitoring';

    /**
     * Needed to make response "more readable"
     */
    private const KEY_NAME_CONCORDANCE = [
        self::DEFAULT_REFRESH_INTERVAL => 'monitoring_default_refresh_interval',
        self::DEFAULT_DOWNTIME_DURATION => 'monitoring_default_downtime_duration'
    ];

    /**
     * Parameters constructor.
     *
     * @param OptionServiceInterface $optionService
     */
    public function __construct(OptionServiceInterface $optionService)
    {
        $this->optionService = $optionService;
    }

    /**
     * Entry point to get global parameters stored in options table
     *
     * @return View
     */
    public function getParameters(): View
    {
        $this->denyAccessUnlessGrantedForApiConfiguration();

        $parameters = [];
        $downtimeDuration = '';
        $downtimeScale = '';
        $refreshInterval = '';

        $options = $this->optionService->findSelectedOptions([
            self::DEFAULT_DOWNTIME_DURATION,
            self::DEFAULT_DOWNTIME_DURATION_SCALE,
            self::DEFAULT_REFRESH_INTERVAL
        ]);

        foreach ($options as $option) {
            switch ($option->getName()) {
                case self::DEFAULT_DOWNTIME_DURATION:
                    $downtimeDuration = $option->getValue();
                    break;
                case self::DEFAULT_DOWNTIME_DURATION_SCALE:
                    $downtimeScale = $option->getValue();
                    break;
                case self::DEFAULT_REFRESH_INTERVAL:
                    $refreshInterval = $option->getValue();
                    break;
                default:
                    break;
            }
        }

        $parameters[self::KEY_NAME_CONCORDANCE[self::DEFAULT_DOWNTIME_DURATION]] =
            $this->convertToSeconds((int) $downtimeDuration, $downtimeScale);

        $parameters[self::KEY_NAME_CONCORDANCE[self::DEFAULT_REFRESH_INTERVAL]] = (int) $refreshInterval;

        return $this->view($parameters);
    }

    /**
     * Converts the combination stored in DB into seconds
     *
     * @param integer $duration
     * @param string $scale
     * @return integer
     */
    private function convertToSeconds(int $duration, string $scale): int
    {
        switch ($scale) {
            case 'm':
                return ($duration * 60);
            case 'h':
                return ($duration * 3600);
            case 'd':
                return ($duration * 86400);
            default:
                return $duration;
        }
    }
}
{'fraction_non_alphanumeric': 0.0961659333752357, 'fraction_numerical': 0.0064424890006285354, 'mean_word_length': 2.7092074592074593, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 26, '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': '6349895', 'n_tokens_mistral': 1742, 'n_tokens_neox': 1670, 'n_words': 426}