RawWysiwygContext::getEditor()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 8
ccs 0
cts 4
cp 0
rs 9.4285
cc 2
eloc 4
nc 2
nop 0
crap 6
1
<?php
2
/**
3
 * @author Sergii Bondarenko, <[email protected]>
4
 */
5
namespace Drupal\TqExtension\Context\Wysiwyg;
6
7
// Contexts.
8
use Drupal\TqExtension\Context\RawTqContext;
9
// Helpers.
10
use Behat\DebugExtension\Message;
11
// Utils.
12
use Drupal\TqExtension\Utils\Wysiwyg\Wysiwyg;
13
14
class RawWysiwygContext extends RawTqContext
15
{
16
    /**
17
     * @var Wysiwyg
18
     */
19
    private $wysiwyg;
20
21
    /**
22
     * @param string $wysiwyg
23
     *   An object name.
24
     * @param array $arguments
25
     *   Arguments for object constructor.
26
     *
27
     * @throws \InvalidArgumentException
28
     * @throws \Exception
29
     */
30
    protected function setEditor($wysiwyg, array $arguments = [])
31
    {
32
        if (empty($wysiwyg)) {
33
            throw new \InvalidArgumentException(
34
                'WYSIWYG name cannot be empty. You must mark your scenario with @wysiwyg' .
35
                'and @wysiwyg:<NAME> tags. For example: @wysiwyg @wysiwyg:CKEditor'
36
            );
37
        }
38
39
        try {
40
            $this->wysiwyg = Wysiwyg::instantiate($wysiwyg, $arguments);
41
        } catch (\Exception $e) {
42
            new Message('comment', 4, [
43
                'To describe a new editor you must create an object which will be extended',
44
                'by "%s" abstraction.',
45
            ], [
46
                Wysiwyg::class,
47
            ]);
48
49
            new Message('error', 4, ['%s'], [$e->getMessage()]);
50
51
            throw new \Exception(sprintf('The WYSIWYG editor "%s" does not exist.', $wysiwyg));
52
        }
53
    }
54
55
    /**
56
     * @throws \Exception
57
     *
58
     * @return Wysiwyg
59
     */
60
    protected function getEditor()
61
    {
62
        if (null === $this->wysiwyg) {
63
            throw new \Exception('The WYSIWYG editor was not set.');
64
        }
65
66
        return $this->wysiwyg;
67
    }
68
}
69