Completed
Push — master ( c30a0b...37c0e6 )
by Nate
08:20
created

TransformerHelper::resolveCollection()   B

Complexity

Conditions 7
Paths 13

Size

Total Lines 37

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 0
Metric Value
dl 0
loc 37
ccs 0
cts 29
cp 0
rs 8.3946
c 0
b 0
f 0
cc 7
nc 13
nop 2
crap 56
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://flipboxfactory.com/software/hubspot/license
6
 * @link       https://www.flipboxfactory.com/software/hubspot/
7
 */
8
9
namespace flipbox\craft\hubspot\helpers;
10
11
use Craft;
12
13
/**
14
 * @author Flipbox Factory <[email protected]>
15
 * @since 1.0.0
16
 */
17
class TransformerHelper
18
{
19
    /**
20
     * @param $transformer
21
     * @return bool
22
     */
23
    public static function isTransformerClass($transformer): bool
24
    {
25
        return is_string($transformer) &&
26
            class_exists($transformer) &&
27
            (
28
                method_exists($transformer, '__invoke') ||
29
                is_callable([$transformer, '__invoke'])
30
            );
31
    }
32
33
    /**
34
     * @param $transformer
35
     * @return bool
36
     */
37
    public static function isTransformerClassArray($transformer): bool
38
    {
39
        if (!is_array($transformer)) {
40
            false;
41
        }
42
43
        return static::isTransformerClass($transformer['class'] ?? null);
44
    }
45
46
    /**
47
     * @noinspection PhpDocMissingThrowsInspection
48
     *
49
     * @param $transformer
50
     * @return callable|null
51
     */
52
    public static function resolveTransformer($transformer)
53
    {
54
        if (is_callable($transformer)) {
55
            return $transformer;
56
        }
57
58
        if (static::isTransformerClass($transformer)) {
59
            return new $transformer();
60
        }
61
62
        if (static::isTransformerClassArray($transformer)) {
63
            /** @noinspection PhpIncompatibleReturnTypeInspection */
64
            /** @noinspection PhpUnhandledExceptionInspection */
65
            return Craft::createObject($transformer);
66
        }
67
68
        return null;
69
    }
70
}
71