Completed
Push — master ( f70581...d40ba6 )
by arto
03:59
created

BuilderFactory::overwriteRequestFactory()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
/**
3
 * @author stev leibelt <[email protected]>
4
 * @since 2015-12-09
5
 */
6
namespace Net\Bazzline\Component\Curl\Builder;
7
8
use Net\Bazzline\Component\Curl\Dispatcher\DispatcherInterface;
9
use Net\Bazzline\Component\Curl\FactoryInterface;
10
use Net\Bazzline\Component\Curl\Request\Request;
11
use Net\Bazzline\Component\Curl\Request\RequestFactory;
12
use Net\Bazzline\Component\Toolbox\HashMap\Merge;
13
14
class BuilderFactory implements FactoryInterface
15
{
16
    /** @var DispatcherInterface */
17
    private $dispatcher;
18
19
    /** @var RequestFactory */
20
    private $factory;
21
22
    /**
23
     * @return Builder|mixed
24
     */
25
    public function create()
26
    {
27
        $builder = new Builder(
28
            $this->createRequestFromFactory(),
29
            new Merge()
30
        );
31
32
        return $builder;
33
    }
34
35
    /**
36
     * @param DispatcherInterface $dispatcher
37
     */
38
    public function overwriteDispatcher(DispatcherInterface $dispatcher)
39
    {
40
        $this->dispatcher = $dispatcher;
41
    }
42
43
    /**
44
     * @param RequestFactory $factory
45
     */
46
    public function overwriteRequestFactory(RequestFactory $factory)
47
    {
48
        $this->factory = $factory;
49
    }
50
51
    /**
52
     * @return Request
53
     */
54
    protected function createRequestFromFactory()
55
    {
56
        $dispatcher         = $this->dispatcher;
57
        $factory            = $this->factory;
58
        $isInvalidFactory   = ($this->factory instanceof RequestFactory);
59
        $isValidDispatcher  = ($dispatcher instanceof DispatcherInterface);
60
61
        if ($isInvalidFactory) {
62
            $this->factory  = new RequestFactory();
63
            $factory        = $this->factory;
64
        }
65
66
        if ($isValidDispatcher) {
67
            $factory->overwriteDispatcher($dispatcher);
68
        }
69
70
        return $factory->create();
71
    }
72
}
73