Completed
Push — feature/configuration ( 48ff2d...765854 )
by Stéphane
53:12 queued 25:50
created

ResponseFactory   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 88.24%

Importance

Changes 6
Bugs 2 Features 0
Metric Value
wmc 10
c 6
b 2
f 0
lcom 0
cbo 3
dl 0
loc 76
ccs 30
cts 34
cp 0.8824
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
B parseHeaders() 0 21 7
A nibbleLine() 0 6 1
B build() 0 25 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\Message
10
 */
11
12
namespace Bee4\Transport\Message;
13
14
use Bee4\Transport\Handle\ExecutionInfos;
15
16
/**
17
 * Build a response from an Handle response
18
 * @package Bee4\Transport\Message
19
 */
20
class ResponseFactory
21
{
22
    /**
23
     * Build a new reponse object from handle execution result
24
     * @param string $content Response content
25
     * @param Handle $handle Handle used to perform request which generate response
0 ignored issues
show
Bug introduced by
There is no parameter named $handle. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
26
     * @param Request\AbstractRequest $request
27
     * @return Response
28
     */
29 10
    public static function build(
30
        $content,
31
        ExecutionInfos $infos,
32
        Request\AbstractRequest $request
33
    ) {
34 10
        $response = new Response($request);
35 10
        $response->setStatus($infos->status);
0 ignored issues
show
Documentation introduced by
The property $status is declared private in Bee4\Transport\Handle\ExecutionInfos. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write 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.");
        }
    }

}

Since the property has write access only, you can use the @property-write 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...
36 10
        $response->setTransactionTime($infos->transactionTime);
0 ignored issues
show
Documentation introduced by
The property $transactionTime is declared private in Bee4\Transport\Handle\ExecutionInfos. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write 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.");
        }
    }

}

Since the property has write access only, you can use the @property-write 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...
37 10
        $response->setExecutionInfos($infos);
38
39
        //Populate request headers with all really sent headers
40 10
        if (!empty($infos->headers)) {
0 ignored issues
show
Documentation introduced by
The property $headers is declared private in Bee4\Transport\Handle\ExecutionInfos. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write 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.");
        }
    }

}

Since the property has write access only, you can use the @property-write 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...
41
            self::parseHeaders(
42
                $infos->headers,
0 ignored issues
show
Documentation introduced by
The property $headers is declared private in Bee4\Transport\Handle\ExecutionInfos. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write 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.");
        }
    }

}

Since the property has write access only, you can use the @property-write 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...
43
                $request
44
            );
45
        }
46
47
        //Headers are returned with content, so we extract it
48 10
        $content = self::parseHeaders($content, $response);
49
50 10
        $response->setBody($content);
51
52 10
        return $response;
53
    }
54
55
    /**
56
     * Parse headers from content and populate response with it
57
     * @param string $content
58
     * @param \Bee4\Transport\Message\AbstractMessage $message
59
     * @return string
60
     */
61 13
    public static function parseHeaders($content, AbstractMessage $message)
62
    {
63 13
        $namespace = explode('\\', get_class($message));
64 13
        $name = strtoupper(array_pop($namespace));
65
66 13
        if (strpos($content, 'HTTP/') === 0 || strpos($content, $name) === 0) {
67 12
            $line = self::nibbleLine($content);
68 12
            if (preg_match('/^HTTP\/[0-9]{1}\.[0-9]{1} (?P<code>[0-9]+) (?P<message>.*)$/', $line, $matches) &&
69 12
                $matches["code"] == "100"
70 12
            ) {
71 1
                self::nibbleLine($content);
72 1
            }
73 12
            while (($line = self::nibbleLine($content)) != "") {
74 12
                if (preg_match('/^([A-Za-z\-]+): (.*)/', $line, $matches)) {
75 12
                    $message->addHeader($matches[1], $matches[2]);
76 12
                }
77 12
            }
78 12
        }
79
80 13
        return $content;
81
    }
82
83
    /**
84
     * Read a line inside a string, remove the read line from the string and return the line
85
     * @param string $content
86
     * @param string $eol
87
     * @return string
88
     */
89 12
    private static function nibbleLine(&$content, $eol = "\r\n")
90
    {
91 12
        $line = substr($content, 0, strpos($content, $eol));
92 12
        $content = substr($content, strlen($line)+2);
93 12
        return $line;
94
    }
95
}
96