Factory::setClientBuilder()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php declare(strict_types=1);
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\ClientBuilderInterface
33
     */
34
    protected $clientBuilder;
35
36
    /**
37
     * @var \FeedIo\Factory\LoggerBuilderInterface
38
     */
39
    protected $loggerBuilder;
40
41
    /**
42
     * @param array $loggerConfig
43
     * @param array $clientConfig
44
     * @return Factory
45
     */
46 3
    public static function create(
47
        array $loggerConfig = [
48
            'builder' => 'NullLogger',
49
            'config' => [],
50
        ],
51
        array $clientConfig = [
52
            'builder' => 'GuzzleClient',
53
            'config' => [],
54
        ]
55
    ) : Factory {
56 3
        $factory = new static();
57
58 3
        $factory->setClientBuilder(
59 3
            $factory->getBuilder($clientConfig['builder'], $factory->extractConfig($clientConfig))
0 ignored issues
show
Compatibility introduced by
$factory->getBuilder($cl...tConfig($clientConfig)) of type object<FeedIo\Factory\BuilderInterface> is not a sub-type of object<FeedIo\Factory\ClientBuilderInterface>. It seems like you assume a child interface of the interface FeedIo\Factory\BuilderInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
60
        )
61 3
            ->setLoggerBuilder(
62 3
                $factory->getBuilder($loggerConfig['builder'], $factory->extractConfig($loggerConfig))
0 ignored issues
show
Compatibility introduced by
$factory->getBuilder($lo...tConfig($loggerConfig)) of type object<FeedIo\Factory\BuilderInterface> is not a sub-type of object<FeedIo\Factory\LoggerBuilderInterface>. It seems like you assume a child interface of the interface FeedIo\Factory\BuilderInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
63
            );
64
65
66 3
        return $factory;
67
    }
68
69
    /**
70
     * @param ClientBuilderInterface $clientBuilder
71
     * @return Factory
72
     */
73 4
    public function setClientBuilder(ClientBuilderInterface $clientBuilder) : Factory
74
    {
75 4
        $this->clientBuilder = $clientBuilder;
76
77 4
        return $this;
78
    }
79
80
    /**
81
     * @param string $builder
82
     * @param array $args
83
     * @return BuilderInterface
84
     */
85 5
    public function getBuilder(string $builder, array $args = []) : BuilderInterface
86
    {
87 5
        $class = "\\FeedIo\\Factory\\Builder\\{$builder}Builder";
88
89 5
        if (!class_exists($class)) {
90 1
            $class = $builder;
91
        }
92
93 5
        $reflection = new \ReflectionClass($class);
94
95
        // Pass args only if constructor has
96 5
        return $reflection->newInstanceArgs([$args]);
97
    }
98
99
    /**
100
     * @param $builderConfig
101
     *
102
     * @return array
103
     */
104 5
    public function extractConfig(array $builderConfig) : array
105
    {
106 5
        return isset($builderConfig['config']) ? $builderConfig['config'] : [];
107
    }
108
109
    /**
110
     * @return \FeedIo\FeedIo
111
     */
112 3
    public function getFeedIo() : FeedIo
113
    {
114 3
        return new FeedIo(
115 3
            $this->clientBuilder->getClient(),
116 3
            $this->loggerBuilder->getLogger()
117
        );
118
    }
119
120
    /**
121
     * @param LoggerBuilderInterface $loggerBuilder
122
     *
123
     * @return Factory
124
     */
125 4
    public function setLoggerBuilder(LoggerBuilderInterface $loggerBuilder) : Factory
126
    {
127 4
        $this->loggerBuilder = $loggerBuilder;
128
129 4
        return $this;
130
    }
131
132
    /**
133
     * @param  BuilderInterface $builder
134
     *
135
     * @return boolean true if the dependency is met
136
     */
137 2
    public function checkDependency(BuilderInterface $builder) : bool
138
    {
139 2
        if (!class_exists($builder->getMainClassName())) {
140 1
            $message = "missing {$builder->getPackageName()}, please install it using composer : composer require {$builder->getPackageName()}";
141 1
            throw new MissingDependencyException($message);
142
        }
143
144 1
        return true;
145
    }
146
}
147