Completed
Push — develop ( f37ae4...1d947e )
by Nate
18:30
created

TransformerHelper::populateTransformerCollection()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
dl 0
loc 23
ccs 0
cts 20
cp 0
rs 9.2408
c 0
b 0
f 0
cc 5
nc 5
nop 2
crap 30
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\hubspot\helpers;
10
11
use Flipbox\Skeleton\Helpers\ObjectHelper;
12
use Flipbox\Transform\Transformers\TransformerInterface;
13
use flipbox\force\Force;
14
use flipbox\hubspot\transformers\collections\TransformerCollectionInterface;
15
16
/**
17
 * @author Flipbox Factory <[email protected]>
18
 * @since 1.0.0
19
 */
20
class TransformerHelper extends \Flipbox\Transform\Helpers\TransformerHelper
21
{
22
    /**
23
     * @param array|TransformerInterface|callable|null $transformer
24
     * @return TransformerInterface|callable|null
25
     * @throws \Flipbox\Skeleton\Exceptions\InvalidConfigurationException
26
     */
27
    public static function resolve($transformer = null)
28
    {
29
        if (empty($transformer)) {
30
            return null;
31
        }
32
33
        if (is_string($transformer) || is_array($transformer)) {
34
            return static::resolve(ObjectHelper::create($transformer));
35
        }
36
37
        return parent::resolve($transformer);
38
    }
39
40
    /**
41
     * @param $transformer
42
     * @return bool
43
     */
44
    public static function isTransformerCollection($transformer): bool
45
    {
46
        return $transformer instanceof TransformerCollectionInterface;
47
    }
48
49
    /**
50
     * @param $transformer
51
     * @return bool
52
     */
53
    public static function isTransformerCollectionClass($transformer): bool
54
    {
55
        return is_string($transformer) && is_subclass_of($transformer, TransformerCollectionInterface::class);
1 ignored issue
show
Bug introduced by
Due to PHP Bug #53727, is_subclass_of might return inconsistent results on some PHP versions if \flipbox\hubspot\transfo...lectionInterface::class can be an interface. If so, you could instead use ReflectionClass::implementsInterface.
Loading history...
56
    }
57
58
    /**
59
     * @param TransformerCollectionInterface|array|string|null $transformer
60
     * @return TransformerCollectionInterface|null
61
     */
62
    public static function resolveCollection($transformer = null)
63
    {
64
        if (empty($transformer)) {
65
            return null;
66
        }
67
68
        if (null !== ($collection = static::returnCollectionFromTransformer($transformer))) {
69
            return $collection;
70
        }
71
72
        if (is_array($transformer)) {
73
            try {
74
                $class = ObjectHelper::checkConfig($transformer, TransformerCollectionInterface::class);
75
76
                /** @var TransformerCollectionInterface $collection */
77
                $collection = new $class();
78
79
                static::populateTransformerCollection(
80
                    $collection,
81
                    $transformer
82
                );
83
84
                return $collection;
85
            } catch (\Throwable $e) {
0 ignored issues
show
Bug introduced by
The class Throwable does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
86
                Force::warning(sprintf(
87
                    "An exception was thrown while trying to resolve transformer collection: '%s'",
88
                    (string)$e->getMessage()
89
                ));
90
            }
91
        }
92
93
        return null;
94
    }
95
96
    /**
97
     * @param TransformerCollectionInterface|string $transformer
98
     * @return null|TransformerCollectionInterface
99
     */
100
    protected static function returnCollectionFromTransformer($transformer)
101
    {
102
        if (static::isTransformerCollection($transformer)) {
103
            return $transformer;
104
        }
105
106
        if (static::isTransformerCollectionClass($transformer)) {
107
            return new $transformer();
108
        }
109
110
        return null;
111
    }
112
113
114
    /**
115
     * @param TransformerCollectionInterface|null $collection
116
     * @param array $config
117
     * @return TransformerCollectionInterface|null
118
     */
119
    public static function populateTransformerCollection(
120
        TransformerCollectionInterface $collection = null,
121
        array $config = []
122
    ) {
123
        if ($collection === null) {
124
            return $collection;
125
        }
126
127
        foreach ($config as $name => $value) {
128
            $setter = 'set' . $name;
129
            if (method_exists($collection, $setter)) {
130
                $collection->$setter($value);
131
                continue;
132
            }
133
134
            if (property_exists($collection, $name)) {
135
                $collection->{$name} = $value;
136
                continue;
137
            }
138
        }
139
140
        return $collection;
141
    }
142
143
}
144