Completed
Push — feature/configuration ( f62226...16be63 )
by Stéphane
05:29
created

RequestFactory   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 94.74%

Importance

Changes 6
Bugs 2 Features 1
Metric Value
wmc 9
c 6
b 2
f 1
lcom 1
cbo 4
dl 0
loc 71
ccs 18
cts 19
cp 0.9474
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B build() 0 26 4
A isAllowedScheme() 0 12 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 13
     * @param Configuration|null $config
34
     */
35 13
    public function __construct(Configuration $config = null)
36 1
    {
37 1
        $this->configuration = $config;
38 1
    }
39 1
40
    /**
41
     * Build a new request from parameters
42 12
     * @param string $method
43 12
     * @param Url $url
44 1
     * @param array $headers
45
     * @return AbstractRequest
46
     * @throws UnknownProtocolException
47 11
     * @throws InvalidArgumentException
48
     */
49 11
    public function build($method, Url $url, array $headers)
50
    {
51
        if (($scheme = self::isAllowedScheme((string)$url->scheme())) === false) {
52
            throw new UnknownProtocolException(sprintf(
53
                "You can't request a transfer on unsupported protocol '%s'!",
54
                $url->scheme()
55
            ));
56
        }
57 13
58
        $schema = ucfirst($scheme);
0 ignored issues
show
Unused Code introduced by
$schema is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
59 13
        $name = __NAMESPACE__.'\\'.$scheme.'\\'.ucfirst(strtolower($method));
60 11
        if (!class_exists($name)) {
61 2
            throw new InvalidArgumentException('Method given is not a valid request: '.$method);
62 1
        }
63 1
64
        $configurationClass = '\Bee4\Transport\Configuration\\'.$scheme.'Configuration';
65
        $request = new $name(
66 1
            $url,
67
            $headers,
68
            new $configurationClass(
69
                $this->configuration!==null?$this->configuration->toArray():[]
70
            )
71
        );
72
73
        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
    private static function isAllowedScheme($scheme)
82
    {
83
        if (preg_match(AbstractRequest::HTTP, $scheme) === 1) {
84
            return 'http';
85
        } elseif (preg_match(AbstractRequest::FTP, $scheme) === 1) {
86
            return 'ftp';
87
        } elseif (preg_match(AbstractRequest::SSH, $scheme) === 1) {
88
            return 'ssh';
89
        } else {
90
            return false;
91
        }
92
    }
93
}
94