Connector::delete()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Axsor\PhpIPAM\Http\Requests;
4
5
use Axsor\PhpIPAM\Exceptions\BadCredentialsException;
6
use Axsor\PhpIPAM\Facades\PhpIPAM;
7
use Illuminate\Support\Facades\Cache;
8
9
class Connector
10
{
11
    const SUCCESS_CODE = 200;
12
    const UNAUTHORIZED_CODE = 401;
13
14
    private $config;
15
16
    private $headers;
17
18
    public function __construct()
19
    {
20
        $this->config = PhpIPAM::getConfig();
0 ignored issues
show
Bug introduced by
The method getConfig() does not exist on Axsor\PhpIPAM\Facades\PhpIPAM. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

20
        /** @scrutinizer ignore-call */ 
21
        $this->config = PhpIPAM::getConfig();
Loading history...
21
22
        $this->configHeaders();
23
    }
24
25
    protected function get($uri)
26
    {
27
        return $this->call('GET', $uri);
28
    }
29
30
    protected function post($uri, $payload)
31
    {
32
        return $this->call('POST', $uri, $payload);
33
    }
34
35
    protected function patch($uri, $payload)
36
    {
37
        return $this->call('PATCH', $uri, $payload);
38
    }
39
40
    protected function delete($uri)
41
    {
42
        return $this->call('DELETE', $uri);
43
    }
44
45
    private function call($method, $uri, $payload = [], $firstTry = false)
46
    {
47
        try {
48
            $response = PhpIPAM::getClient()->$method($this->config['url'].'/'.$this->config['app'].'/'.$uri, [
0 ignored issues
show
Bug introduced by
The method getClient() does not exist on Axsor\PhpIPAM\Facades\PhpIPAM. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

48
            $response = PhpIPAM::/** @scrutinizer ignore-call */ getClient()->$method($this->config['url'].'/'.$this->config['app'].'/'.$uri, [
Loading history...
49
                'headers' => $this->headers,
50
                'json' => $payload,
51
            ]);
52
53
            return json_decode($response->getBody()->getContents(), true);
54
        } catch (\GuzzleHttp\Exception\ClientException $e) {
55
            if (! $firstTry && $e->getCode() == self::UNAUTHORIZED_CODE) {
56
                Cache::forget(PhpIPAM::getCacheKey());
0 ignored issues
show
Bug introduced by
The method getCacheKey() does not exist on Axsor\PhpIPAM\Facades\PhpIPAM. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

56
                Cache::forget(PhpIPAM::/** @scrutinizer ignore-call */ getCacheKey());
Loading history...
57
                $this->configHeaders();
58
59
                return $this->call($method, $uri, $payload, true);
60
            } else {
61
                throw $e;
62
            }
63
        }
64
    }
65
66
    private function configHeaders()
67
    {
68
        $cachedData = null;
69
70
        if (Cache::has(PhpIPAM::getCacheKey())) {
71
            $cachedData = Cache::get(PhpIPAM::getCacheKey());
72
73
            if ($cachedData['expires'] <= date('Y-m-d h:i:s')) {
74
                $cachedData = null;
75
            }
76
        }
77
78
        if (! $cachedData) {
79
            $token = $this->login();
80
        }
81
82
        $this->headers = [
83
            'token' => $cachedData ? $cachedData['token'] : $token,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $token does not seem to be defined for all execution paths leading up to this point.
Loading history...
84
        ];
85
    }
86
87
    /**
88
     * Tries to login into PhpIPAM and stores API token into laravel Cache.
89
     *
90
     * @return mixed Token to connect to PhpIPAM
91
     * @throws \Axsor\PhpIPAM\Exceptions\BadCredentialsException
92
     */
93
    private function login()
94
    {
95
        $response = PhpIPAM::getClient()->post($this->config['url'].'/'.$this->config['app'].'/user/', [
96
            'auth' => [
97
                $this->config['user'],
98
                $this->config['pass'],
99
            ],
100
            'headers' => [
101
                'token' => $this->config['token'],
102
            ],
103
            'json' => [],
104
        ]);
105
106
        if ($response->getStatusCode() != self::SUCCESS_CODE) {
107
            throw new BadCredentialsException();
108
        }
109
110
        $payload = json_decode($response->getBody()->getContents(), true)['data'];
111
112
        Cache::set(PhpIPAM::getCacheKey(), $payload);
113
114
        return $payload['token'];
115
    }
116
}
117