Completed
Push — master ( 4537c6...c97103 )
by Evgenij
03:39
created

StreamContext::createFromResource()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 * Async sockets
4
 *
5
 * @copyright Copyright (c) 2015-2017, Efimov Evgenij <[email protected]>
6
 *
7
 * This source file is subject to the MIT license that is bundled
8
 * with this source code in the file LICENSE.
9
 */
10
11
namespace AsyncSockets\Configuration;
12
13
/**
14
 * Class StreamContext
15
 */
16
class StreamContext
17
{
18
    /**
19
     * Stream context resource
20
     *
21
     * @var resource
22
     */
23
    private $resource;
24
25
    /**
26
     * StreamContext constructor.
27
     *
28
     * @param array|resource|\Traversable|null $settings Context options
29
     */
30 188
    public function __construct($settings)
31
    {
32 188
        $this->resource = $this->createResource($settings);
33 187
    }
34
35
    /**
36
     * Return stream context resource
37
     *
38
     * @return resource
39
     */
40 168
    public function getResource()
41
    {
42 168
        return $this->resource;
43
    }
44
45
    /**
46
     * Creates resource from given settings
47
     *
48
     * @param array|resource|\Traversable|null $settings Context settings
49
     *
50
     * @return resource
51
     */
52 188
    protected function createResource($settings)
53
    {
54 188
        if ($settings instanceof \Traversable) {
55 1
            $settings = iterator_to_array($settings);
56 1
        }
57
58
        $map = [
59 188
            'null'     => [ $this, 'createFromNull' ],
60 188
            'resource' => [ $this, 'createFromResource' ],
61 188
            'array'    => [ $this, 'createFromArray' ],
62 188
        ];
63
64 188
        $type = strtolower(gettype($settings));
65 188
        if (!isset($map[$type])) {
66 1
            throw new \InvalidArgumentException(
67 1
                sprintf(
68 1
                    'Can not create stream context for variable type %s',
69 1
                    is_object($settings) ? get_class($settings) : $type
70 1
                )
71 1
            );
72
        }
73
74
75 187
        return $map[$type]($settings);
76
    }
77
78
    /**
79
     * Create context from resource
80
     *
81
     * @param resource $resource Context resource
82
     *
83
     * @return resource
84
     */
85 156
    private function createFromResource($resource)
86
    {
87 156
        return $resource;
88
    }
89
90
    /**
91
     * Create context from resource
92
     *
93
     * @return resource
94
     */
95 182
    private function createFromNull()
96
    {
97 182
        return stream_context_get_default();
98
    }
99
100
    /**
101
     * Create context from array
102
     *
103
     * @param array $settings Context settings
104
     *
105
     * @return resource
106
     */
107 10
    private function createFromArray(array $settings)
108
    {
109 10
        return stream_context_create(
110 10
            isset($settings[ 'options' ]) ? $settings[ 'options' ] : [],
111 10
            isset($settings[ 'params' ]) ? $settings[ 'params' ] : []
112 10
        );
113
    }
114
}
115