Completed
Push — master ( 6a9792...3cf5e2 )
by Haridarshan
02:58
created

HelperFactory::__clone()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 3
rs 10
cc 1
eloc 1
nc 1
nop 0
1
<?php
2
namespace Haridarshan\Instagram;
3
4
use GuzzleHttp\Client;
5
use GuzzleHttp\Exception\ClientException;
6
use GuzzleHttp\Psr7\Response;
7
use GuzzleHttp\Psr7\Stream;
8
use Haridarshan\Instagram\Exceptions\InstagramException;
9
use Haridarshan\Instagram\Exceptions\InstagramOAuthException;
10
use Haridarshan\Instagram\Exceptions\InstagramServerException;
11
12
class HelperFactory
13
{
14
	/** @var Singleton The reference to *Singleton* instance of this class */
15
	private static $instance;
16
	
17
    /** @var Response $response */
18
    protected static $response;
19
    
20
    /** @var Stream $stream */
21
    protected static $stream;
22
    
23
    /** @var object $content */
24
    protected static $content;
25
    
26
	/*
27
     * Returns the *Singleton* instance of this class.
28
     *
29
     * @return Singleton The *Singleton* instance.
30
     */
31
    public static function getInstance()
32
    {
33
        if (null === static::$instance) {
0 ignored issues
show
Bug introduced by
Since $instance is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $instance to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
34
            static::$instance = new static();
0 ignored issues
show
Bug introduced by
Since $instance is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $instance to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
Documentation Bug introduced by
It seems like new static() of type this<Haridarshan\Instagram\HelperFactory> is incompatible with the declared type object<Haridarshan\Instagram\Singleton> of property $instance.

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...
35
        }
36
        
37
        return static::$instance;
0 ignored issues
show
Bug introduced by
Since $instance is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $instance to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
Bug Compatibility introduced by
The expression static::$instance; of type Haridarshan\Instagram\He...han\Instagram\Singleton adds the type Haridarshan\Instagram\HelperFactory to the return on line 37 which is incompatible with the return type documented by Haridarshan\Instagram\HelperFactory::getInstance of type Haridarshan\Instagram\Singleton.
Loading history...
38
    }
39
	
40
	/*
41
     * Protected constructor to prevent creating a new instance of the
42
     * *Singleton* via the `new` operator from outside of this class.
43
     */
44
    protected function __construct()
45
    {
46
        // a factory constructor should never be invoked
47
    }
48
    
49
    /*
50
     * Factory Client method to create \GuzzleHttp\Client object
51
     * @param string $uri
52
     * @return Client
53
     */
54
    public function client($uri)
55
    {
56
        return new Client([
57
            'base_uri' => $uri
58
        ]);
59
    }
60
61
    /*	
62
     * @param Client $client
63
     * @param string $endpoint
64
     * @param array|string $options
65
     * @param string $method
66
     * @return Response
67
     * @throws InstagramOAuthException
68
     * @throws InstagramException
69
     */
70
    public function request(Client $client, $endpoint, $options, $method = 'GET')
71
    {
72
        try {
73
            return $client->request($method, $endpoint, [
74
                'headers' => ['Accept' => 'application/json'],
75
                'body' => static::createBody($options, $method)
76
            ]);
77
        } catch (ClientException $exception) {
78
            static::throwException(static::extractOriginalExceptionMessage($exception), $exception);
79
        }
80
    }
81
    
82
    /*
83
     * Create body for Guzzle client request
84
     * @param array|null|string $options
85
     * @param string $method GET|POST
86
     * @return string|mixed
87
     */
88
    protected static function createBody($options, $method)
89
    {
90
        return ('GET' !== $method) ? is_array($options) ? http_build_query($options) : ltrim($options, '&') : null;
91
    }
92
    
93
    /*
94
     * Method to extract all exceptions for Guzzle ClientException
95
     * @param ClientException $exception
96
     * @return
97
     */
98
    protected static function extractOriginalExceptionMessage(ClientException $exception)
99
    {
100
        self::$response = $exception->getResponse();
101
        self::$stream = self::$response->getBody();
102
        self::$content = self::$stream->getContents();
103
        if (empty(self::$content)) {
104
            throw new InstagramServerException(
105
                $exception->getMessage(),
106
                $exception->getCode(),
107
                $exception
108
            );
109
        }
110
		return json_decode(self::$content);
111
    }
112
    
113
    /*
114
     * @param \stdClass $object
115
	 * @param ClientException $exception
0 ignored issues
show
Bug introduced by
There is no parameter named $exception. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
116
     * @return void
117
     */
118
    protected static function throwException(\stdClass $object, ClientException $exMessage)
119
    {
120
        $exception = static::getExceptionMessage($object);
121
        if (stripos($exception['error_type'], "oauth") !== false) {
122
            throw new InstagramOAuthException(
123
                json_encode(array("Type" => $exception['error_type'], "Message" => $exception['error_message'])),
124
                $exception['error_code'],
125
                $exMessage
126
            );
127
        }
128
        throw new InstagramException(
129
            json_encode(array("Type" => $exception['error_type'], "Message" => $exception['error_message'])),
130
            $exception['error_code'],
131
            $exMessage
132
        );
133
    }
134
	
135
    /*
136
	 * @param \stdClass $object
137
	 * @return array
138
	 */
139
    protected static function getExceptionMessage(\stdClass $object)
140
    {
141
        $message = array();		
142
		$message['error_type'] = isset($object->meta) ? $object->meta->error_type : $object->error_type;
143
		$message['error_message'] = isset($object->meta) ? $object->meta->error_message : $object->error_message;
144
		$message['error_code'] = isset($object->meta) ? $object->meta->code : $object->code;
145
        return $message;
146
    }
147
	
148
	/*
149
     * Private clone method to prevent cloning of the instance of the
150
     * *Singleton* instance.
151
     *
152
     * @return void
153
     */
154
    private function __clone()
155
    {
156
    }
157
}
158