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

BuilderFactory   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 4
Bugs 0 Features 2
Metric Value
wmc 6
c 4
b 0
f 2
lcom 1
cbo 3
dl 0
loc 59
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 9 1
A overwriteDispatcher() 0 4 1
A overwriteRequestFactory() 0 4 1
A createRequestFromFactory() 0 18 3
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