Completed
Push — 1.x ( ee8545...084efb )
by Joel
02:48
created

BuilderHelper::build()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 14
rs 9.4286
cc 2
eloc 9
nc 2
nop 3
1
<?php
2
3
namespace Docker\Helper;
4
5
use Docker\Context\ContextInterface;
6
use Docker\Exception\DockerException;
7
use Docker\Manager\ImageManager;
8
use Docker\Stream\BuildStream;
9
use Http\Message\StreamFactory;
10
11
/**
12
 * Helper for building image with a context
13
 */
14
class BuilderHelper
15
{
16
    /**
17
     * @var ImageManager
18
     */
19
    protected $imageManager;
20
21
    /**
22
     * @var StreamFactory
23
     */
24
    protected $streamFactory;
25
26
    public function __construct(ImageManager $imageManager, StreamFactory $streamFactory)
27
    {
28
        $this->imageManager = $imageManager;
29
        $this->streamFactory = $streamFactory;
30
    }
31
32
    /**
33
     * Build an image
34
     *
35
     * @param ContextInterface $context
36
     * @param string           $name
37
     * @param array            $parameters
38
     *
39
     * @return BuildStream Return a stream which can be waited and listened to have build
40
     *
41
     * @throws DockerException
42
     */
43
    public function build(ContextInterface $context, $name, $parameters = [])
44
    {
45
        $stream = $this->streamFactory->createStream($context->read());
46
        $buildResponse = $this->imageManager->build($stream, array_merge($parameters, [
47
            't' => $name,
48
            'q' => 'false'
49
        ]), ImageManager::FETCH_RESPONSE);
50
51
        if (200 !== $buildResponse->getStatusCode()) {
52
            throw new DockerException(sprintf('Build failed with status code %s', $buildResponse->getStatusCode()));
53
        }
54
55
        return new BuildStream($buildResponse->getBody());
56
    }
57
}
58