Completed
Push — 1.x ( 084efb...fac983 )
by Joel
02:47
created

ImageManager::build()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 24
Code Lines 13

Duplication

Lines 24
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 24
loc 24
rs 8.6846
cc 4
eloc 13
nc 4
nop 3
1
<?php
2
3
namespace Docker\Manager;
4
5
use Docker\API\Model\AuthConfig;
6
use Docker\API\Model\BuildInfo;
7
use Docker\API\Model\CreateImageInfo;
8
use Docker\API\Model\PushImageInfo;
9
use Docker\API\Resource\ImageResource;
10
use Docker\Stream\BuildStream;
11
use Docker\Stream\CreateImageStream;
12
use Docker\Stream\PushStream;
13
use Joli\Jane\Swagger\Client\QueryParam;
14
15
class ImageManager extends ImageResource
16
{
17
    const FETCH_STREAM = 'stream';
18
19
    /**
20
     * {@inheritdoc}
21
     *
22
     * @return \Psr\Http\Message\ResponseInterface|BuildInfo[]|BuildStream
23
     */
24 View Code Duplication
    public function build($inputStream, $parameters = [], $fetch = self::FETCH_OBJECT)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
25
    {
26
        $response = parent::build($inputStream, $parameters, self::FETCH_RESPONSE);
27
28
        if (200 === $response->getStatusCode()) {
29
            if (self::FETCH_STREAM === $fetch) {
30
                return new BuildStream($response->getBody(), $this->serializer);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \Docker\Strea...(), $this->serializer); (Docker\Stream\BuildStream) is incompatible with the return type of the parent method Docker\API\Resource\ImageResource::build of type Psr\Http\Message\ResponseInterface.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
31
            }
32
33
            if (self::FETCH_OBJECT === $fetch) {
34
                $buildInfoList = [];
35
36
                $stream = new BuildStream($response->getBody(), $this->serializer);
37
                $stream->onFrame(function (BuildInfo $buildInfo) use (&$buildInfoList) {
38
                    $buildInfoList[] = $buildInfo;
39
                });
40
                $stream->wait();
41
42
                return $buildInfoList;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $buildInfoList; (array) is incompatible with the return type of the parent method Docker\API\Resource\ImageResource::build of type Psr\Http\Message\ResponseInterface.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
43
            }
44
        }
45
46
        return $response;
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     *
52
     * @return \Psr\Http\Message\ResponseInterface|CreateImageInfo[]|CreateImageStream
53
     */
54 View Code Duplication
    public function create($parameters = [], $fetch = self::FETCH_OBJECT)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
55
    {
56
        $response = parent::create($parameters, self::FETCH_RESPONSE);
57
58
        if (200 === $response->getStatusCode()) {
59
            if (self::FETCH_STREAM === $fetch) {
60
                return new CreateImageStream($response->getBody(), $this->serializer);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \Docker\Strea...(), $this->serializer); (Docker\Stream\CreateImageStream) is incompatible with the return type of the parent method Docker\API\Resource\ImageResource::create of type Psr\Http\Message\ResponseInterface.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
61
            }
62
63
            if (self::FETCH_OBJECT === $fetch) {
64
                $createImageInfoList = [];
65
66
                $stream = new CreateImageStream($response->getBody(), $this->serializer);
67
                $stream->onFrame(function (CreateImageInfo $createImageInfo) use (&$createImageInfoList) {
68
                    $createImageInfoList[] = $createImageInfo;
69
                });
70
                $stream->wait();
71
72
                return $createImageInfoList;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $createImageInfoList; (array) is incompatible with the return type of the parent method Docker\API\Resource\ImageResource::create of type Psr\Http\Message\ResponseInterface.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
73
            }
74
        }
75
76
        return $response;
77
    }
78
79
    /**
80
     * {@inheritdoc}
81
     *
82
     * @return \Psr\Http\Message\ResponseInterface|PushImageInfo[]|CreateImageStream
83
     */
84
    public function push($name, $parameters = [], $fetch = self::FETCH_OBJECT)
85
    {
86
        if (isset($parameters['X-Registry-Auth']) && $parameters['X-Registry-Auth'] instanceof AuthConfig) {
87
            $parameters['X-Registry-Auth'] = base64_encode($this->serializer->serialize($parameters['X-Registry-Auth'], 'json'));
88
        }
89
90
        $queryParam = new QueryParam();
91
        $queryParam->setDefault('tag', null);
92
        $queryParam->setDefault('X-Registry-Auth', null);
93
        $queryParam->setHeaderParameters(['X-Registry-Auth']);
94
95
        $url      = 'http://localhost/images/{name}/push';
96
        $url      = str_replace('{name}', $name, $url);
97
        $url      = $url . ('?' . $queryParam->buildQueryString($parameters));
98
99
        $headers  = array_merge(['Host' => 'localhost'], $queryParam->buildHeaders($parameters));
100
101
        $body     = $queryParam->buildFormDataString($parameters);
102
103
        $request  = $this->messageFactory->createRequest('POST', $url, $headers, $body);
104
        $response = $this->httpClient->sendRequest($request);
105
106
        if (200 === $response->getStatusCode()) {
107
            if (self::FETCH_STREAM === $fetch) {
108
                return new PushStream($response->getBody(), $this->serializer);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \Docker\Strea...(), $this->serializer); (Docker\Stream\PushStream) is incompatible with the return type of the parent method Docker\API\Resource\ImageResource::push of type Psr\Http\Message\ResponseInterface.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
109
            }
110
111
            if (self::FETCH_OBJECT === $fetch) {
112
                $pushImageInfoList = [];
113
114
                $stream = new PushStream($response->getBody(), $this->serializer);
115
                $stream->onFrame(function (PushImageInfo $pushImageInfo) use (&$pushImageInfoList) {
116
                    $pushImageInfoList[] = $pushImageInfo;
117
                });
118
                $stream->wait();
119
120
                return $pushImageInfoList;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $pushImageInfoList; (array) is incompatible with the return type of the parent method Docker\API\Resource\ImageResource::push of type Psr\Http\Message\ResponseInterface.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
121
            }
122
        }
123
124
        return $response;
125
    }
126
}
127