Passed
Push — master ( 32692a...12a81e )
by Sebastian
02:54
created

RequestHelper_Boundaries   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 119
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 11
eloc 32
c 1
b 0
f 0
dl 0
loc 119
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A createBoundary() 0 3 1
A __construct() 0 3 1
A addVariable() 0 6 1
A addBoundary() 0 6 1
A render() 0 13 2
A getContentLength() 0 3 1
A addContent() 0 8 1
A addFile() 0 9 1
A getMimeBoundary() 0 3 1
A getEOL() 0 3 1
1
<?php
2
/**
3
 * File containing the {@link RequestHelper_Boundaries} class.
4
 * @package Application Utils
5
 * @subpackage RequestHelper
6
 * @see RequestHelper_Boundaries
7
 */
8
9
declare(strict_types=1);
10
11
namespace AppUtils;
12
13
/**
14
 * Container for the collection of boundaries that will be 
15
 * sent in the request.
16
 *
17
 * @package Application Utils
18
 * @subpackage RequestHelper
19
 * @author Sebastian Mordziol <[email protected]>
20
 */
21
class RequestHelper_Boundaries
22
{
23
   /**
24
    * @var RequestHelper
25
    */
26
    protected $helper;
27
28
   /**
29
    * @var RequestHelper_Boundaries_Boundary[]
30
    */
31
    protected $boundaries = array();
32
    
33
   /**
34
    * @var integer
35
    */
36
    protected $contentLength = 0;
37
    
38
    public function __construct(RequestHelper $helper)
39
    {
40
        $this->helper = $helper;
41
    }
42
    
43
    public function getMimeBoundary() : string
44
    {
45
        return $this->helper->getMimeBoundary();
46
    }
47
    
48
    public function getEOL() : string
49
    {
50
        return $this->helper->getEOL();
51
    }
52
    
53
   /**
54
    * Retrieves the total content length of all boundaries.
55
    * @return int
56
    */
57
    public function getContentLength() : int
58
    {
59
        return $this->contentLength;
60
    }
61
    
62
   /**
63
    * Adds a file to be sent with the request.
64
    *
65
    * @param string $varName The variable name to send the file in
66
    * @param string $fileName The name of the file as it should be received at the destination
67
    * @param string $content The raw content of the file
68
    * @param string $contentType The content type, use the constants to specify this
69
    * @param string $encoding The encoding of the file, use the constants to specify this
70
    */
71
    public function addFile(string $varName, string $fileName, string $content, string $contentType = RequestHelper::FILETYPE_TEXT, string $encoding = RequestHelper::ENCODING_UTF8) : RequestHelper_Boundaries
72
    {
73
        $boundary = $this->createBoundary(chunk_split(base64_encode($content)))
74
        ->setName($varName)
75
        ->setFileName(basename($fileName))
76
        ->setContentType($contentType)
77
        ->setContentEncding($encoding);
0 ignored issues
show
Bug introduced by
The method setContentEncding() does not exist on AppUtils\RequestHelper_Boundaries_Boundary. Did you maybe mean setContentEncoding()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

77
        ->/** @scrutinizer ignore-call */ setContentEncding($encoding);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
78
        
79
        $this->addBoundary($boundary);
0 ignored issues
show
Bug Best Practice introduced by
In this branch, the function will implicitly return null which is incompatible with the type-hinted return AppUtils\RequestHelper_Boundaries. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
80
    }
81
    
82
   /**
83
    * Adds arbitrary content.
84
    *
85
    * @param string $varName
86
    * @param string $content
87
    * @param string $contentType
88
    */
89
    public function addContent(string $varName, string $content, string $contentType) : RequestHelper_Boundaries
90
    {
91
        $boundary = $this->createBoundary($content)
92
        ->setName($varName)
93
        ->setContentType($contentType)
94
        ->setContentEncoding(RequestHelper::ENCODING_UTF8);
95
        
96
        return $this->addBoundary($boundary);
97
    }
98
    
99
   /**
100
    * Adds a variable to be sent with the request. If it
101
    * already exists, its value is overwritten.
102
    *
103
    * @param string $name
104
    * @param string $value
105
    */
106
    public function addVariable(string $name, string $value) : RequestHelper_Boundaries
107
    {
108
        $boundary = $this->createBoundary($value)
109
        ->setName($name);
110
        
111
        return $this->addBoundary($boundary);
112
    }
113
    
114
    protected function addBoundary(RequestHelper_Boundaries_Boundary $boundary) : RequestHelper_Boundaries
115
    {
116
        $this->boundaries[] = $boundary;
117
        $this->contentLength += $boundary->getContentLength();
118
        
119
        return $this;
120
    }
121
    
122
    public function render() : string
123
    {
124
        $result = '';
125
        
126
        foreach($this->boundaries as $boundary)
127
        {
128
            $result .= $boundary->render();
129
        }
130
        
131
        $result .= "--" . $this->getMimeBoundary() . "--" . 
132
        $this->getEOL() . $this->getEOL(); // always finish with two eol's!!
133
        
134
        return $result;
135
    }
136
    
137
    protected function createBoundary(string $content) : RequestHelper_Boundaries_Boundary
138
    {
139
        return new RequestHelper_Boundaries_Boundary($this, $content);
140
    }
141
}
142