TransformerHelper   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 64
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 64
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://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
     * The sync payload action name
21
     */
22
    const PAYLOAD_ACTION_SYNC = 'sync';
23
24
    /**
25
     * The sync payload action name
26
     */
27
    const PAYLOAD_ACTION_SAVE = 'save';
28
29
    /**
30
     * @param $transformer
31
     * @return bool
32
     */
33
    public static function isTransformerClass($transformer): bool
34
    {
35
        return is_string($transformer) &&
36
            class_exists($transformer) &&
37
            (
38
                method_exists($transformer, '__invoke') ||
39
                is_callable([$transformer, '__invoke'])
40
            );
41
    }
42
43
    /**
44
     * @param $transformer
45
     * @return bool
46
     */
47
    public static function isTransformerClassArray($transformer): bool
48
    {
49
        if (!is_array($transformer)) {
50
            false;
51
        }
52
53
        return static::isTransformerClass($transformer['class'] ?? null);
54
    }
55
56
    /**
57
     * @noinspection PhpDocMissingThrowsInspection
58
     *
59
     * @param $transformer
60
     * @return callable|null
61
     */
62
    public static function resolveTransformer($transformer)
63
    {
64
        if (is_callable($transformer)) {
65
            return $transformer;
66
        }
67
68
        if (static::isTransformerClass($transformer)) {
69
            return new $transformer();
70
        }
71
72
        if (static::isTransformerClassArray($transformer)) {
73
            /** @noinspection PhpIncompatibleReturnTypeInspection */
74
            /** @noinspection PhpUnhandledExceptionInspection */
75
            return Craft::createObject($transformer);
76
        }
77
78
        return null;
79
    }
80
}
81