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

Request   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 92.31%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 1
dl 0
loc 43
ccs 12
cts 13
cp 0.9231
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A createRequest() 0 8 2
A detectClass() 0 10 4
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