Completed
Push — master ( 3407e1...79fe03 )
by Nate
07:26
created

TransformerHelper   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 10
lcom 0
cbo 1
dl 0
loc 54
ccs 0
cts 29
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A isTransformerClass() 0 9 4
A isTransformerClassArray() 0 8 2
A resolveTransformer() 0 18 4
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://github.com/flipbox/salesforce/blob/master/LICENSE.md
6
 * @link       https://github.com/flipbox/salesforce
7
 */
8
9
namespace flipbox\craft\salesforce\helpers;
10
11
use Craft;
12
13
/**
14
 * @author Flipbox Factory <[email protected]>
15
 * @since 1.1.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