Completed
Push — master ( 2ec2e2...476664 )
by Joachim
05:23
created

CallbackFactory::create()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 9
nc 3
nop 1
dl 0
loc 16
ccs 6
cts 6
cp 1
crap 3
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Loevgaard\AltaPay\Callback;
4
5
use Loevgaard\AltaPay\Callback\Form as FormCallback;
6
use Loevgaard\AltaPay\Callback\Redirect as RedirectCallback;
7
use Loevgaard\AltaPay\Callback\Xml as XmlCallback;
8
use Psr\Http\Message\ServerRequestInterface;
9
10
class CallbackFactory
11
{
12
    /**
13
     * Will take a Psr Server Request and return a Form, Xml or Redirect
14
     * callback object that represent the actual callback
15
     *
16
     * @param ServerRequestInterface $request
17
     * @throws \InvalidArgumentException
18
     * @return CallbackInterface
19
     */
20 12
    public function create(ServerRequestInterface $request) : CallbackInterface
21
    {
22
        $callbacks = [
23 12
            XmlCallback::class,
24
            FormCallback::class,
25
            RedirectCallback::class
26
        ];
27
28 12
        foreach ($callbacks as $callback) {
29 12
            if (call_user_func([$callback, 'initable'], $request)) {
30 12
                return new $callback($request);
31
            }
32
        }
33
34 3
        throw new \InvalidArgumentException('A callback could not be instantiated');
35
    }
36
}
37