Completed
Push — master ( ce1278...2488b5 )
by Nate
05:22 queued 04:08
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 TransformerCollectionInterface|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());
54
        $this->runner = $runner;
55
        $this->transformer = $transformer;
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),
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