Completed
Push — feature/factory ( 6c3fab...15be70 )
by Alex
08:16
created

Factory::create()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 24
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 24
rs 8.9713
cc 3
eloc 15
nc 4
nop 2
1
<?php
2
/*
3
 * This file is part of the feed-io package.
4
 *
5
 * (c) Alexandre Debril <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace FeedIo;
12
13
use FeedIo\Factory\MissingDependencyException;
14
use FeedIo\Factory\LoggerBuilderInterface;
15
use FeedIo\Factory\ClientBuilderInterface;
16
use FeedIo\Factory\BuilderInterface;
17
18
class Factory
19
{
20
   
21
    /**
22
     * @var \Psr\Log\LoggerInterface
23
     */
24
    protected $logger;
25
    
26
    /**
27
     * @var \FeedIo\Adapter\ClientInterface
28
     */
29
    protected $client;
30
    
31
    /**
32
     * @var FeedIo\Factory\LoggerBuilderInterface
33
     */
34
    protected $loggerBuilder;
35
    
36
    static public function create(
37
            array $loggerConfig = [
38
                    'builder' => 'NullLogger',
39
                    'config' => [],
40
            ],
41
            array $clientConfig = [
42
                    'builder' => 'GuzzleClient',
43
                    'config' => [],
44
            ]
45
46
    )
47
    {
48
        $factory = new static();
49
        $clientConfig['config'] = isset($clientConfig['config']) ? $clientConfig['config']:[];
50
        $loggerConfig['config'] = isset($loggerConfig['config']) ? $loggerConfig['config']:[];
51
        
52
        $factory->setClientBuilder(
53
                $factory->getBuilder($clientConfig['builder'], $clientConfig['config']))
54
                ->setLoggerBuilder(
55
                $factory->getBuilder($loggerConfig['builder'], $loggerConfig['config']));
56
                
57
                
58
        return $factory;        
59
    }
60
    
61
    /**
62
     * @return \FeedIo\FeedIo
63
     */
64
    public function getFeedIo()
65
    {
66
        return new FeedIo(
67
            $this->clientBuilder->getClient(),
0 ignored issues
show
Bug introduced by
The property clientBuilder does not seem to exist. Did you mean client?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
68
            $this->loggerBuilder->getLogger()
69
            );
70
    }
71
72
    public function getBuilder($builder, array $args = [])
73
    {
74
        $class = "\FeedIo\Factory\Builder\\{$builder}Builder";
75
        
76
        if ( ! class_exists($class) ) {
77
            $class = $builder;
78
        }
79
        
80
        $reflection = new \ReflectionClass($class);
81
        
82
        return $reflection->newInstanceArgs($args);
83
    }
84
    
85
    public function setLoggerBuilder(LoggerBuilderInterface $loggerBuilder)
86
    {
87
        $this->loggerBuilder = $loggerBuilder;
0 ignored issues
show
Documentation Bug introduced by
It seems like $loggerBuilder of type object<FeedIo\Factory\LoggerBuilderInterface> is incompatible with the declared type object<FeedIo\FeedIo\Fac...LoggerBuilderInterface> of property $loggerBuilder.

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...
88
        
89
        return $this;
90
    }
91
    
92
    public function setClientBuilder(ClientBuilderInterface $clientBuilder)
93
    {
94
        $this->clientBuilder = $clientBuilder;
0 ignored issues
show
Bug introduced by
The property clientBuilder does not seem to exist. Did you mean client?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
95
    
96
        return $this;
97
    }
98
    
99
    /**
100
     * @param  BuilderInterface $builder
101
     * @return boolean true if the dependency is met
102
     */
103
    public function checkDependency(BuilderInterface $builder)
104
    {
105
        if ( ! class_exists($builder->getMainClassName()) ) {
106
            $message = "missing {$builder->getPackageName()}, please install it using composer : composer require {$builder->getPackageName()}";
107
            throw new MissingDependencyException($message);
108
        }
109
        
110
        return true;
111
    }
112
}
113