OptionsTrait   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 15
c 2
b 1
f 0
dl 0
loc 43
rs 10
wmc 4

1 Method

Rating   Name   Duplication   Size   Complexity  
A loadOptions() 0 20 4
1
<?php
2
/**
3
 * @copyright Copyright (c) 2018 Andrey Girnik
4
 * @author Andrey Girnik <[email protected]>
5
 * @license http://opensource.org/licenses/MIT MIT License
6
 */
7
namespace Itstructure\CKEditor;
8
9
use yii\helpers\ArrayHelper;
10
use yii\base\InvalidConfigException;
11
12
/**
13
 * Trait OptionsTrait
14
 *
15
 * @see http://docs.ckeditor.com/
16
 *
17
 * @package Itstructure\CKEditor
18
 */
19
trait OptionsTrait
20
{
21
    /**
22
     * The toolbar preset.
23
     * Available values: basic, full, standard, custom (with configuration in [clientOptions]).
24
     *
25
     * @var string
26
     */
27
    public $preset = 'standard';
28
29
    /**
30
     * The options for the CKEditor 4.
31
     *
32
     * @var array
33
     */
34
    public $clientOptions = [];
35
36
    /**
37
     * Set the clientOptions.
38
     * Options from presets are merging with the client options.
39
     *
40
     * @return void
41
     */
42
    protected function loadOptions(): void
43
    {
44
        $options = [];
45
46
        if ($this->preset == 'custom') {
47
            $optionsFile = null;
48
        } else {
49
            $optionsFile = __DIR__ . '/presets/' . $this->preset . '.php';
50
        }
51
52
        if ($optionsFile !== null) {
53
            if (!is_file($optionsFile)) {
54
                throw new InvalidConfigException('The CKEditor options file can not be loaded.');
55
            }
56
            $options = require($optionsFile);
57
        }
58
59
        $this->clientOptions = ArrayHelper::merge(
60
            $options,
61
            $this->clientOptions
62
        );
63
    }
64
}
65