Completed
Push — master ( f62226...8b6b73 )
by Stéphane
10s
created

ExecutionInfos::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 4
nc 1
nop 2
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\Handle
10
 */
11
12
namespace Bee4\Transport\Handle;
13
14
/**
15
 * Handle execution informations implementation
16
 * @package Bee4\Transport\Handle
17
 * @method ExecutionInfos status(integer $status)      Set the execution status
18
 * @method ExecutionInfos headers(array $headers)      Set headers
19
 * @method ExecutionInfos effectiveUrl(string $url)    Effective URL for the request
20
 * @method ExecutionInfos transactionTime(float $time) Total request process duration in second
21
 * @method ExecutionInfos contentType(string $type)    Content-Type header for the request
22
 */
23
class ExecutionInfos
24
{
25
    /**
26
     * Handle used to perform the execution
27
     */
28
    private $handle;
29
30
    /**
31
     * Execution status code
32
     * @var integer
33
     */
34
    private $status;
0 ignored issues
show
Unused Code introduced by
The property $status is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
35
36
    /**
37
     * Header collection
38
     * @var string
39
     */
40
    private $headers;
0 ignored issues
show
Unused Code introduced by
The property $headers is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
41
42
    /**
43
     * Effective URL
44
     * @var string
45
     */
46
    private $effectiveUrl;
0 ignored issues
show
Unused Code introduced by
The property $effectiveUrl is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
47
48
    /**
49
     * Transaction time
50
     * @var float
51
     */
52
    private $transactionTime;
0 ignored issues
show
Unused Code introduced by
The property $transactionTime is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
53
54
    /**
55
     * Content type for the request
56
     * @var string
57
     */
58
    private $contentType;
0 ignored issues
show
Unused Code introduced by
The property $contentType is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
59
60
    /**
61
     * Build an ExecutioInfos instance
62
     * @param HandleInterface $handle
63
     */
64
    public function __construct(
65
        HandleInterface $handle,
66
        callable $resolver = null
0 ignored issues
show
Unused Code introduced by
The parameter $resolver is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
67
    ) {
68
        $this->handle = $handle;
69
    }
70
71
    /**
72
     * Retrieve current handle
73
     * @return HandleInterface
74
     */
75
    public function getHandle()
76
    {
77
        return $this->handle;
78
    }
79
80
    /**
81
     * Retrieve a property value
82
     * @param  string $name Property name
83
     * @return mixed
84
     */
85
    public function __get($name)
86
    {
87
        if (!property_exists($this, $name)) {
88
            throw new \BadMethodCallException('Invalid property name: '.$name);
89
        }
90
91
        if (null === $this->$name && isset($this->resolver)) {
92
            $this->$name = call_user_func($this->resolver, $name);
0 ignored issues
show
Documentation introduced by
The property resolver does not exist on object<Bee4\Transport\Handle\ExecutionInfos>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
93
        }
94
95
        return $this->$name;
96
    }
97
98
    /**
99
     * Set a property
100
     * @param  string $name
101
     * @param  array  $arguments
102
     * @return ExecutionInfos
103
     */
104
    public function __call($name, array $arguments)
105
    {
106
        //Define getter and setter for each valid property
107
        if (property_exists($this, $name) && count($arguments) == 1) {
108
            $this->$name = $arguments[0];
109
            return $this;
110
        } else {
111
            throw new \BadMethodCallException('Invalid method: '.$name);
112
        }
113
    }
114
}
115