Completed
Pull Request — master (#5)
by Jean-Baptiste
04:36
created

GuzzleFactory::createMockedClient()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 15
Code Lines 9

Duplication

Lines 15
Ratio 100 %

Importance

Changes 0
Metric Value
dl 15
loc 15
rs 9.2
c 0
b 0
f 0
cc 4
eloc 9
nc 4
nop 1
1
<?php
2
3
namespace CanalTP\AbstractGuzzle;
4
5
use CanalTP\AbstractGuzzle\Exception\UnsupportedException;
6
use CanalTP\AbstractGuzzle\Tests\Mock\Guzzle5Mock;
7
8
class GuzzleFactory
9
{
10
    /**
11
     * @param string $baseUri
12
     *
13
     * @return Guzzle
14
     *
15
     * @throws NotSupportedException when Guzzle vendor version is not supported.
16
     */
17 View Code Duplication
    public static function createGuzzle($baseUri, $options)
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...
18
    {
19
        $guzzleVersion = self::detectGuzzleVersion();
20
21
        switch ($guzzleVersion) {
22
            case 6:
23
                return new Version\Guzzle6($baseUri, $options);
24
25
            case 5:
26
                return new Version\Guzzle5($baseUri);
27
28
            case 3:
29
                return new Version\Guzzle3($baseUri);
30
        }
31
    }
32
33
    /**
34
     * @return int current Guzzle vendor version.
35
     *
36
     * @throws NotSupportedException when Guzzle vendor version is not supported.
37
     */
38
    public static function detectGuzzleVersion()
39
    {
40
        if (self::supportsGuzzle6()) {
41
            return 6;
42
        }
43
44
        if (self::supportsGuzzle5()) {
45
            return 5;
46
        }
47
48
        if (self::supportsGuzzle3()) {
49
            return 3;
50
        }
51
52
        throw new UnsupportedException();
53
    }
54
55
    /**
56
     * @return bool
57
     */
58
    private static function supportsGuzzle6()
59
    {
60
        return class_exists('GuzzleHttp\\Client') && !trait_exists('GuzzleHttp\\HasDataTrait');
61
    }
62
63
    /**
64
     * @return bool
65
     */
66
    private static function supportsGuzzle5()
67
    {
68
        return class_exists('GuzzleHttp\\Client') && trait_exists('GuzzleHttp\\HasDataTrait');
69
    }
70
71
    /**
72
     * @return bool
73
     */
74
    private static function supportsGuzzle3()
75
    {
76
        return class_exists('Guzzle\\Service\\Client');
77
    }
78
79 View Code Duplication
    public static function createMockedClient(array $mockedResponseCollection)
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...
80
    {
81
        $guzzleVersion = self::detectGuzzleVersion();
82
83
        switch ($guzzleVersion) {
84
            case 6:
85
                return new Version\Guzzle6($baseUri);
0 ignored issues
show
Bug introduced by
The variable $baseUri does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The call to Guzzle6::__construct() misses a required argument $options.

This check looks for function calls that miss required arguments.

Loading history...
86
87
            case 5:
88
                return new Guzzle5Mock($mockedResponseCollection);
89
90
            case 3:
91
                return new Version\Guzzle3($baseUri);
92
        }
93
    }
94
}
95