Completed
Push — feature/configuration ( 48ff2d...765854 )
by Stéphane
53:12 queued 25:50
created

RequestFactory::build()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 26
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 4

Importance

Changes 4
Bugs 2 Features 0
Metric Value
c 4
b 2
f 0
dl 0
loc 26
ccs 19
cts 19
cp 1
rs 8.5806
cc 4
eloc 16
nc 3
nop 3
crap 4
1
<?php
2
/**
3
 * This file is part of the bee4/transport package.
4
 * For the full copyright and license information, please view the LICENSE
5
 * file that was distributed with this source code.
6
 *
7
 * @copyright Bee4 2015
8
 * @author  Stephane HULARD <[email protected]>
9
 * @package Bee4\Transport\Message\Request
10
 */
11
12
namespace Bee4\Transport\Message\Request;
13
14
use Bee4\Transport\Configuration\Configuration;
15
use Bee4\Transport\Exception\InvalidArgumentException;
16
use Bee4\Transport\Exception\UnknownProtocolException;
17
use Bee4\Transport\Url;
18
19
/**
20
 * Static object in charge to build and initialize Request instance
21
 * @package Bee4\Transport\Message\Request
22
 */
23
class RequestFactory
24
{
25
    /**
26
     * Configuration to use when building Request
27
     * @var Configuration
28
     */
29
    protected $configuration;
30
31
    /**
32
     * Initialize the Request factory
33
     * @param Configuration|null $config
34
     */
35 16
    public function __construct(Configuration $config = null)
36
    {
37 16
        $this->configuration = $config;
38 16
    }
39
40
    /**
41
     * Build a new request from parameters
42
     * @param string $method
43
     * @param Url $url
44
     * @param array $headers
45
     * @return AbstractRequest
46
     * @throws UnknownProtocolException
47
     * @throws InvalidArgumentException
48
     */
49 13
    public function build($method, Url $url, array $headers)
50
    {
51 13
        if (($scheme = self::isAllowedScheme((string)$url->scheme())) === false) {
52 1
            throw new UnknownProtocolException(sprintf(
53 1
                "You can't request a transfer on unsupported protocol '%s'!",
54 1
                $url->scheme()
55 1
            ));
56
        }
57
58 12
        $scheme = ucfirst($scheme);
59 12
        $name = __NAMESPACE__.'\\'.$scheme.'\\'.ucfirst(strtolower($method));
60 12
        if (!class_exists($name)) {
61 1
            throw new InvalidArgumentException('Method given is not a valid request: '.$method);
62
        }
63
64 11
        $configurationClass = '\Bee4\Transport\Configuration\\'.$scheme.'Configuration';
65 11
        $request = new $name(
66 11
            $url,
67 11
            $headers,
68 11
            new $configurationClass(
69 11
                $this->configuration!==null?$this->configuration->toArray():[]
70 11
            )
71 11
        );
72
73 11
        return $request;
74
    }
75
76
    /**
77
     * Validate scheme by checking if its an allowed one
78
     * @param string $scheme
79
     * @return boolean|string If invalid, false, else return the request known one
80
     */
81 13
    private static function isAllowedScheme($scheme)
82
    {
83 13
        if (preg_match(AbstractRequest::HTTP, $scheme) === 1) {
84 11
            return 'http';
85 2
        } elseif (preg_match(AbstractRequest::FTP, $scheme) === 1) {
86 1
            return 'ftp';
87 1
        } elseif (preg_match(AbstractRequest::SSH, $scheme) === 1) {
88
            return 'ssh';
89
        } else {
90 1
            return false;
91
        }
92
    }
93
}
94