Completed
Push — master ( 11aab9...ad1cee )
by Stéphane
03:43
created

RequestFactory::setConfiguration()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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