RequestFactory::isAllowedScheme()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4.0313

Importance

Changes 3
Bugs 1 Features 1
Metric Value
c 3
b 1
f 1
dl 0
loc 12
ccs 7
cts 8
cp 0.875
rs 9.2
cc 4
eloc 9
nc 4
nop 1
crap 4.0313
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\Exception\UnknownProtocolException;
15
use Bee4\Transport\Url;
16
17
/**
18
 * Static object in charge to build and initialize Request instance
19
 * @package Bee4\Transport\Message\Request
20
 */
21
class RequestFactory
22
{
23
    /**
24
     * Build a new request from parameters
25
     * @param string $method
26
     * @param Url $url
27
     * @param array $headers
28
     * @return AbstractRequest
29
     * @throws UnknownProtocolException
30
     * @throws InvalidArgumentException
31
     */
32 13
    public function build($method, Url $url, array $headers)
33
    {
34 13
        if (($scheme = self::isAllowedScheme($url->scheme())) === false) {
0 ignored issues
show
Bug introduced by
It seems like $url->scheme() targeting Bee4\Transport\Url::scheme() can also be of type object<Bee4\Transport\Url>; however, Bee4\Transport\Message\R...tory::isAllowedScheme() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
35 1
            throw new UnknownProtocolException(sprintf(
36 1
                "You can't request a transfer on unsupported protocol '%s'!",
37 1
                $url->scheme()
38 1
            ));
39
        }
40
41 12
        $name = __NAMESPACE__.'\\'.ucfirst($scheme).'\\'.ucfirst(strtolower($method));
42 12
        if (!class_exists($name)) {
43 1
            throw new \InvalidArgumentException('Method given is not a valid request: '.$method);
44
        }
45
46 11
        $request = new $name($url, $headers);
47
48 11
        return $request;
49
    }
50
51
    /**
52
     * Validate scheme by checking if its an allowed one
53
     * @param string $scheme
54
     * @return boolean|string If invalid, false, else return the request known one
55
     */
56 13
    private static function isAllowedScheme($scheme)
57
    {
58 13
        if (preg_match(AbstractRequest::HTTP, $scheme) === 1) {
59 11
            return 'http';
60 2
        } elseif (preg_match(AbstractRequest::FTP, $scheme) === 1) {
61 1
            return 'ftp';
62 1
        } elseif (preg_match(AbstractRequest::SSH, $scheme) === 1) {
63
            return 'ssh';
64
        } else {
65 1
            return false;
66
        }
67
    }
68
}
69