Completed
Pull Request — master (#26)
by
unknown
07:04
created

ListenerBuilder::configureOptions()   C

Complexity

Conditions 13
Paths 1

Size

Total Lines 72
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 72
rs 5.5073
cc 13
eloc 41
nc 1
nop 1

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Mdb\PayPal\Ipn;
4
5
use Http\Message\MessageFactory\DiactorosMessageFactory;
6
use Http\Message\MessageFactory\GuzzleMessageFactory;
7
use Http\Message\StreamFactory\DiactorosStreamFactory;
8
use Http\Message\StreamFactory\GuzzleStreamFactory;
9
use Symfony\Component\EventDispatcher\EventDispatcher;
10
use Symfony\Component\OptionsResolver\OptionsResolver;
11
12
class ListenerBuilder
13
{
14
    protected $options;
15
16
    public function __construct(array $options = [])
17
    {
18
        $resolver = new OptionsResolver();
19
        $this->configureOptions($resolver);
20
21
        $this->options = $resolver->resolve($options);
22
    }
23
24
    public function configureOptions(OptionsResolver $resolver)
25
    {
26
        $resolver
27
            ->setDefaults([
28
                'use_sandbox' => false,
29
                'event_dispatcher' => function () {
30
                    return new EventDispatcher();
31
                },
32
                'httplug.client' => function () {
33
                    if (class_exists('\Http\Discovery\HttpClientDiscovery')) {
34
                        return \Http\Discovery\HttpClientDiscovery::find();
35
                    }
36
37
                    if (class_exists('\Http\Adapter\Guzzle6\Client')) {
38
                        return new \Http\Adapter\Guzzle6\Client();
39
                    }
40
41
                    if (class_exists('\Http\Adapter\Guzzle5\Client')) {
42
                        return new \Http\Adapter\Guzzle5\Client();
43
                    }
44
45
                    if (class_exists('\Http\Client\Socket\Client')) {
46
                        return new \Http\Client\Socket\Client();
47
                    }
48
49
                    if (class_exists('\Http\Client\Curl\Client')) {
50
                        return new \Http\Client\Curl\Client();
51
                    }
52
53
                    if (class_exists('\Http\Adapter\Buzz\Client')) {
54
                        return new \Http\Adapter\Buzz\Client();
55
                    }
56
57
                    throw new \LogicException('The httplug.client could not be guessed. Install one of the following packages: php-http/guzzle6-adapter. You can also overwrite the config option with your implementation.');
58
                },
59
                'httplug.message_factory' => function () {
60
                    if (class_exists('\Http\Discovery\MessageFactoryDiscovery')) {
61
                        return \Http\Discovery\MessageFactoryDiscovery::find();
62
                    }
63
64
                    if (class_exists('\Zend\Diactoros\Request')) {
65
                        return new DiactorosMessageFactory();
66
                    }
67
68
                    if (class_exists('\GuzzleHttp\Psr7\Request')) {
69
                        return new GuzzleMessageFactory();
70
                    }
71
72
                    throw new \LogicException('The httplug.message_factory could not be guessed. Install one of the following packages: php-http/guzzle6-adapter. You can also overwrite the config option with your implementation.');
73
                },
74
                'httplug.stream_factory' => function () {
75
                    if (class_exists('\Http\Discovery\StreamFactoryDiscovery')) {
76
                        return \Http\Discovery\StreamFactoryDiscovery::find();
77
                    }
78
79
                    if (class_exists('\Zend\Diactoros\Stream')) {
80
                        return new DiactorosStreamFactory();
81
                    }
82
83
                    if (function_exists('\GuzzleHttp\Psr7\stream_for')) {
84
                        return new GuzzleStreamFactory();
85
                    }
86
87
                    throw new \LogicException('The httplug.stream_factory could not be guessed. Install one of the following packages: php-http/guzzle6-adapter. You can also overwrite the config option with your implementation.');
88
                },
89
            ])
90
            ->setAllowedTypes('use_sandbox', 'bool')
91
            ->setAllowedTypes('event_dispatcher', '\Symfony\Component\EventDispatcher\EventDispatcher')
92
            ->setAllowedTypes('httplug.client', '\Http\Client\HttpClient')
93
            ->setAllowedTypes('httplug.message_factory', '\Http\Message\MessageFactory')
94
            ->setAllowedTypes('httplug.stream_factory', '\Http\Message\StreamFactory');
95
    }
96
97
    /**
98
     * @return Listener
99
     */
100
    public function build()
101
    {
102
        return new Listener(
103
            $this->options['httplug.stream_factory'],
104
            $this->getVerifier(),
105
            $this->options['event_dispatcher']
106
        );
107
    }
108
109
    /**
110
     * @return Verifier
111
     */
112
    private function getVerifier()
113
    {
114
        return new Verifier(
115
            $this->options['httplug.client'],
116
            $this->options['httplug.message_factory'],
117
            $this->options['use_sandbox']
118
        );
119
    }
120
}
121