Completed
Push — master ( 407a98...cf77e4 )
by Mikaël
01:32
created

ToastUiEditor::cloudinaryMeta()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.8333
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace BbsLab\NovaToastUiEditorField;
4
5
use Illuminate\Support\Carbon;
6
use Laravel\Nova\Fields\Field;
7
8
class ToastUiEditor extends Field
9
{
10
    const EDIT_TYPE_MARKDOWN = 'markdown';
11
    const EDIT_TYPE_WYSIWYG = 'wysiwyg';
12
13
    const PREVIEW_STYLE_VERTICAL = 'vertical';
14
    const PREVIEW_STYLE_TAB = 'tab';
15
16
    /**
17
     * The field's component.
18
     *
19
     * @var string
20
     */
21
    public $component = 'nova-toast-ui-editor-field';
22
23
    /**
24
     * Indicates if the element should be shown on the index view.
25
     *
26
     * @var bool
27
     */
28
    public $showOnIndex = false;
29
30
    protected $initialEditType;
31
32
    protected $options;
33
34
    protected $height;
35
36
    protected $previewStyle;
37
38
    protected $allowIframe;
39
40
    protected $useCloudinary;
41
42
    public function __construct($name, $attribute = null, callable $resolveCallback = null)
43
    {
44
        parent::__construct($name, $attribute, $resolveCallback);
45
46
        $this->initialEditType = config('nova-toast-ui-editor.initialEditType');
47
        $this->options = config('nova-toast-ui-editor.options');
48
        $this->height = config('nova-toast-ui-editor.height');
49
        $this->previewStyle = config('nova-toast-ui-editor.previewStyle');
50
        $this->allowIframe = (bool) config('nova-toast-ui-editor.allowIframe');
51
        $this->useCloudinary = (bool) config('nova-toast-ui-editor.useCloudinary');
52
    }
53
54
    public function initialEditTypeMarkdown()
55
    {
56
        $this->initialEditType = static::EDIT_TYPE_MARKDOWN;
57
58
        return $this;
59
    }
60
61
    public function initialEditTypeWYSIWYG()
62
    {
63
        $this->initialEditType = static::EDIT_TYPE_WYSIWYG;
64
65
        return $this;
66
    }
67
68
    public function minHeight(string $minHeight)
69
    {
70
        $this->options['minHeight'] = $minHeight;
71
72
        return $this;
73
    }
74
75
    public function language(string $language)
76
    {
77
        $this->options['language'] = $language;
78
79
        return $this;
80
    }
81
82
    public function useCommandShortcut(bool $useCommandShortcut = true)
83
    {
84
        $this->options['useCommandShortcut'] = $useCommandShortcut;
85
86
        return $this;
87
    }
88
89
    public function hideModeSwitch(bool $hideModeSwitch = true)
90
    {
91
        $this->options['hideModeSwitch'] = $hideModeSwitch;
92
93
        return $this;
94
    }
95
96
    public function toolbarItems(array $toolbarItems)
97
    {
98
        $this->options['toolbarItems'] = $toolbarItems;
99
100
        return $this;
101
    }
102
103
    public function height(string $height)
104
    {
105
        $this->height = $height;
106
107
        return $this;
108
    }
109
110
    public function previewStyleVertical()
111
    {
112
        $this->previewStyle = static::PREVIEW_STYLE_VERTICAL;
113
114
        return $this;
115
    }
116
117
    public function previewStyleTab()
118
    {
119
        $this->previewStyle = static::PREVIEW_STYLE_TAB;
120
121
        return $this;
122
    }
123
124
    public function allowIframe(bool $allowIframe = true)
125
    {
126
        $this->allowIframe = $allowIframe;
127
128
        return $this;
129
    }
130
131
    public function useCloudinary(bool $useCloudinary = true)
132
    {
133
        $this->useCloudinary = $useCloudinary;
134
135
        return $this;
136
    }
137
138
    /**
139
     * Return Cloudinary field meta.
140
     *
141
     * @return array
142
     */
143
    protected function cloudinaryMeta()
144
    {
145
        $signature = $this->cloudinarySignature();
146
147
        return [
148
            'string' => $signature['string'],
149
            'api_key' => config('nova-toast-ui-editor.cloudinary.api_key'),
150
            'username' => $signature['username'],
151
            'signature' => $signature['signature'],
152
            'timestamp' => $signature['timestamp'],
153
            'cloud_name' => config('nova-toast-ui-editor.cloudinary.cloud_name'),
154
        ];
155
    }
156
157
    /**
158
     * Compute Cloudinary signature from environment.
159
     *
160
     * @return array
161
     */
162
    protected function cloudinarySignature()
163
    {
164
        $cloudName = config('nova-toast-ui-editor.cloudinary.cloud_name');
165
        $username = config('nova-toast-ui-editor.cloudinary.username');
166
        $apiSecret = config('nova-toast-ui-editor.cloudinary.api_secret');
167
        $timestamp = Carbon::now()->timestamp;
168
        $string = 'cloud_name='.$cloudName.'&timestamp='.$timestamp.'&username='.$username.$apiSecret;
169
170
        return [
171
            'string' => $string,
172
            'username' => $username,
173
            'timestamp' => $timestamp,
174
            'signature' => hash('sha256', $string),
175
        ];
176
    }
177
178
    public function jsonSerialize()
179
    {
180
        return array_merge(parent::jsonSerialize(), [
181
            'editor' => [
182
                'initialEditType' => $this->initialEditType,
183
                'options' => $this->options,
184
                'height' => $this->height,
185
                'previewStyle' => $this->previewStyle,
186
                'allowIframe' => $this->allowIframe === true,
187
                'useCloudinary' => $this->useCloudinary === true,
188
                'cloudinary' => $this->useCloudinary === true ? $this->cloudinaryMeta() : null,
189
            ],
190
        ]);
191
    }
192
}
193