Completed
Push — develop ( f725fa...2499d2 )
by Nate
01:45
created

Resource::createPipeline()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 0
cts 8
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 1
crap 2
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\Salesforce\Resources;
10
11
use Flipbox\Pipeline\Builders\BuilderTrait;
12
use Flipbox\Relay\Runner\Runner;
13
use Flipbox\Salesforce\Pipeline\Pipelines\HttpPipeline;
14
use Flipbox\Salesforce\Pipeline\Stages\TransformerCollectionStage;
15
use Flipbox\Salesforce\Salesforce;
16
use Flipbox\Salesforce\Transformers\Collections\TransformerCollectionInterface;
17
use Flipbox\Skeleton\Object\AbstractObject;
18
use League\Pipeline\PipelineBuilderInterface;
19
use Psr\Log\LoggerInterface;
20
21
/**
22
 * A Relay pipeline builder intended to make building the Relay and Pipeline easier.
23
 *
24
 * @author Flipbox Factory <[email protected]>
25
 * @since 1.0.0
26
 */
27
class Resource extends AbstractObject implements PipelineBuilderInterface
28
{
29
    use BuilderTrait;
30
31
    /**
32
     * @var Runner
33
     */
34
    protected $runner;
35
36
    /**
37
     * @var TransformerCollectionStage|null
38
     */
39
    protected $transformer;
40
41
    /**
42
     * @param Runner $runner
43
     * @param TransformerCollectionInterface|null $transformer
44
     * @param LoggerInterface|null $logger
45
     * @param array $config
46
     */
47
    public function __construct(
48
        Runner $runner,
49
        TransformerCollectionInterface $transformer = null,
50
        LoggerInterface $logger = null,
51
        array $config = []
52
    ) {
53
        $this->setLogger($logger ?: Salesforce::getLogger());
1 ignored issue
show
Documentation Bug introduced by
The method setLogger does not exist on object<Flipbox\Salesforce\Resources\Resource>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
54
        $this->runner = $runner;
55
        $this->transformer = $transformer;
0 ignored issues
show
Documentation Bug introduced by
It seems like $transformer can also be of type object<Flipbox\Salesforc...merCollectionInterface>. However, the property $transformer is declared as type object<Flipbox\Salesforc...erCollectionStage>|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...
56
57
        parent::__construct($config);
58
    }
59
60
    /**
61
     * @inheritdoc
62
     */
63
    protected function createPipeline(array $config = []): HttpPipeline
64
    {
65
        return new HttpPipeline(
66
            $this->runner,
67
            $this->createTransformerStage($this->transformer),
0 ignored issues
show
Bug introduced by
It seems like $this->transformer can also be of type object<Flipbox\Salesforc...sformerCollectionStage>; however, Flipbox\Salesforce\Resou...reateTransformerStage() does only seem to accept null|object<Flipbox\Sale...merCollectionInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
68
            $config
69
        );
70
    }
71
72
    /**
73
     * @param null $source
74
     * @return mixed
75
     */
76
    public function execute($source = null)
77
    {
78
        // Resources do not pass a payload ... but they can pass a source, so that why this may look funny
79
        return call_user_func_array($this->build(), [null, $source]);
80
    }
81
82
    /**
83
     * @param mixed|null $source
84
     * @return mixed
85
     */
86
    public function __invoke($source = null)
87
    {
88
        return $this->execute($source);
89
    }
90
91
    /**
92
     * @param TransformerCollectionInterface|null $transformer
93
     * @return TransformerCollectionStage|null
94
     */
95
    private function createTransformerStage(
96
        TransformerCollectionInterface $transformer = null
97
    ) {
98
        if ($transformer === null) {
99
            return null;
100
        }
101
102
        return new TransformerCollectionStage($transformer);
103
    }
104
}
105