Completed
Pull Request — develop (#22)
by Michael
03:04 queued 01:11
created

AbstractHttpProvider::__construct()   B

Complexity

Conditions 7
Paths 7

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 7

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 16
ccs 5
cts 5
cp 1
rs 8.2222
cc 7
eloc 8
nc 7
nop 2
crap 7
1
<?php
2
3
/**
4
 * This file is part of the Teazee package.
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 *
8
 * @license    MIT License
9
 */
10
namespace Teazee\Provider;
11
12
use Http\Client\HttpClient;
13
use Http\Message\MessageFactory;
14
use Psr\Http\Message\ResponseInterface;
15
use Psr\Http\Message\UriInterface;
16
use Teazee\Exception\ServiceMissingException;
17
18
/**
19
 * @author Michael Crumm <[email protected]>
20
 */
21
abstract class AbstractHttpProvider extends AbstractProvider
22
{
23
    /**
24
     * @var HttpClient
25
     */
26
    private $client;
27
28
    /**
29
     * @var MessageFactory
30
     */
31
    private $messageFactory;
32
33
    /**
34
     * AbstractHttpProvider Constructor.
35
     *
36
     * @param HttpClient     $client         HttpClient makes HTTP requests.
37
     * @param MessageFactory $messageFactory MessageFactory creates Request objects.
38
     */
39
    public function __construct(HttpClient $client = null, MessageFactory $messageFactory = null)
40
    {
41 10
        parent::__construct();
42
43
        if (null === $client and !class_exists('Http\Discovery\HttpClientDiscovery')) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
44 1
            throw ServiceMissingException::noHttpClient();
45
        }
46
47 10
        $this->client = $client ?: \Http\Discovery\HttpClientDiscovery::find();
48
49
        if (null === $messageFactory and !class_exists('Http\Discovery\MessageFactoryDiscovery')) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
50 1
            throw ServiceMissingException::noMessageFactory();
51
        }
52
53 10
        $this->messageFactory = $messageFactory ?: \Http\Discovery\MessageFactoryDiscovery::find();
54
    }
55
56
    /**
57
     * Returns the HttpClient instance for this Provider.
58
     *
59
     * @return HttpClient
60
     */
61
    public function getClient()
62
    {
63 1
        return $this->client;
64
    }
65
66
    /**
67
     * Returns a ResponseInterface for the given URI/method.
68
     *
69
     * @param string|UriInterface $uri    Request URI.
70
     * @param string              $method HTTP method (Defaults to 'GET').
71
     *
72
     * @return ResponseInterface
73
     */
74
    protected function getResponse($uri, $method = 'GET')
75
    {
76 6
        $request = $this->messageFactory->createRequest($method, $uri);
77
78 6
        return $this->client->sendRequest($request);
79
    }
80
}
81