Completed
Push — master ( 092247...6dd5e4 )
by Stéphane
06:20 queued 03:28
created

RequestFactory::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 6
ccs 3
cts 4
cp 0.75
rs 9.4285
cc 2
eloc 3
nc 2
nop 1
crap 2.0625
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
        if (null !== $config) {
38
            $this->setConfiguration($config);
39
        }
40 16
    }
41
42
    /**
43
     * Set configuration to use when building requests
44
     * @param Configuration $config
45
     * @return RequestFactory
46
     */
47
    public function setConfiguration(Configuration $config)
48
    {
49
        $this->configuration = $config;
50
    }
51
52
    /**
53
     * Build a new request from parameters
54
     * @param string $method
55
     * @param Url $url
56
     * @param array $headers
57
     * @return AbstractRequest
58
     * @throws UnknownProtocolException
59
     * @throws InvalidArgumentException
60
     */
61 13
    public function build($method, Url $url, array $headers)
62
    {
63 13
        if (($scheme = self::isAllowedScheme((string)$url->scheme())) === false) {
64 1
            throw new UnknownProtocolException(sprintf(
65 1
                "You can't request a transfer on unsupported protocol '%s'!",
66 1
                $url->scheme()
67
            ));
68
        }
69
70 12
        $scheme = ucfirst($scheme);
71 12
        $name = __NAMESPACE__.'\\'.$scheme.'\\'.ucfirst(strtolower($method));
72 12
        if (!class_exists($name)) {
73 1
            throw new InvalidArgumentException('Method given is not a valid request: '.$method);
74
        }
75
76 11
        $configurationClass = '\Bee4\Transport\Configuration\\'.$scheme.'Configuration';
77 11
        $request = new $name(
78
            $url,
79
            $headers,
80 11
            new $configurationClass(
81 11
                $this->configuration!==null?$this->configuration->toArray():[]
82
            )
83
        );
84
85 11
        return $request;
86
    }
87
88
    /**
89
     * Validate scheme by checking if its an allowed one
90
     * @param string $scheme
91
     * @return boolean|string If invalid, false, else return the request known one
92
     */
93 13
    private static function isAllowedScheme($scheme)
94
    {
95 13
        if (preg_match(AbstractRequest::HTTP, $scheme) === 1) {
96 11
            return 'http';
97 2
        } elseif (preg_match(AbstractRequest::FTP, $scheme) === 1) {
98 1
            return 'ftp';
99 1
        } elseif (preg_match(AbstractRequest::SSH, $scheme) === 1) {
100
            return 'ssh';
101
        } else {
102 1
            return false;
103
        }
104
    }
105
}
106