Completed
Push — develop ( 951c22...71e040 )
by Nate
03:40
created

TransformerHelper::eventName()   A

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
dl 0
loc 8
c 0
b 0
f 0
ccs 0
cts 7
cp 0
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
crap 6
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://flipboxfactory.com/software/flux/license
6
 * @link       https://www.flipboxfactory.com/software/flux/
7
 */
8
9
namespace flipbox\flux\helpers;
10
11
use Craft;
12
use craft\helpers\ArrayHelper;
13
use craft\helpers\StringHelper;
14
use flipbox\flux\Flux;
15
use Flipbox\Transform\Helpers\TransformerHelper as BaseTransformerHelper;
16
use Flipbox\Transform\Transformers\TransformerInterface;
17
use yii\base\InvalidConfigException;
18
19
/**
20
 * @author Flipbox Factory <[email protected]>
21
 * @since 1.0.0
22
 */
23
class TransformerHelper extends BaseTransformerHelper
24
{
25
    /**
26
     * @param $name
27
     * @return string
28
     */
29
    public static function eventName($name)
30
    {
31
        if(!is_array($name)) {
32
            $name = ArrayHelper::toArray($name);
33
        }
34
35
        return StringHelper::toString(array_filter($name), ':');
36
    }
37
38
    /**
39
     * @param $transformer
40
     * @return bool
41
     */
42
    public static function isTransformerConfig($transformer)
43
    {
44
        if (!is_array($transformer)) {
45
            return false;
46
        }
47
48
        if ($class = ArrayHelper::getValue($transformer, 'class')) {
49
            return false;
50
        }
51
52
        return static::isTransformerClass($class);
53
    }
54
55
    /**
56
     * @param $transformer
57
     * @return null|callable|TransformerInterface
58
     */
59
    public static function resolve($transformer)
60
    {
61
        if (null !== ($callable = parent::resolve($transformer))) {
62
            return $callable;
63
        }
64
65
        try {
66
            if (static::isTransformerConfig($transformer)) {
67
                return static::resolve(
68
                    Craft::createObject($transformer)
69
                );
70
            }
71
        } catch (InvalidConfigException $e) {
72
            Flux::warning("Invalid transformer configuration.");
73
        }
74
75
        return null;
76
    }
77
}
78