Completed
Branch feature/factory (c69aa7)
by Alex
03:00
created

Factory::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 22
ccs 7
cts 7
cp 1
rs 9.2
cc 1
eloc 13
nc 1
nop 2
crap 1
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 3
    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 3
        $factory = new static();
49
        
50 3
        $factory->setClientBuilder(
51 3
                $factory->getBuilder($clientConfig['builder'], $factory->extractConfig($clientConfig)))
52 3
                ->setLoggerBuilder(
53 3
                $factory->getBuilder($loggerConfig['builder'],$factory->extractConfig($loggerConfig)));
54
                
55
                
56 3
        return $factory;        
57
    }
58
    
59
    /**
60
     * @param $builderConfig
61
     * @return array
62
     */
63 5
    public function extractConfig(array $builderConfig)
64
    {
65 5
        return isset($builderConfig['config']) ? $builderConfig['config']:[];
66
    }
67
    
68
    /**
69
     * @return \FeedIo\FeedIo
70
     */
71 2
    public function getFeedIo()
72
    {
73 2
        return new FeedIo(
74 2
            $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...
75 2
            $this->loggerBuilder->getLogger()
76 2
            );
77
    }
78
79 5
    public function getBuilder($builder, array $args = [])
80
    {
81 5
        $class = "\FeedIo\Factory\Builder\\{$builder}Builder";
82
        
83 5
        if ( ! class_exists($class) ) {
84 1
            $class = $builder;
85 1
        }
86
        
87 5
        $reflection = new \ReflectionClass($class);
88
        
89 5
        return $reflection->newInstanceArgs($args);
90
    }
91
    
92 5
    public function setLoggerBuilder(LoggerBuilderInterface $loggerBuilder)
93
    {
94 5
        $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...
95
        
96 5
        return $this;
97
    }
98
    
99 5
    public function setClientBuilder(ClientBuilderInterface $clientBuilder)
100
    {
101 5
        $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...
102
    
103 5
        return $this;
104
    }
105
    
106
    /**
107
     * @param  BuilderInterface $builder
108
     * @return boolean true if the dependency is met
109
     */
110 2
    public function checkDependency(BuilderInterface $builder)
111
    {
112 2
        if ( ! class_exists($builder->getMainClassName()) ) {
113 1
            $message = "missing {$builder->getPackageName()}, please install it using composer : composer require {$builder->getPackageName()}";
114 1
            throw new MissingDependencyException($message);
115
        }
116
        
117 1
        return true;
118
    }
119
}
120