Completed
Push — master ( c30967...3bd52a )
by Andrii
03:06
created

Request::createRequest()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 0
crap 2
1
<?php
2
/**
3
 * ActiveRecord for API
4
 *
5
 * @link      https://github.com/hiqdev/yii2-hiart
6
 * @package   yii2-hiart
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2015-2017, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hiqdev\hiart\auto;
12
13
use hiqdev\hiart\Query;
14
use hiqdev\hiart\QueryBuilderInterface;
15
use hiqdev\hiart\RequestCreatorInterface;
16
use hiqdev\hiart\RequestErrorException;
17
18
/**
19
 * Auto Request.
20
 * Detects best available transport in the system.
21
 */
22
class Request implements RequestCreatorInterface
23
{
24
    /**
25
     * {@inheritdoc}
26
     */
27 2
    public function __construct(QueryBuilderInterface $builder, Query $query)
28
    {
29 2
        $this->builder = $builder;
0 ignored issues
show
Bug introduced by
The property builder does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
30 2
        $this->query = $query;
0 ignored issues
show
Bug introduced by
The property query does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
31 2
    }
32
33
    protected $detectedClass;
34
35
    /**
36
     * {@inheritdoc}
37
     */
38 2
    public function createRequest()
39
    {
40 2
        if ($this->detectedClass === null) {
41 2
            $this->detectedClass = $this->detectClass();
42
        }
43
44 2
        return new $this->detectedClass($this->builder, $this->query);
45
    }
46
47
    public $tryClasses = [
48
        \hiqdev\hiart\guzzle\Request::class,
49
        \hiqdev\hiart\httpclient\Request::class,
50
        \hiqdev\hiart\curl\Request::class,
51
        \hiqdev\hiart\stream\Request::class,
52
    ];
53
54 2
    public function detectClass()
55
    {
56 2
        foreach ($this->tryClasses as $class) {
57 2
            if (class_exists($class) && $class::isSupported()) {
58 2
                return $class;
59
            }
60
        }
61
62
        throw new RequestErrorException('could not auto detect Request class');
0 ignored issues
show
Bug introduced by
The call to RequestErrorException::__construct() misses a required argument $request.

This check looks for function calls that miss required arguments.

Loading history...
63
    }
64
}
65