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

BaseCriteria::setTransformer()   A

Complexity

Conditions 5
Paths 7

Size

Total Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
dl 0
loc 27
ccs 0
cts 23
cp 0
rs 9.1768
c 0
b 0
f 0
cc 5
nc 7
nop 1
crap 30
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\hubspot\criteria;
10
11
use flipbox\hubspot\HubSpot;
12
use flipbox\hubspot\services\Cache;
13
use flipbox\hubspot\services\Connections;
14
use flipbox\hubspot\transformers\collections\DynamicTransformerCollection;
15
use flipbox\ember\helpers\ObjectHelper;
16
use flipbox\hubspot\connections\ConnectionInterface;
17
use flipbox\hubspot\helpers\TransformerHelper;
18
use flipbox\hubspot\transformers\collections\TransformerCollectionInterface;
19
use Psr\SimpleCache\CacheInterface;
20
use yii\base\BaseObject;
21
22
/**
23
 * @author Flipbox Factory <[email protected]>
24
 * @since 1.0.0
25
 */
26
class BaseCriteria extends BaseObject implements CriteriaInterface
27
{
28
    /**
29
     * @var ConnectionInterface|string
30
     */
31
    protected $connection = Connections::DEFAULT_CONNECTION;
32
33
    /**
34
     * @var CacheInterface|string|null
35
     */
36
    protected $cache = Cache::DEFAULT_CACHE;
37
38
    /**
39
     * @var TransformerCollectionInterface|null
40
     */
41
    protected $transformer = ['class' => DynamicTransformerCollection::class];
42
43
    /**
44
     * @param $value
45
     * @return $this
46
     */
47
    public function connection($value)
48
    {
49
        return $this->setConnection($value);
50
    }
51
52
    /**
53
     * @param $value
54
     * @return $this
55
     */
56
    public function setConnection($value)
57
    {
58
        $this->connection = $value;
59
        return $this;
60
    }
61
62
    /**
63
     * @inheritdoc
64
     */
65
    public function getConnection(): ConnectionInterface
66
    {
67
        if ($this->connection instanceof ConnectionInterface) {
68
            return $this->connection;
69
        }
70
71
        if ($this->connection === null) {
72
            $this->connection = Connections::DEFAULT_CONNECTION;
73
        }
74
75
        return $this->connection = HubSpot::getInstance()->getConnections()->get($this->connection);
76
    }
77
78
    /**
79
     * @param $value
80
     * @return $this
81
     */
82
    public function cache($value)
83
    {
84
        return $this->setCache($value);
85
    }
86
87
    /**
88
     * @param $value
89
     * @return $this
90
     */
91
    public function setCache($value)
92
    {
93
        $this->cache = $value;
94
        return $this;
95
    }
96
97
    /**
98
     * @inheritdoc
99
     */
100
    public function getCache(): CacheInterface
101
    {
102
        if ($this->cache instanceof CacheInterface) {
103
            return $this->cache;
104
        }
105
106
        if ($this->cache === null) {
107
            $this->cache = Cache::DEFAULT_CACHE;
108
        }
109
110
        return $this->cache = HubSpot::getInstance()->getCache()->get($this->cache);
111
    }
112
113
    /**
114
     * @param $value
115
     * @return $this
116
     */
117
    public function transformer($value)
118
    {
119
        return $this->setTransformer($value);
120
    }
121
122
    /**
123
     * @inheritdoc
124
     */
125
    public function setTransformer($value)
126
    {
127
        if (empty($value)) {
128
            $this->transformer = null;
129
            return $this;
130
        }
131
132
        if (is_string($value)) {
133
            if (TransformerHelper::isTransformerCollectionClass($value)) {
134
                $value = ['class' => $value];
135
            } else {
136
                $value = ['handle' => [$value]];
137
            }
138
        }
139
140
        if (array_key_exists('class', $value)) {
141
            $this->transformer = $value;
142
            return $this;
143
        }
144
145
        TransformerHelper::populateTransformerCollection(
146
            $this->getTransformer(),
0 ignored issues
show
Bug introduced by
It seems like $this->getTransformer() targeting flipbox\hubspot\criteria...teria::getTransformer() can also be of type object or string; however, flipbox\hubspot\helpers\...TransformerCollection() does only seem to accept null|object<flipbox\hubs...merCollectionInterface>, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
147
            $value
148
        );
149
150
        return $this;
151
    }
152
153
    /**
154
     * @inheritdoc
155
     */
156
    public function getTransformer()
157
    {
158
        if ($this->transformer === false) {
159
            return null;
160
        }
161
162
        if ($this->transformer instanceof TransformerCollectionInterface) {
163
            return $this->transformer;
164
        }
165
166
        // Prevent subsequent resolves (since it already didn't)
167
        if (null === ($this->transformer = TransformerHelper::resolveCollection($this->transformer))) {
1 ignored issue
show
Documentation Bug introduced by
It seems like \flipbox\hubspot\helpers...ion($this->transformer) can also be of type string. However, the property $transformer is declared as type object<flipbox\hubspot\t...llectionInterface>|null. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
168
            $this->transformer = false;
1 ignored issue
show
Documentation Bug introduced by
It seems like false of type false is incompatible with the declared type object<flipbox\hubspot\t...llectionInterface>|null of property $transformer.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
169
            return null;
170
        }
171
172
        return $this->transformer;
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->transformer; of type null|string|object adds the type string to the return on line 172 which is incompatible with the return type declared by the interface flipbox\hubspot\criteria...terface::getTransformer of type flipbox\hubspot\transfor...ollectionInterface|null.
Loading history...
173
    }
174
175
    /**
176
     * @inheritdoc
177
     */
178
    protected function prepare(array $criteria = [])
179
    {
180
        ObjectHelper::populate(
181
            $this,
182
            $criteria
183
        );
184
    }
185
}
186