Context   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 127
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 82.86%

Importance

Changes 3
Bugs 1 Features 1
Metric Value
wmc 14
c 3
b 1
f 1
lcom 1
cbo 2
dl 0
loc 127
ccs 29
cts 35
cp 0.8286
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A getDirectory() 0 4 1
A setDirectory() 0 4 1
A getDockerfileContent() 0 4 1
A isStreamed() 0 4 1
A read() 0 4 2
A __construct() 0 5 1
A toTar() 0 11 2
A toStream() 0 9 2
A __destruct() 0 10 3
1
<?php
2
3
namespace Docker\Context;
4
5
use Symfony\Component\Process\Process;
6
use Symfony\Component\Process\Exception\ProcessFailedException;
7
8
/**
9
 * Docker\Context\Context
10
 */
11
class Context implements ContextInterface
12
{
13
    const FORMAT_STREAM = 'stream';
14
15
    const FORMAT_TAR = 'tar';
16
17
    /**
18
     * @var string
19
     */
20
    private $directory;
21
22
    /**
23
     * @var process Tar process
24
     */
25
    private $process;
26
27
    /**
28
     * @var resource Tar stream
29
     */
30
    private $stream;
31
32
    /**
33
     * @var string Format of the context (stream or tar)
34
     */
35
    private $format = self::FORMAT_STREAM;
36
37
    /**
38
     * @param string     $directory Directory of context
39
     * @param string     $format    Format to use when sending the call (stream or tar: string)
40
     */
41 14
    public function __construct($directory, $format = self::FORMAT_STREAM)
42
    {
43 14
        $this->directory = $directory;
44 14
        $this->format = $format;
45 14
    }
46
47
    /**
48
     * Get directory of Context
49
     *
50
     * @return string
51
     */
52 7
    public function getDirectory()
53
    {
54 7
        return $this->directory;
55
    }
56
57
    /**
58
     * Set directory of Context
59
     *
60
     * @param string $directory Targeted directory
61
     */
62
    public function setDirectory($directory)
63
    {
64
        $this->directory = $directory;
65
    }
66
67
    /**
68
     * Return content of Dockerfile of this context
69
     *
70
     * @return string Content of dockerfile
71
     */
72 2
    public function getDockerfileContent()
73
    {
74 2
        return file_get_contents($this->directory.DIRECTORY_SEPARATOR.'Dockerfile');
75
    }
76
77
    /**
78
     * @return boolean
79
     */
80 4
    public function isStreamed()
81
    {
82 4
        return $this->format === self::FORMAT_STREAM;
83
    }
84
85
    /**
86
     * @return resource|string
87
     */
88 4
    public function read()
89
    {
90 4
        return $this->isStreamed() ? $this->toStream() : $this->toTar();
91
    }
92
93
    /**
94
     * Return the context as a tar archive
95
     *
96
     * @throws \Symfony\Component\Process\Exception\ProcessFailedException
97
     *
98
     * @return string Tar content
99
     */
100 1
    public function toTar()
101
    {
102 1
        $process = new Process('/usr/bin/env tar c .', $this->directory);
103 1
        $process->run();
104
105 1
        if (!$process->isSuccessful()) {
106
            throw new ProcessFailedException($process);
107
        }
108
109 1
        return $process->getOutput();
110
    }
111
112
    /**
113
     * Return a stream for this context
114
     *
115
     * @return resource Stream resource in memory
116
     */
117 5
    public function toStream()
118
    {
119 5
        if (!is_resource($this->process)) {
120 5
            $this->process = proc_open("/usr/bin/env tar c .", [["pipe", "r"], ["pipe", "w"], ["pipe", "w"]], $pipes, $this->directory);
0 ignored issues
show
Documentation Bug introduced by
It seems like proc_open('/usr/bin/env ...ipes, $this->directory) of type resource is incompatible with the declared type object<Docker\Context\process> of property $process.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
121 5
            $this->stream  = $pipes[1];
122 5
        }
123
124 5
        return $this->stream;
125
    }
126
127 14
    public function __destruct()
128
    {
129 14
        if (is_resource($this->process)) {
130 5
            proc_close($this->process);
131 5
        }
132
133 14
        if (is_resource($this->stream)) {
134
            fclose($this->stream);
135
        }
136 14
    }
137
}
138