Completed
Pull Request — master (#364)
by Alessandro
04:31
created

InvalidUrlException::invalidUrlParts()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 8
ccs 0
cts 8
cp 0
rs 9.4285
cc 1
eloc 5
nc 1
nop 2
crap 2
1
<?php
2
3
/*
4
 * This file is part of the FOSHttpCache package.
5
 *
6
 * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace FOS\HttpCache\Exception;
13
14
/**
15
 * Thrown during setup if the configuration for a proxy client is invalid.
16
 */
17
class InvalidUrlException extends InvalidArgumentException implements HttpCacheException
18
{
19
    /**
20
     * @param string $url    The invalid URL
21
     * @param string $reason Further explanation why the URL was invalid (optional)
22
     *
23
     * @return self
24
     */
25
    public static function invalidUrl($url, $reason = null)
26
    {
27
        $msg = sprintf('URL "%s" is invalid.', $url);
28
        if ($reason) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $reason of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
29
            $msg .= sprintf(' Reason: %s', $reason);
30
        }
31
32
        return new self($msg);
33
    }
34
35
    /**
36
     * @param string $server  Invalid server
37
     * @param array  $allowed Allowed URL parts
38
     *
39
     * @return self
40
     */
41
    public static function invalidUrlParts($server, array $allowed)
42
    {
43
        return new self(sprintf(
44
            'Server "%s" is invalid. Only %s URL parts are allowed.',
45
            $server,
46
            implode(', ', $allowed)
47
        ));
48
    }
49
}
50