Completed
Pull Request — master (#26)
by
unknown
01:44
created

ListenerBuilder::build()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 8
rs 9.4285
cc 1
eloc 5
nc 1
nop 0
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
                'httplug.client' => function () {
30
                    if (class_exists('\Http\Discovery\HttpClientDiscovery')) {
31
                        return \Http\Discovery\HttpClientDiscovery::find();
32
                    }
33
34
                    if (class_exists('\Http\Adapter\Guzzle6\Client')) {
35
                        return new \Http\Adapter\Guzzle6\Client();
36
                    }
37
38
                    if (class_exists('\Http\Adapter\Guzzle5\Client')) {
39
                        return new \Http\Adapter\Guzzle5\Client();
40
                    }
41
42
                    if (class_exists('\Http\Client\Socket\Client')) {
43
                        return new \Http\Client\Socket\Client();
44
                    }
45
46
                    if (class_exists('\Http\Client\Curl\Client')) {
47
                        return new \Http\Client\Curl\Client();
48
                    }
49
50
                    if (class_exists('Http\Adapter\Buzz\Client')) {
51
                        return new \Http\Adapter\Buzz\Client();
52
                    }
53
54
                    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.');
55
                },
56
                'httplug.message_factory' => function () {
57
                    if (class_exists('\Http\Discovery\MessageFactoryDiscovery')) {
58
                        return \Http\Discovery\MessageFactoryDiscovery::find();
59
                    }
60
61
                    if (class_exists('\Zend\Diactoros\Request')) {
62
                        return new DiactorosMessageFactory();
63
                    }
64
65
                    if (class_exists('\GuzzleHttp\Psr7\Request')) {
66
                        return new GuzzleMessageFactory();
67
                    }
68
69
                    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.');
70
                },
71
                'httplug.stream_factory' => function () {
72
                    if (class_exists('\Http\Discovery\StreamFactoryDiscovery')) {
73
                        return \Http\Discovery\StreamFactoryDiscovery::find();
74
                    }
75
76
                    if (class_exists('\Zend\Diactoros\Stream')) {
77
                        return new DiactorosStreamFactory();
78
                    }
79
80
                    if (function_exists('\GuzzleHttp\Psr7\stream_for')) {
81
                        return new GuzzleStreamFactory();
82
                    }
83
84
                    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.');
85
                },
86
            ])
87
            ->setAllowedTypes('use_sandbox', 'bool')
88
            ->setAllowedTypes('httplug.client', '\Http\Client\HttpClient')
89
            ->setAllowedTypes('httplug.message_factory', '\Http\Message\MessageFactory')
90
            ->setAllowedTypes('httplug.stream_factory', '\Http\Message\StreamFactory');
91
    }
92
93
    /**
94
     * @return Listener
95
     */
96
    public function build()
97
    {
98
        return new Listener(
99
            $this->options['httplug.stream_factory'],
100
            $this->getVerifier(),
101
            $this->getEventDispatcher()
102
        );
103
    }
104
105
    /**
106
     * @return Verifier
107
     */
108
    private function getVerifier()
109
    {
110
        return new Verifier(
111
            $this->options['httplug.client'],
112
            $this->options['httplug.message_factory'],
113
            $this->options['use_sandbox']
114
        );
115
    }
116
117
    /**
118
     * @return EventDispatcher
119
     */
120
    private function getEventDispatcher()
121
    {
122
        return new EventDispatcher();
123
    }
124
}
125